我可以告诉Castle Windsor在单独的AppDomain中创建一个组件吗?

时间:2009-08-25 17:06:18

标签: castle-windsor appdomain

我创建了一个多线程服务,使用Castle Windsor创建在不同线程上运行的组件。我按名称使用每个线程的参数来解析组件。

我遇到了组件使用的第三方库的并发问题。我怀疑在单独的AppDomains中隔离这些组件将解决问题。

有没有办法让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();
    }
}

2 个答案:

答案 0 :(得分:2)

答案 1 :(得分:1)

如果您已经创建了单独的AppDomain,那么您不能只在私有AppDomain中创建一个Windsor容器的新实例吗?