1

I Want to use custome FaultException like this

 [DataContract]
public class Fault
{
    [DataMember]
    public string Message { get; set; }
}

public class MyFault<T>:FaultException<T> where T : Fault
{
     public MyFault(T faultClass)
        : base(faultClass, new FaultReason(faultClass.Message))
    {

    }
}

...

throw new MyFault<Fault>(new Fault {Message = "hello"});

...

Interface

[OperationContract]
[FaultContract(typeof(Fault))]
string GetData2(int value);

but when it get to the client it's transform to FaultException

      try
        {
            var res = serv.GetData2(1);
        }
        catch (MyFault<WcfService1Sample.Fault> ex)
        {
            label1.Text = ex.Reason.ToString(); //-> i want here
        }
        catch (FaultException<ServiceReference1.Fault> ex)
        {
            label1.Text = ex.Reason.ToString(); //-> catch here
        }
        catch (Exception)
        {

            throw;
        }

can i catch custom fault on clint ? or the WCF automatic convert it to FaultException how do i make this issue work

thanks kfir

kfir
  • 21
  • 3
  • Your `Fault` class should have a `DataContract` attribute, and the `Message` property should be a `DataMember`. – anton.burger Aug 29 '12 at 09:37
  • you right i forget to put data contract. but even if i add it it dosnt work lik i want it to work. – kfir Aug 30 '12 at 05:21

1 Answers1

1

There are two issues, I think. First:

  • What shambulator said: Your custom Fault class must be
    • a DataContract, and use
    • DataMember to mark all properties that shall be serialized.

If this is done, then WCF will

  • translate a service-side throw new FaultException<MyFault>(new MyFault { ... }); into a message which contains your fault, including the data; and
  • translate this fault on the client-side to a FaultException<MyFault>.

Second:

This client-side FaultException<> is generated by WCF. Maybe one can register a custom Exception-Translator, but I have never used it, never needed it, and not found such a thing after a one-minute Google search. I'd recommend just going with the FaultException plus custom Fault-type.

Community
  • 1
  • 1
gimpf
  • 4,503
  • 1
  • 27
  • 40
  • thanks. you right i search more the then a min i google and could find it, that why i post it. you maybe right i could use regular FaultException – kfir Aug 30 '12 at 05:24