views:

141

answers:

1

What you think about this approach:

Fault helper:

[Serializable]
public class WcfHwServiceFault
{
    private readonly Guid _guid;
    private readonly byte[] _data;

    private WcfHwServiceFault(Guid guid, byte[] data)
    {
        _guid = guid;
        _data = data;
    }

    public static WcfHwServiceFault Create(Exception ex)
    {
        var formatter = new SoapFormatter(null, new StreamingContext(StreamingContextStates.CrossMachine));
        var ms = new MemoryStream();
        formatter.Serialize(ms, ex);

        return new WcfHwServiceFault(ex.GetType().GUID, ms.GetBuffer());
    }

    public Exception Get()
    {
        var formatter = new SoapFormatter(null, new StreamingContext(StreamingContextStates.CrossMachine));
        var ms = new MemoryStream(_data);
        return (Exception)formatter.Deserialize(ms);
    }
}

Server side usage:

try
{
    ...
}
catch (Exception ex)
{
    throw new FaultException<WcfHwServiceFault>(WcfHwServiceFault.Create(ex));
}

Client side usage:

try
{
    Channel.DoSomeMethod(...);
}
catch (FaultException<WcfHwServiceFault> ex)
{
    throw ex.Detail.Get();
}
catch (CommunicationException ex)
{
    throw new ApplicationException("Communication error.", ex);
}
A: 

Interesting idea. It saves you decorating your services with 100's of individual exceptions.

But why not just have exception as a property of WcfHwServiceFault?

Shiraz Bhaiji
This properties will be null on client side (after WCF deserializer), because they doesn't have appropriate attributes.
SeeSharp
Can you not just mark it with the appropriate attribute?
Shiraz Bhaiji
Over http doesn't work.
SeeSharp