如何在Autofac DI中创建线程作用域

时间:2018-08-20 05:26:36

标签: multithreading autofac

我是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类?

1 个答案:

答案 0 :(得分:0)

在需要使用MyThreadScopedComponent的任何类中,将其注入到构造函数中,如下所示:

public class Test
{
    private MyThreadScopedComponent MyComponent { get; }

    public Test(MyThreadScopedComponent myComponent)
    {
        MyComponent = myComponent;
    }

    public void Foo()
    {
        MyComponent.DoStuff();
    }
}