【发布时间】:2021-01-27 00:19:18
【问题描述】:
我有一些客户端代码通过远程调用一个方法
myProxy = (IProxy)Activator.GetObject(
typeof(IProxy),
"tcp://" + address + ":" + theControllerPort + "/Proxy");
IProxy 定义为
public interface IProxy {
void DoStuff();
}
代理被定义为
public class Proxy : MarshalByRefObject, IProxy {
public void DoStuff() {
throw new DerivedException();
}
}
我有一个名为 DerivedException 的异常定义为
[Serializable]
public class DerivedException: ApplicationException {
public DerivedException() : base () { }
public DerivedException(string message) : base (message) { }
public DerivedException(string message, Exception innerException) : base (message, innerException) { }
protected DerivedException(SerializationInfo info, StreamingContext context) : base(info, context) { }
}
当我调用 myProxy.DoStuff()
try{
result = myProxy.DoStuff();
}
catch (Exception ex) {
myLog.Error("Error", ex);
}
我得到一个例外:
"Unable to find assembly 'xyz, Version=123, Culture=neutral, PublicKeyToken=null'."
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
...
我认为我的客户端代码应该能够反序列化抛出的 DerivedException,因为 DerivedException 继承自 Exception,并且客户端代码设置为捕获 Exception。我在这里做错了什么?如果我在客户端代码的 bin/debug 文件夹中包含定义 DerivedException 的 .dll,则客户端会正确接收异常。有没有其他方法可以让这个异常反序列化并被客户端代码正确捕获?
【问题讨论】: