【发布时间】:2011-03-21 15:59:32
【问题描述】:
我想以隔离的方式运行来自不同 .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实际上标有SerializableAttribute...但可能是因为RoleEntryPoint没有,所以无法做到。
有什么想法或解决方法吗?
谢谢!
【问题讨论】:
标签: c# .net reflection runtime appdomain