从其他 .dll 加载服务并隔离运行 [英] Loading services from other .dll and run them isolated
问题描述
我想以隔离的方式运行来自不同 .dll 的多个服务.基本上,所有服务都源自 RoleEntryPoint
,我想将每个服务加载到单独的 AppDomain
中,并在不同的线程中运行它.
到目前为止,我可以找到该服务并获取其类型:
String pathToDll = @"C:\....\bin\Debug\ChildWorkerRole.dll";装配装配 = Assembly.LoadFrom(pathToDll);类型 serviceType = assembly.GetTypes().SingleOrDefault(t => t.BaseType == typeof(RoleEntryPoint));
并且也在当前的AppDomain
和Thread
中运行:
RoleEntryPoint myRole2 = (RoleEntryPoint)Activator.CreateInstance(serviceType);如果 (myRole2.OnStart())myRole2.Run();
但是当我尝试在不同的 AppDomain 中单独运行它时,出现异常:
AppDomain domain = AppDomain.CreateDomain("MyNewDomain");RoleEntryPoint myRole = (RoleEntryPoint)domain.CreateInstanceFromAndUnwrap(pathToDll, serviceType.FullName);如果 (myRole.OnStart())myRole.Run();
这个异常:
System.Runtime.Serialization.SerializationException 未处理Message=Type 'ChildWorkerRole.WorkerRole' in assembly 'ChildWorkerRole, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' 未标记为可序列化.源=mscorlib堆栈跟踪:在 System.AppDomain.CreateInstanceFromAndUnwrap(String assemblyName, String typeName)......
有趣的是,ChildWorkerRole
实际上是用 SerializableAttribute
标记的……但可能是因为 RoleEntryPoint
不是,所以不能完成.
有什么想法或解决方法吗?
谢谢!
为了在单独的 AppDomain 中使用类型,它以及您直接使用的所有类型都需要标记为 [Serializable]
,或者您需要从 MarshalByRefObject 派生它们.>
如果这是不可能的,唯一真正的选择是制作一个您可以使用的代理类,它确实遵循上述标准,并允许它在内部管理您的类型.
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!
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.
这篇关于从其他 .dll 加载服务并隔离运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!