views:

74

answers:

2

I have a task running on a remote system connecting back to a server using WCF. It is very possible that the task can throw exceptions, and I'd like those exceptions to be channeled back to the server. Essentially I want to do something like this:

Client:

server.SendException(new UnauthorizedAccessException("Some Exception"));

Server:

public void SendException(Exception e)
{
    throw e;
}

Contract:

[ServiceContract]
public interface IServerContract
{
    [OperationContract]
    void SendException(Exception e);
}

I've been reading a little bit about fault exceptions, but from what I understand, they are used for specific exceptions that are running in a method called over WCF. What I want is to send any type of exception that the application has thrown outside of WCF context, and then channel them back to the server for further processing. If it helps in a solution, the server also has a connection back to the client.

+2  A: 

You need to catch the exceptions on the server and package them up into SOAP faults to send them back over the wire. Otherwise, your client-server channel will be "faulted" and unusable.

A SOAP fault is basically the interoperable SOAP equivalent of a .NET exception. You are not supposed to throw .NET exceptions because of the very fact they are .NET specific - WCF and SOA is by definition system-agnostic.

If both ends of the communication wire are indeed guaranteed to be .NET, you can easily wrap up any .NET exception into a FaultException<T> and thus easily channel back .NET specific exception information using a SOAP compliant "transport vehicle".

Read up more on SOAP faults and how to turn your .NET exceptions on the server into SOAP faults on the wire here:

marc_s
This is what I have found most places to say, unfortunately I need it to go in the other direction, as in I'm trying to catch client exceptions and send them to the server.
MGSoto
Well, use the same mechanism : create a service method, e.g. "ReceiveFault", and make it's parameter a "FaultException<T>". Now, from the client, when something goes wrong, wrap up your .NET exception into a FaultException<T> and call "ReceiveFault" on your server
marc_s
While the method I chose worked, this is probably the more proper way of doing this.
MGSoto
+2  A: 

For what I needed, I solved my own problem. I used the ExceptionDetail class as provided by ServiceModel, and used that to send relevant exception data back to the server.

MGSoto