Class HttpListenerRouter

Routes HttpListenerContext instances through a NanoRoute pipeline.

Inheritance
object
RoutingContext
Router
HttpListenerRouter
Inherited Members
Router.TraceIdName
Router.OriginalRequestName
Router.Handle(HttpRequestMessage, IServiceProvider, CancellationToken)
object.GetType()
object.MemberwiseClone()
object.ToString()
object.Equals(object)
object.Equals(object, object)
object.ReferenceEquals(object, object)
object.GetHashCode()
Namespace: NanoRoute
Assembly: NanoRoute.dll
Syntax
public class HttpListenerRouter : Router
Remarks

This adapter converts incoming HttpListener traffic into the core HttpRequestMessage/HttpResponseMessage pipeline used by NanoRoute.

Properties

Config

Configuration assigned to this instance.

Declaration
public HttpListenerRouterConfig Config { get; }
Property Value
Type Description
HttpListenerRouterConfig

Methods

CreateBuilder()

Creates a strongly typed builder for configuring an HttpListenerRouter.

Declaration
public static RouterBuilder<HttpListenerRouter, HttpListenerRouterConfig> CreateBuilder()
Returns
Type Description
RouterBuilder<HttpListenerRouter, HttpListenerRouterConfig>

A builder that can register handlers, value parsers, and router configuration.

Route(HttpListenerContext, IServiceProvider, CancellationToken)

Routes a single HttpListener request and writes the produced response.

Declaration
public Task Route(HttpListenerContext context, IServiceProvider services, CancellationToken cancellation = default)
Parameters
Type Name Description
HttpListenerContext context

The listener context that supplies the request and receives the response.

IServiceProvider services

The service provider exposed to handlers through Services.

CancellationToken cancellation

A token that can cancel request processing and response streaming.

Returns
Type Description
Task

A task that completes after the router has finished writing the response.

Remarks

Request and content headers are copied into the intermediate HttpRequestMessage. The original HttpListenerRequest is stored in OriginalRequestName on the generated request message. Response headers are copied back except for reserved HttpListenerResponse headers that must be managed by HttpListener itself. Cancellation is not translated into an HTTP error response by this adapter.

Examples
HttpListenerRouter router = HttpListenerRouter
    .CreateBuilder()
    .AddJsonErrorDetails()
    .AddDefaultValueParsers()
    .AddHandler("GET", "/hello/{name:str}", (context, _) =>
        Task.FromResult(HttpResponseMessage.Json(new
        {
            message = $"Hello {context.Parameters["name"]}"
        })))
    .CreateRouter();

await router.Route(listenerContext, services, cancellationToken);
Exceptions
Type Condition
OperationCanceledException

Thrown when the caller cancels cancellation. The listener response is aborted before the exception is rethrown.

In this article
Back to top Generated by DocFX