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

发布于 2024-08-02 09:35:56 字数 1252 浏览 6 评论 0原文

我创建了一个多线程服务,它使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月寒剑心 2024-08-09 09:35:56

如果您已经创建了一个单独的 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文