我可以告诉 Castle Windsor 在单独的 AppDomain 中创建组件吗?
我创建了一个多线程服务,它使用 Castle Windsor 创建在单独线程上运行的组件。我通过每个线程的名称和参数解析组件。
我遇到了组件使用的第三方库的并发问题。我怀疑将这些组件隔离在单独的 AppDomain 中可以解决该问题。
有没有办法让 Resolve 使用不同的 AppDomain 创建组件?
private ActivityThread NewActivityThread(ActivityInstance activityInstance)
{
// Set up the creation arguments.
System.Collections.IDictionary arguments = new Dictionary<string, string>();
activityInstance.Parameters.ForEach(p => arguments.Add(p.Name, p.Value));
// Get the activity handler from the container.
IActivity activity = Program.Container.Resolve<IActivity>(activityInstance.Name, arguments);
// Create a thread for the activity.
ActivityThread thread = new ActivityThread(activity, activityInstance, _nextActivityID++);
return thread;
}
public ActivityThread(IActivity activity, ActivityInstance instance, int id)
{
_activity = activity;
_instance = instance;
_id = id;
}
public void Start()
{
if (_thread == null)
{
// Create a new thread to run this activity.
_thread = new Thread(delegate() { _activity.Run(); });
_thread.Name = _activity.ToString();
_thread.SetApartmentState(ApartmentState.STA);
_thread.Start();
}
}
I've created a multi-threaded service that uses Castle Windsor to create components to run on separate threads. I Resolve an component by name with parameters for each thread.
I'm running into concurrency problems with a 3rd party library used by the components. I suspect that isolating those components in separate AppDomains will resolve the problem.
Is there a way to have Resolve create the component using a different AppDomain?
private ActivityThread NewActivityThread(ActivityInstance activityInstance)
{
// Set up the creation arguments.
System.Collections.IDictionary arguments = new Dictionary<string, string>();
activityInstance.Parameters.ForEach(p => arguments.Add(p.Name, p.Value));
// Get the activity handler from the container.
IActivity activity = Program.Container.Resolve<IActivity>(activityInstance.Name, arguments);
// Create a thread for the activity.
ActivityThread thread = new ActivityThread(activity, activityInstance, _nextActivityID++);
return thread;
}
public ActivityThread(IActivity activity, ActivityInstance instance, int id)
{
_activity = activity;
_instance = instance;
_id = id;
}
public void Start()
{
if (_thread == null)
{
// Create a new thread to run this activity.
_thread = new Thread(delegate() { _activity.Run(); });
_thread.Name = _activity.ToString();
_thread.SetApartmentState(ApartmentState.STA);
_thread.Start();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试远程工具。请参阅:
Try the remoting facility. See:
如果您已经创建了一个单独的 AppDomain,难道您不能在私有 AppDomain 中创建一个 Windsor 容器的新实例吗?
If you've already created a separate AppDomain, can't you just create a new instance of a Windsor container within the private AppDomain?