从现有的
我试图在每个请求的基础上限定我的服务的作用域,但是我注入的服务似乎总是被限定在父/根容器的作用域,并且后续请求看到来自前一个请求的状态。
我已经在我的启动类中设置了一个自定义的StructureMap容器
// Wireup the container
Container = new Container();
Container.Configure(config =>
{
// register the dotnet framework registered services into our container
config.Populate(services);
// Import all the registries
config.Scan(scanner =>
{
scanner.TheCallingAssembly();
scanner.LookForRegistries();
});
});
return Container.GetInstance<IServiceProvider>();
将服务注册为;
For<ITest>().Use<Test>().ContainerScoped();
然后连接以下中间件
var container = serviceProvider.GetRequiredService(typeof(IContainer)) as IContainer;
if (container != null)
{
using (var requestContainer = container.GetNestedContainer())
{
context.RequestServices = requestContainer.GetInstance<IServiceProvider>();
await _next.Invoke(context);
}
}
else
{
await _next.Invoke(context);
}
任何指针,我错过了什么?由于
不需要自己连接RequestServices
属性。ASP。. NET主机已经为您完成了。
RequestServices
提供程序中解析您需要的内容。