从其他 .dll 加载服务并隔离运行它们

发布于 2024-10-24 18:01:06 字数 1511 浏览 1 评论 0原文

我想以隔离的方式运行来自不同 .dll 的多个服务。基本上,所有服务都派生自 RoleEntryPoint ,我想将每个服务加载到单独的 AppDomain 中,并在不同的线程中运行它。

到目前为止,我可以找到该服务并获取其类型:

        String pathToDll = @"C:\....\bin\Debug\ChildWorkerRole.dll"; 
        Assembly assembly = Assembly.LoadFrom(pathToDll);
        Type serviceType = assembly.GetTypes().SingleOrDefault(t => t.BaseType == typeof(RoleEntryPoint));

并且还在当前的 AppDomain 和 Thread 中运行它:

        RoleEntryPoint myRole2 = (RoleEntryPoint)Activator.CreateInstance(serviceType);
        if (myRole2.OnStart())
            myRole2.Run();

但是当我尝试在不同的单独环境中运行它时AppDomain 我得到一个异常:

        AppDomain domain = AppDomain.CreateDomain("MyNewDomain");
        RoleEntryPoint myRole = (RoleEntryPoint)domain.CreateInstanceFromAndUnwrap(pathToDll, serviceType.FullName);
        if (myRole.OnStart())
            myRole.Run();

这个异常:

System.Runtime.Serialization.SerializationException was unhandled
  Message=Type 'ChildWorkerRole.WorkerRole' in assembly 'ChildWorkerRole, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.
  Source=mscorlib
  StackTrace:
       at System.AppDomain.CreateInstanceFromAndUnwrap(String assemblyName, String typeName)
.......

有趣的是 ChildWorkerRole 实际上是用 SerializedAttribute 标记的......但可能是因为 RoleEntryPoint不是,这是做不到的。

有什么想法或解决方法吗?

谢谢!

I'd like to run several services from different .dll's in a isolated way. Basically, all services are derived from RoleEntryPoint , and I want to load each one in a separated AppDomain and run it there in a different thread.

So far, I can locate the service and get its type:

        String pathToDll = @"C:\....\bin\Debug\ChildWorkerRole.dll"; 
        Assembly assembly = Assembly.LoadFrom(pathToDll);
        Type serviceType = assembly.GetTypes().SingleOrDefault(t => t.BaseType == typeof(RoleEntryPoint));

And also run it in the current AppDomain and Thread:

        RoleEntryPoint myRole2 = (RoleEntryPoint)Activator.CreateInstance(serviceType);
        if (myRole2.OnStart())
            myRole2.Run();

But when I try to run it in a separate in different AppDomain I get an exception:

        AppDomain domain = AppDomain.CreateDomain("MyNewDomain");
        RoleEntryPoint myRole = (RoleEntryPoint)domain.CreateInstanceFromAndUnwrap(pathToDll, serviceType.FullName);
        if (myRole.OnStart())
            myRole.Run();

This exception:

System.Runtime.Serialization.SerializationException was unhandled
  Message=Type 'ChildWorkerRole.WorkerRole' in assembly 'ChildWorkerRole, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.
  Source=mscorlib
  StackTrace:
       at System.AppDomain.CreateInstanceFromAndUnwrap(String assemblyName, String typeName)
.......

The funny thing is that ChildWorkerRole is actually marked with the SerializableAttribute ... but may be because RoleEntryPoint is not, it cannot be done.

Any idea or workaround?

thanks!

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

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

发布评论

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

评论(1

梦途 2024-10-31 18:01:06

为了在单独的 AppDomain 中使用类型,它以及您直接使用的所有类型都需要标记为 [Serialized],或者您需要从 MarshalByRefObject

如果这是不可能的,唯一真正的选择是创建一个您可以使用的代理类,它遵循上述标准,并允许它在内部管理您的类型。

In order to work with a type in a separate AppDomain, it, and all of the types you directly use, need to be either marked as [Serializable], or you need to derive them from MarshalByRefObject.

If this is not possible, the only real option is to make a proxy class that you can use which does follow the above criteria, and allow it to manage your types internally.

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