首页 新闻 会员 周边 捐助

Aap.net core 中间件为什么不能使用 services.AddScoped 注册的services ?

0
悬赏园豆:10 [已关闭问题] 关闭于 2017-11-28 02:03

直接上代码吧

public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<IOperationScoped, Operation>();
}

 

private readonly RequestDelegate _next;
private readonly IOperationScoped _scopedOperation;

public ServiceLifetimesMiddleware(RequestDelegate next,
IOperationScoped scopedOperation)
{
_next = next;
_scopedOperation = scopedOperation;
}

 

public async Task Invoke(HttpContext context)
{
await context.Response.WriteAsync($"Scoped:{_scopedOperation.OperationId}\r\n");

}

 

在中间件中报错:System.InvalidOperationException: Cannot resolve scoped service 'IOperationScoped' from root provider.

为什么在 mvc 下可以使用, mvc不也是个中间件吗?

华子哥的主页 华子哥 | 菜鸟二级 | 园豆:202
提问于:2017-11-28 01:44
< >
分享
所有回答(1)
0

将 IOperationScoped scopedOperation 写到 Invoke(HttpContext context) 参数里面

cctvkill1 | 园豆:202 (菜鸟二级) | 2018-01-25 20:48
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册