简体   繁体   English

使用IErrorHandler接口进行WCF错误处理

[英]Wcf Error handling using IErrorHandler interface

I have a question regarding WCF fault exceptions. 我对WCF故障异常有疑问。 I am implementing the IErrorHandler interface and creating a FaultException . 我正在实现IErrorHandler接口并创建FaultException

My question is this: When am I creating a FaultException , is there a pattern that I must follow to send a friendly error message to the client? 我的问题是:在创建FaultException ,是否必须遵循一种模式才能向客户端发送友好的错误消息?

I log the actual exception using the HandleError method to the database and creating a FaultException in the ProvideFault method. 我使用HandleError方法将实际异常记录到数据库中,并在ProvideFault方法中创建FaultException

Given delow, there is my sample implementation of IErrorHandler interface. 考虑到不足,这里是我的IErrorHandler接口的示例实现。

public bool HandleError(Exception error)
{
    System.Diagnostics.Debug.WriteLine(error.Message);
            // Log the error to the Db  
    return true;
}

public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
        // Here is a sample that I was playing with. 
    MessageFault faultException;
    if(error is DataException)
        faultException = new FaultException<string>(ErrorStrings.DatabaseOpenError, new FaultReason(ErrorStrings.DatabaseOpenError)).CreateMessageFault();
    else if (error is NullReferenceException)
        faultException = new FaultException<string>(ErrorStrings.NullDataError, new FaultReason(ErrorStrings.NullDataError)).CreateMessageFault();
    else
        faultException = new FaultException<string>(ErrorStrings.GeneralError, new FaultReason(ErrorStrings.GeneralError)).CreateMessageFault();

    fault = Message.CreateMessage(version, faultException, "");
}

I need some clarity on what exactly has to be done inside ProvideFault method to return a friendly Fault to the user. 我需要弄清楚ProvideFault方法内部到底要做什么,才能向用户返回友好的Fault。 I am also planning to use localization to display errors. 我还计划使用本地化来显示错误。 Not sure if this will be done by the client or if service should send localized messages. 不确定是由客户端完成还是由服务发送本地化消息。

Based on what I read, I should not send information of the stack from the error or EntityFramework errors, like EntityValidation errors and so on, to the client due to security reasons. 基于我的阅读,由于安全原因,我不应该将错误或EntityFramework错误(例如EntityValidation错误等)中的堆栈信息发送给客户端。

In this case, will I need to check for the type of exception and mine the details to be more appropriate before sending to the client? 在这种情况下,在发送给客户端之前,我是否需要检查异常类型并挖掘更合适的详细信息?

The main requirement is to use the FaultContractAttribute so WCF understands the service contract being fulfilled. 主要要求是使用FaultContractAttribute,以便WCF了解正在履行的服务合同。 Please see http://msdn.microsoft.com/en-us/library/ms752208(v=vs.110).aspx 请参阅http://msdn.microsoft.com/en-us/library/ms752208(v=vs.110).aspx

WCF will then also make sure your WSDL reflects your fault contracts. 然后,WCF还将确保您的WSDL反映您的故障合同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM