我正在尝试从自定义域在COM组件内进行调用。 问题是当我尝试解开ObjectHandle时,引发了序列化异常。
但是,如果我使用当前的AppDomain创建实例,则它可以正常工作。...
异常消息:
An unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in ConsoleApp1.exe
Additional information: Type 'MyAddin.Main' in assembly 'MyAddin, Version=2019.0.1.5, Culture=neutral, PublicKeyToken=null' is not marked as serializable.
我的代码:
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string addinPath = "C:\\sources\\MyAddin\\bin\\x64\\Debug\\MyAddin.dll";
string addinFolder = "C:\\sources\\MyAddin\\bin\\x64\\Debug\\";
string addinConfigPath = "C:\\sources\\MyAddin\\bin\\x64\\Debug\\MyAddin.dll.config";
System.AppDomainSetup setup = new System.AppDomainSetup();
setup.ApplicationBase = addinFolder;
setup.ConfigurationFile = addinConfigPath;
setup.ApplicationName = "MyAddin.dll";
string strClsid = "{2616ad89-f4d1-4dc7-9d9d-a5de101b9085}"; // CLSID of my COM addin
System.AppDomain customDomain = System.AppDomain.CreateDomain(strClsid, null, setup);
// The type of domain is System.Runtime.Remoting.Proxies.__TransparentProxy}
// The type of custom domain is
System.Type addinComType = System.Type.GetTypeFromCLSID(System.Guid.Parse(strClsid));
System.Runtime.Remoting.ObjectHandle addinInstanceObjectHandle = customDomain.CreateComInstanceFrom(addinPath, addinComType.FullName);
System.Object addinInstance = addinInstanceObjectHandle.Unwrap(); //Throw the Serialization exception when create COM instance from customDomain
// But working if I did System.AppDomain.CurrentDomain.CreateComInstanceFrom
System.Reflection.MethodBase myMethod = addinInstance.GetType().GetMethod("connectToEwAPI");
System.Object[] parameters = { null };
myMethod.Invoke(addinInstance, parameters);
}
}
}
所以我做些蠢事? 你知道我在想什么吗?
预先感谢您的帮助
我尝试使用更基本的Assembly。 所以我有一个仅包含此代码的C#程序集
namespace ClassLibrary2
{
public class Class1
{
public Class1()
{
}
public string MyMethod() => "OK";
}
}
我的exe代码就是
static void Main(string[] args)
{
string basePath = @"C:\source\MyAddin\ConsoleApp1\ClassLibrary2\bin\Debug";
string dllName = @"ClassLibrary2";
string typeName = "ClassLibrary2.Class1";
string dllFullpath = $"{basePath}\\{dllName}.dll";
try
{
ObjectHandle objectHandle = AppDomain.CurrentDomain.CreateInstanceFrom(dllFullpath, typeName);
Object addinObject = objectHandle.Unwrap();
var myAddinMethod = addinObject.GetType().GetMethod("MyMethod");
string result = myAddinMethod.Invoke(addinObject, null) as string; // Working
AppDomainSetup setup = new AppDomainSetup()
{
ApplicationBase = basePath,
ApplicationName = dllName,
ConfigurationFile = dllName + ".dll.config",
PrivateBinPath = basePath
};
AppDomain customDomain = AppDomain.CreateDomain("MyDomain", null, setup);
ObjectHandle objectHandleFromCustomDomain = customDomain.CreateInstanceFrom(dllFullpath, typeName);
Object addinObjectFromCustomDomain = objectHandleFromCustomDomain.Unwrap(); // Exception thrown
var myAddinMethodFromCustomDomain = addinObjectFromCustomDomain.GetType().GetMethod("MyMethod");
string resultFromCustomDomain = myAddinMethodFromCustomDomain.Invoke(myAddinMethodFromCustomDomain, null) as string;
}
catch(Exception e)
{
var t = e.Message; // Exception thrown: 'System.Runtime.Serialization.SerializationException' by objectHandleFromCustomDomain.Unwrap()
}
}
如您所见,它与默认域一起使用,而不与自定义域一起使用。...
你有什么主意吗?
答案 0 :(得分:0)
如果要跨越BehaviorRelay
边界访问对象,则需要执行以下两项操作之一:
设置类AppDomain
,例如通过添加Serializable
属性。在这种情况下,将复制对象的副本,并跨[Serializable]
边界传递该对象。您调用的所有膜都将在副本上,并且不会影响原始对象。
使类从MarshalByRefObject派生。在这种情况下,将在AppDomain边界上整理对对象的引用,并且您调用的任何成员都会在其原始AppDomain中影响该对象。