Catching Exception thrown by a remote service running on remote machine

G

Guest

Hi
I am running a service on remote machine A and executing some code on same machine from local machine that is machine B. I can create all kinds of objects on remote machines and execute different methods on same. But if there is some exception on remote machine then I am not able to catch it on local machine. Anyone has any idea how this can be done
Please note that I am raising MyException and not Exception
Here is my exception class that I am using on remote machine to catch and throw exception
// This class definition is in a DLL which is referred by remoteservice on remote machine as well as client on local machine which is calling
// different methods from remoteservice on remote machin

public class MyException : ApplicationException

public MyException(string msg): base(message

message = msg

public MyException(SerializationInfo info, StreamingContext context): base(info, context

message = info.GetString("message")

public static MyException CreateException( string message

Trace.WriteLine( DateTime.Now + "\t" + "EXCEPTION MESSAGE" )
Trace.WriteLine( message )
Trace.WriteLine( DateTime.Now + "\t" + "STACK TRACE " )
Trace.WriteLine( stackTrace )
Trace.Flush()
return new MyException( message )



and I am invoking this in RemoteService of Client Activated Object type as

Tr

//Do somethin

catch(Exception e

throw MyException.CreateException(e.message)


Let me know why it does not work

Thanks
Vina
 
J

José Joye

You may have a look there:
http://www.ingorammer.com/RemotingFAQ/CustomExceptions.html

José
VInay said:
Hi,
I am running a service on remote machine A and executing some code on same
machine from local machine that is machine B. I can create all kinds of
objects on remote machines and execute different methods on same. But if
there is some exception on remote machine then I am not able to catch it on
local machine. Anyone has any idea how this can be done.
Please note that I am raising MyException and not Exception.
Here is my exception class that I am using on remote machine to catch and throw exception,
// This class definition is in a DLL which is referred by remoteservice on
remote machine as well as client on local machine which is calling
 
D

Dave

When using custom exceptions you need to ensure that they are serializable.
What happens is that the exception object is itself like any other object
and must be deserialized on the receiving end. To make this work you need to
add the
[Serializable] attribute to the MyException class, as in:

[Serializable]
public class MyException : ApplicationException

....and make sure that the assembly that contains the exception definition is
available on both machines.

Also, if you add custom fields you should probably implement the
ISerializable interface. You already have the constructor for that interface
in your class but you did not declare that your class derives from it, so
add that to the declaration, as in...

[Serializable]
public class MyException : ApplicationException : ISerializable

When you do this you will need to add a method called GetObjectData which
serializes the object into the serialization stream, the signature for which
is:
public void GetObjectData(object graph,SerializationInfo info,
StreamingContext context);


Also, unless you are creating a new field "message" you don't really need to
duplicate the code that already exists in the base class. I usually argue
against extending the exception hierarchy unless there really is some
benefit to doing so, especially when the exception can be thrown across an
appdomain boundary, because of the serialization, versioning, and deployment
issues.


VInay said:
Hi,
I am running a service on remote machine A and executing some code on same
machine from local machine that is machine B. I can create all kinds of
objects on remote machines and execute different methods on same. But if
there is some exception on remote machine then I am not able to catch it on
local machine. Anyone has any idea how this can be done.
Please note that I am raising MyException and not Exception.
Here is my exception class that I am using on remote machine to catch and throw exception,
// This class definition is in a DLL which is referred by remoteservice on
remote machine as well as client on local machine which is calling
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top