中间件只初始化一次,当您注册设置。您需要在Invoke方法中解析您的依赖关系。
public class MyMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger _logger;
public MyMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
var service = context.RequestServices.GetService<IMyService>();
service.SetCompany("My Company");
await _next.Invoke(context);
}
}
现在该服务按照每个请求而不是每个应用程序的生命周期正确解析。
编辑:
为了让您的中间件被称为
即在授权middelware被称为你会做这样的事情在你的Configure(IAppBuilder app)
方法:
app.UseCookieAuthentication(options => { ... });
app.UseJwtBearerAuthentication(options => { ... });
app.UseMiddleware<MyMiddleware>(options => { ... });
然后在一个请求,首先会调用cookie中间件。如果它可以处理该方案并且失败,那么以下中间件将不会被执行。如果它不能处理该方案,下一个将被称为(jwt承载)。如果通过,下一个中间件(MyMiddleware
)将被调用。
换句话说,当您调用MyMiddleware.Invoke()
方法时,用户已被认证。
调用GetService不起作用context.RequestServices.GetService();不允许类型参数。你确定这对RC2有用吗?我还需要在授权后设置公司,在HandleAuthenticateAsync()之后调用Invoke? –
是的,你需要'使用Microsoft.Extensions.DependencyInjection'命名空间声明。或者只是使用非通用版本:'.GetService(typeof(IMyService))',但是在将它分配给一个变量之前,您必须先将它转换为 – Tseng
我不认为您想重写Invoke,I获取有关具有多个公共“调用”方法的错误。(我继承自AuthenticationMiddleware) –