1

Im getting a
Type 'WcfServiceLibrary1.GetDataErrorException' cannot be ISerializable and have DataContractAttribute attribute.

When trying to add a custom exception in a WCF service.

 [ServiceContract]
public interface IService1 {

    [OperationContract]
    [FaultContract(typeof(GetDataErrorException))]
    string GetData(int value);
}

public class Service1 : IService1 {
    public string GetData(int value) {
        if (value.Equals(0))
            throw new FaultException<GetDataError>(new GetDataError(), new FaultReason("Zero"));

        return string.Format("You entered: {0}", value);
    }
}

[DataContract]
public class GetDataError {
    public GetDataError() { }
}

[DataContract]
public class GetDataErrorException : FaultException<GetDataError> {
    public GetDataErrorException(string message) :
        base(new GetDataError(), new FaultReason(message)) { }
}

Why does this not work? Im missing something simple I guess...

hub
  • 165
  • 3
  • 13
  • Does FaultException already implement ISerializable? And would they then be subject to different serialisers? – brumScouse Apr 16 '14 at 12:57
  • FaultException inherits from Exception which is marked as Serializable. I think this is an app domain related design decision. could be wrong. – brumScouse Apr 16 '14 at 13:01

2 Answers2

1

You cannot and should not have both. as the That's evident from the exception you got. The framework sees an ambiguous state when it sees 2 serializers. for more info refer to the following blog

http://blogs.msdn.com/b/sowmy/archive/2006/05/14/597476.aspx

your Fault Contract should look like

public class DataErrorException : FaultException<GetDataError> 
{
    public DataErrorException(string message) :
            base(new GetDataError(), new FaultReason(message)) { }
}

Note that the only change here is that I have removed the DataContract attribute

Dhawalk
  • 1,257
  • 13
  • 28
1

I used the example in msdn faultexception example and that worked for me.

hub
  • 165
  • 3
  • 13