我创建了一个多线程服务,它使用 Castle Windsor 创建组件以在单独的线程上运行。我使用每个线程的参数按名称解析组件。
我遇到了组件使用的第 3 方库的并发问题。我怀疑将这些组件隔离在单独的 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();
}
}