Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 多线程和多线程_C#_Multithreading_Dryioc - Fatal编程技术网

C# 多线程和多线程

C# 多线程和多线程,c#,multithreading,dryioc,C#,Multithreading,Dryioc,我想使用dryioc来管理多个线程所需的依赖关系。我想启动线程,向每个线程传递一个需要ioc解析依赖关系的作业。不确定这应该是什么样子理想的任何援助感谢 如果需要将服务范围限定为一个线程(每个线程一个实例),则为容器设置ThreadScopeContext: RootContainer = new Container(scopeContext: new ThreadScopeContext()); RootContainer.Register<IService, MyService>

我想使用dryioc来管理多个线程所需的依赖关系。我想启动线程,向每个线程传递一个需要ioc解析依赖关系的作业。不确定这应该是什么样子理想的任何援助感谢

如果需要将服务范围限定为一个线程(每个线程一个实例),则为容器设置
ThreadScopeContext

RootContainer = new Container(scopeContext: new ThreadScopeContext());

RootContainer.Register<IService, MyService>(Reuse.InCurrentScope);

// in your thread
using (RootContainer.OpenScope())
{
    var service = RootContainer.Resolve<IService>();
    // use the service
}
从:线程安全和无锁-注册和解析可以并行进行,而不会损坏容器状态。-如果该语句为真,那么您应该能够从任何线程自由注入依赖项,而无需担心。祝你好运
RootContainer = new Container(); // without ambient scope context

RootContainer.Register<IService, MyService>(Reuse.InCurrentScope);

// in your thread
using (var scopedContainer = RootContainer.OpenScope())
{
    var service = scopedContainer.Resolve<IService>();
    // use the service
}