views:

378

answers:

2

if i made my exception Serializable like this article from msdn , so can my exception serialized over WCF ?

A: 

I don't see why not, if you've successfully created an object that can be serialized over classic asmx, then it be fine in WCF.

JL
A: 

Yes you can serialize exceptions and return them with WCF. I wrote an app where it was necessary for the WCF client to have the real exception that occurred on the server; not just a high level fault.

Here are the steps to implement what we have done:

1 - Declare a class EncodedException with a single string property

public class EncodedException 
{
     public string SerializedException {get;set;}
}

2 - On your service contract add the attribute to indicate that your service could return a FaultException exception.

[ServiceContract()]
public class MyService
{
    [OperationContract]
    [FaultContract(typeof(EncodedException), 
                  ProtectionLevel = ProtectionLevel.EncryptAndSign)]
     public string Method1 ();
}

3 - In you service implementation add a try/catch in all service operations:

public void Method1()
{
    try
    {
        // some code here
    }
    catch( Exception ex)
    {
        EncodedException encodedException = Helper.SerializeException( ex );
        throw new FaulException<EncodedException>();
    }
}

4 - In your client code catch the exception and unwrap it:

public void someMethod()
{
    try
    {
        serviceClient.Method1();
    }
    catch( FaulException<EncodedException> ex)
    {
        Exception decodedException = Helper.DeserializeException( ex );
        throw new decodedException();
    }
}

5 - Write the Helper to serialize/deserialize the exception. If you need help with that part too, let me know.

I hope this helps.

Sly
so can you till me how to do this with EntLib4.1
BigOmar