1. DataContract - new class FaultMessage
[DataContract]
public class FaultMessage
{
[DataMember]
public string ErrorCode { get; set; }
[DataMember]
public string Message { get; set; }
}
2. WCF Service
try
{
return XXX;
}catch(Exception ex)
{
FaultMessage faultMessage = new FaultMessage();
faultMessage.ErrorCode = "ErrorCode ";
faultMessage.Message = ex.Message;
throw new FaultException<FaultMessage>(faultMessage);
}
3. ServiceContract -- add [FaultContract(typeof(FaultMessage))]
[ServiceContract]
public interface IGameContract
{
[OperationContract]
[FaultContract(typeof(FaultMessage))]
IList<DungeonInfo> GetDungeonList();
[OperationContract]
[FaultContract(typeof(FaultMessage))]
...
}
4.UI
try
{
Service1.ServiceClient client = new Service1.ServiceClient()
XXXX
}
catch (FaultException<Service1.FaultMessage> ex)
{
LabelMsg.Text = string.Format("ErrorCode:{0}\n Message:{1}", ex.Detail.ErrorCode, ex.Detail.Message);
}