Application Builder Map 不适用于中间件
Application Builder Map does not work for middlewares
我遇到了以下问题:
我有一个 ASP NET Core
应用程序,我在其中使用以下路由:
status
,
message
,
http
.
前2个accept a websocket request.
问题是管道中的 AppBuilder.Map
不起作用,它总是将我发送到所有请求的第一条路线。
计划
class Program
{
static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args)=>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseKestrel()
.UseSockets()
.UseUrls($"http://0.0.0.0:{Constants.SERVER_PORT}/")
.Build();
}
启动
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<StatusService>();//(x => new StatusService());
services.AddTransient<RegisterService>();
services.AddTransient<MessagingService>();
services.AddCors();
}
public void Configure(IApplicationBuilder builder)
{
builder.UseCors((p) => p.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod().AllowCredentials());
builder.UseWebSockets();
builder.Map("/status", app =>
{
builder.UseMiddleware<StatusWare>();
});
builder.Map("/http", app =>
{
builder.UseMiddleware<HTTPWare>();
});
builder.Map("/message", app =>
{
builder.UseMiddleware<MessageWare>();
});
}
}
中间件都使用它们的特定服务,我不会post因为不会调用其他两个中间件的Invoke
方法。
中间件
状态
class StatusWare
{
StatusService handler;
public StatusWare(StatusService _handler,RequestDelegate del)
{
this.handler = _handler;
this.next = del;
}
RequestDelegate next;
public async Task Invoke(HttpContext context)
{
if (!context.WebSockets.IsWebSocketRequest)
{
await this.next(context);
return;
}
await this.handler.AddClientAsync(context.WebSockets);
}
}
留言
class MessageWare
{
private MessagingService messageService;
private RequestDelegate next;
public MessageWare(MessagingService serv,RequestDelegate del)
{
this.messageService = serv;
this.next = del;
}
public async Task Invoke(HttpContext context)
{
if (!context.WebSockets.IsWebSocketRequest)
{
await next(context);
}
await this.messageService.AcceptClientAsync(context.WebSockets);
}
}
HTTP
class HTTPWare
{
RequestDelegate next;
RegisterService registerService;
public HTTPWare(RequestDelegate _del,RegisterService service)
{
this.next = _del;
this.registerService = service;
}
public async Task Invoke(HttpContext context)
{
}
}
如您所见,中间件几乎相同(我没有在 HttpWare
中编写任何内容,因为它的 Invoke
方法也不会被调用。
所以我的问题是。为什么尽管使用 AppBuilder.Map
所有请求都进入第一个中间件 StatusWare
?
难道是因为在ConfigureServices
中添加特定服务的方式?
Configure
正在对原始 builder
而不是委托参数调用 UseMiddleware
。在这种情况下 app
更新 Map
调用以在构建器委托参数上使用中间件。
public void Configure(IApplicationBuilder builder) {
builder.UseCors((p) => p.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod().AllowCredentials());
builder.UseWebSockets();
builder.Map("/status", app => {
app.UseMiddleware<StatusWare>();
});
builder.Map("/http", app => {
app.UseMiddleware<HTTPWare>();
});
builder.Map("/message", app => {
app.UseMiddleware<MessageWare>();
});
}
我遇到了以下问题:
我有一个 ASP NET Core
应用程序,我在其中使用以下路由:
status
, message
, http
.
前2个accept a websocket request.
问题是管道中的 AppBuilder.Map
不起作用,它总是将我发送到所有请求的第一条路线。
计划
class Program
{
static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args)=>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseKestrel()
.UseSockets()
.UseUrls($"http://0.0.0.0:{Constants.SERVER_PORT}/")
.Build();
}
启动
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<StatusService>();//(x => new StatusService());
services.AddTransient<RegisterService>();
services.AddTransient<MessagingService>();
services.AddCors();
}
public void Configure(IApplicationBuilder builder)
{
builder.UseCors((p) => p.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod().AllowCredentials());
builder.UseWebSockets();
builder.Map("/status", app =>
{
builder.UseMiddleware<StatusWare>();
});
builder.Map("/http", app =>
{
builder.UseMiddleware<HTTPWare>();
});
builder.Map("/message", app =>
{
builder.UseMiddleware<MessageWare>();
});
}
}
中间件都使用它们的特定服务,我不会post因为不会调用其他两个中间件的Invoke
方法。
中间件
状态
class StatusWare
{
StatusService handler;
public StatusWare(StatusService _handler,RequestDelegate del)
{
this.handler = _handler;
this.next = del;
}
RequestDelegate next;
public async Task Invoke(HttpContext context)
{
if (!context.WebSockets.IsWebSocketRequest)
{
await this.next(context);
return;
}
await this.handler.AddClientAsync(context.WebSockets);
}
}
留言
class MessageWare
{
private MessagingService messageService;
private RequestDelegate next;
public MessageWare(MessagingService serv,RequestDelegate del)
{
this.messageService = serv;
this.next = del;
}
public async Task Invoke(HttpContext context)
{
if (!context.WebSockets.IsWebSocketRequest)
{
await next(context);
}
await this.messageService.AcceptClientAsync(context.WebSockets);
}
}
HTTP
class HTTPWare
{
RequestDelegate next;
RegisterService registerService;
public HTTPWare(RequestDelegate _del,RegisterService service)
{
this.next = _del;
this.registerService = service;
}
public async Task Invoke(HttpContext context)
{
}
}
如您所见,中间件几乎相同(我没有在 HttpWare
中编写任何内容,因为它的 Invoke
方法也不会被调用。
所以我的问题是。为什么尽管使用 AppBuilder.Map
所有请求都进入第一个中间件 StatusWare
?
难道是因为在ConfigureServices
中添加特定服务的方式?
Configure
正在对原始 builder
而不是委托参数调用 UseMiddleware
。在这种情况下 app
更新 Map
调用以在构建器委托参数上使用中间件。
public void Configure(IApplicationBuilder builder) {
builder.UseCors((p) => p.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod().AllowCredentials());
builder.UseWebSockets();
builder.Map("/status", app => {
app.UseMiddleware<StatusWare>();
});
builder.Map("/http", app => {
app.UseMiddleware<HTTPWare>();
});
builder.Map("/message", app => {
app.UseMiddleware<MessageWare>();
});
}