Reputation: 1503
I am creating a WCF service and handling errors by throwing WebFaultException<MyResultClass>
. (MyResultClass) is a class in my project:
[DataContract]
public class MyResultClass
{
[DataMember(IsRequired = false)]
public int Code;
[DataMember(IsRequired = false)]
public string Description;
public MyResultClass()
{
}
}
The problem is that WCF service is not serializing this class.
Upvotes: 1
Views: 444
Reputation: 10851
First of all (as others have mentioned) you should use properties.
[DataContract]
public class MyResultClass
{
[DataMember]
public int Code { set; get; }
[DataMember]
public string Description { set; get; }
}
Furthermore, you need to specify the FaultContract
in your interface. Otherwise the client cannot know what to expect. Since WebFaultException
inherits from FaultException
you don't need to specify it.
[ServiceContract]
public interface IService:
{
[OperationContract]
[FaultContract(typeof(MyResultClass))]
void DoStuff();
}
And your implementaion:
public class Service : IService
{
public void DoStuff()
{
var detail = new MyResultClass
{
Code = 400,
Description = "foo"
};
throw new WebFaultException<MyResultClass>(detail, HttpStatusCode.BadRequest);
}
}
And your client:
try
{
// Call your WCF service here...
}
catch (FaultException<MyResultClass> e)
{
MyResultClass detail = e.Detail;
// Do stuff with detail
}
catch (Exception e)
{
// Some other error
}
Upvotes: 2
Reputation: 2764
please change your class to this and try:
[DataContract]
public class MyResultClass
{
[DataMember]
public int Code { set; get; }
[DataMember]
public string Description { set; get; }
}
Upvotes: 0