WCF Error Handling without fault contract

两盒软妹~` 提交于 2019-12-23 12:49:29

问题


I have a WCF service which all operations return type is OperationStatus:

[DataContract]
public abstract class ServiceResponse
{
    [DataMember]
    public bool Success { get; set; }

    [DataMember]
    public String StatusReason { get; set; }

}

I'd like to create an ErrorHandler to catch all exceptions and then I'd like it to return to the client instance of class ServiceReponse with property Success set to false and StatusReason set to "INTERNAL SERROR".

As for now I have my own class implementing IErrorHandler but i don't wanna use FaultContract - i just want to return to the client regular object of type StatusReason. Can this be done?


回答1:


You do need a fault contract, in order to send over a Reason, try this:

   MyException fault = new MyException("Error message");
   throw new FaultException<MyException>(fault, new FaultReason("Reason Text"));



回答2:


Here is a nice article about handling exceptions in WCF




回答3:


You could add

 <serviceDebug includeExceptionDetailInFaults="true"/>

To your service behaviors section but it isn't really the best for production environment.



来源:https://stackoverflow.com/questions/1810039/wcf-error-handling-without-fault-contract

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!