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.
The process forms a sequence of bytes into a logical object; this is called an encoding process. At runtime when WCF receives the logical message, it transforms them back into corresponding . Net objects. This process is called serialization.
This code constructs an instance of the DataContractSerializer that can be used only to serialize or deserialize instances of the Person class. DataContractSerializer dcs = new DataContractSerializer(typeof(Person)); // This can now be used to serialize/deserialize Person but not PurchaseOrder.
Whenever you create a custom exception type, your exception type should always support serialization. You do this using the Serializable attribute. Exceptions should be serializable so that they can automatically be marshalled across application domains or threads.
Windows Communication Foundation (WCF) uses the DataContractSerializer as its default serialization engine to convert data into XML and to convert XML back into data. The DataContractSerializer is designed to serialize data contract types.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With