AppDomain.CreateInstanceAndUnwrap 失败并显示“类型未标记为可序列化”

发布于 2025-01-03 21:28:29 字数 1020 浏览 4 评论 0原文

我有这段代码(简化)

internal class Worker : MarshalByRefObject {
    public void DoWork() {
    }
}

internal class WorkerInvoker {
    public void InvokeWorker() {
        var newDomain = AppDomain.CreateDomain("Work", null, new AppDomainSetup { ApplicationBase = AppDomain.CurrentDomain.BaseDirectory, PrivateBinPath = AppDomain.CurrentDomain.RelativeSearchPath });
        try {
FAIL HERE>  var worker = (Worker)newDomain.CreateInstanceAndUnwrap(typeof(Worker).Assembly.FullName, typeof(Worker).FullName);
            worker.DoWork();
        }
        finally {
            if (newDomain != null)
                AppDomain.Unload(newDomain);
        }
    }
}

,但指示行 (CreateInstanceAndUnwrap) 失败并显示

程序集“Castle.Windsor,Version=3.0.0.0,Culture=neutral,PublicKeyToken=407dd0808d44fbdc”中的类型“Castle.MicroKernel.Lifestyle.Scoped.CallContextLifetimeScope”未标记为可序列化。

这是为什么呢?我确实在应用程序中使用 Castle Windsor,但我不尝试在应用程序域之间传递 CallContextLifetimeScope 实例。

I have this code (simplified)

internal class Worker : MarshalByRefObject {
    public void DoWork() {
    }
}

internal class WorkerInvoker {
    public void InvokeWorker() {
        var newDomain = AppDomain.CreateDomain("Work", null, new AppDomainSetup { ApplicationBase = AppDomain.CurrentDomain.BaseDirectory, PrivateBinPath = AppDomain.CurrentDomain.RelativeSearchPath });
        try {
FAIL HERE>  var worker = (Worker)newDomain.CreateInstanceAndUnwrap(typeof(Worker).Assembly.FullName, typeof(Worker).FullName);
            worker.DoWork();
        }
        finally {
            if (newDomain != null)
                AppDomain.Unload(newDomain);
        }
    }
}

but the indicated line (CreateInstanceAndUnwrap) fails with

Type 'Castle.MicroKernel.Lifestyle.Scoped.CallContextLifetimeScope' in assembly 'Castle.Windsor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc' is not marked as serializable.

Why is this? I do use Castle Windsor in the application, but am not trying to pass a CallContextLifetimeScope instance between app domains.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

篱下浅笙歌 2025-01-10 21:28:29

此问题现已在 Windsor 3.1 中修复。

当跨 AppDomain 使用范围生活方式时,就会发生这种情况。一些测试运行程序(如 MsTest)或其他库(如 Reporting Services)正在另一个应用程序域中执行某些操作,这就是您看到此情况的原因。

This issue is now fixed in Windsor 3.1.

It happens when scoped lifestyle is used across AppDomains. Some test runners (like MsTest) or other libraries (like Reporting Services) are doing some stuff in another app domain and that's why you're seeing this.

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