Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
267 views
in Technique[技术] by (71.8m points)

c# - Throwing an exception on a remoting server not defined in the client causes an "Unable to find assembly" exception

I have some client code calling a method via remoting using the

myProxy = (IProxy)Activator.GetObject(
                    typeof(IProxy),
                    "tcp://" + address + ":" + theControllerPort + "/Proxy");

IProxy is defined as

    public interface IProxy  {
        void DoStuff();
    }

Proxy is defined as

    public class Proxy : MarshalByRefObject, IProxy {
        public void DoStuff() {
            throw new DerivedException();
        }
    }

I have an Exception called DerivedException defined as

    [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) { }
    }

When I call myProxy.DoStuff()

try{
    result = myProxy.DoStuff();
}
catch (Exception ex) {
    myLog.Error("Error", ex);
}

I get an exception:

"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)
...

I would think that my client code should be able to deserialize the thrown DerivedException since DerivedException inherits from Exception and the client code is set up to catch Exception. What am I doing wrong here? If I include the .dll that defines DerivedException in the bin/debug folder of the client code, the exception is received by the client properly. Is there any other way to get this exception to deserialize and be caught properly by the client code?

question from:https://stackoverflow.com/questions/65911209/throwing-an-exception-on-a-remoting-server-not-defined-in-the-client-causes-an

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The issue will be related to the fact that your Proxy derives from MarshalByRefObject and it that case an object reference is passed from one application domain to another, rather than the object itself.

On the other hand your DerivedException is marked as Serializable. When class is marked as Serializable it will automatically be serialized, transported from the one application domain to the other, and then deserialized to produce an exact copy of the object in the second application domain.

I would try mark Proxy class as Serializable. When this object is used with remoting, the formatter responsible for serialization, takes control of the serialization process, and replaces all objects derived from MarshalByRefObject with a proxy.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...