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