まず(他の人が言及したように)プロパティを使用する必要があります。
[DataContract]
public class MyResultClass
{
[DataMember]
public int Code { set; get; }
[DataMember]
public string Description { set; get; }
}
さらに、あなたはあなたのインターフェイスでFaultContract
を指定する必要があります。さもなければ、クライアントは何を期待するかを知ることができません。 WebFaultException
はFaultException
から継承しているので、指定する必要はありません。
[ServiceContract]
public interface IService:
{
[OperationContract]
[FaultContract(typeof(MyResultClass))]
void DoStuff();
}
そして、あなたのimplementaion:
public class Service : IService
{
public void DoStuff()
{
var detail = new MyResultClass
{
Code = 400,
Description = "foo"
};
throw new WebFaultException<MyResultClass>(detail, HttpStatusCode.BadRequest);
}
}
そして、あなたのクライアント:
try
{
// Call your WCF service here...
}
catch (FaultException<MyResultClass> e)
{
MyResultClass detail = e.Detail;
// Do stuff with detail
}
catch (Exception e)
{
// Some other error
}
あなたは[DataMemberを]ジュースを試しましたか?プロパティで試してください https://msdn.microsoft.com/en-us/library/ms733127.aspx – PiotrKowalski
それは動作していません – Ala