Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serializing exceptions over WCF

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.

like image 953
MGSoto Avatar asked Dec 18 '09 19:12

MGSoto


People also ask

What is by serialization with respect to WCF?

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.

How do you serialize an object in WCF?

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.

Should exceptions be serializable?

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.

What is the type of serializer used in WCF?

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.


1 Answers

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:

  • Specifying and Handling Faults in Contracts and Services
  • WCF Error Handling and Fault Conversion
  • WCF error handling and some best practices
  • Exception Handling in WCF using Fault Contract
  • Using the Fault Contracts (SOAP Faults) in WCF Programming
like image 54
marc_s Avatar answered Nov 06 '22 03:11

marc_s