Class HttpServer
A simple HTTP server.
[CLSCompliant(false)]
[MustDisposeResource]
public abstract class HttpServer : IDisposable
- Inheritance
-
objectHttpServer
- Implements
- Derived
- Extension Methods
Constructors
HttpServer(ushort, bool)
Gets ready to serve HTTP requests. Call StartHandlingRequests() after completing any additional setup.
protected HttpServer(ushort port = 0, bool localOnly = false)
Parameters
portushortThe TCP port to listen on;
0to automatically pick free port.localOnlybooltrueto only respond to requests from the local machine instead of the network.
Exceptions
- WebException
Unable to serve on the specified
port, or unable to find a free port ifportis0.- NotAdminException
Needs admin rights to serve HTTP requests.
Properties
IsListening
Indicates whether the server is currently accepting requests.
public bool IsListening { get; }
Property Value
Remarks
Becomes false after Dispose() or if the server stopped due to an unrecoverable failure.
MaxConcurrentRequests
The maximum number of requests to handle at the same time. Additional requests are rejected with ServiceUnavailable.
public int MaxConcurrentRequests { get; set; }
Property Value
Port
The TCP port the server is listening on.
public ushort Port { get; }
Property Value
Methods
Dispose()
Stops serving HTTP requests. Does not wait for requests that are currently being handled to complete.
public virtual void Dispose()
HandleRequest(HttpListenerContext)
Handles a single HTTP request.
protected abstract void HandleRequest(HttpListenerContext context)
Parameters
contextHttpListenerContext
StartHandlingRequests()
To be called by derived constructor when setup is complete.
protected void StartHandlingRequests()