我創建了一個多線程服務,使用Castle Windsor創建組件以在單獨的線程上運行。我使用每個線程的參數通過名稱解析組件。我可以告訴Castle Windsor在單獨的AppDomain中創建組件嗎?
我遇到了組件使用的第三方庫的併發問題。我懷疑在單獨的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();
}
}
你肯定要在其他* AppDomain中*的東西,而不是每個線程一個組件實例嗎? (請參閱http://www.castleproject.org/container/documentation/trunk/usersguide/lifestyles.html中的PerThread) – 2009-08-25 17:26:33
我試圖使用AppDomains將可能有害的組件分離出來。例如,使用實例化COM對象的第三方庫。 – 2009-09-10 21:42:30