我是autofac DI的新手。我从下面的URL关注了autofac范围的官方文档。
https://autofaccn.readthedocs.io/en/latest/lifetime/instance-scope.html#thread-scope
这里提到创建线程作用域,给出的示例是
var builder = new ContainerBuilder();
builder.RegisterType<MyThreadScopedComponent>()
.InstancePerLifetimeScope();
var container = builder.Build();
如何创建MyThreadScopedComponent类?
答案 0 :(得分:0)
在需要使用MyThreadScopedComponent
的任何类中,将其注入到构造函数中,如下所示:
public class Test
{
private MyThreadScopedComponent MyComponent { get; }
public Test(MyThreadScopedComponent myComponent)
{
MyComponent = myComponent;
}
public void Foo()
{
MyComponent.DoStuff();
}
}