【问题标题】:WCF HTTP/SOAP Webservice - Returning exception message in faultsWCF HTTP/SOAP Webservice - 在错误中返回异常消息
【发布时间】:2011-05-16 12:52:18
【问题描述】:

我有一个简单的 WCF HTTP/SOAP Web 服务,服务实现看起来像这样:

public CustomResponse DoSomething(CustomRequest request)
{
    try
    {
        return InternalGubbins.WithErrorHandling.ProcessRequest(request);
    }
    catch
    {
        // Some sort of error occurred that is not gracefully
        // handled elsewhere in the framework
        throw new SoapException("Hmmm, it would seem that the cogs are meshed!", SoapException.ServerFaultCode);
    }
}

现在,如果抛出 SoapException,我希望将异常消息(即Hmmm, it would seem that the cogs are meshed!)返回给调用客户端,而不需要任何额外的异常细节(即堆栈跟踪)。

如果我将 includeExceptionDetailInFaults 设置为 true(服务器上的 web.config),那么带有堆栈跟踪等的完整异常将返回给客户端。但是,如果我将其设置为 false,我会收到一条通用消息:

服务器无法处理 由于内部错误而请求。为了 有关错误的更多信息, 要么打开 IncludeExceptionDetailInFaults(要么 来自 ServiceBehaviorAttribute 或来自 配置 行为)在服务器上,以便 将异常信息发回给 客户端,或根据 Microsoft .NET Framework 3.0 SDK 文档并检查服务器 跟踪日志。

所以问题是,我怎样才能将我的 SoapException 消息返回给调用客户端?即:

<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
    <s:Header>
        <a:Action s:mustUnderstand="1">http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher/fault</a:Action>
        <a:RelatesTo>urn:uuid:185719f4-6113-4126-b956-7290be375342</a:RelatesTo>
    </s:Header>
    <s:Body>
        <s:Fault>
            <s:Code>
                <s:Value>s:Receiver</s:Value>
                <s:Subcode>
                    <s:Value xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher">a:InternalServiceFault</s:Value>
                </s:Subcode>
            </s:Code>
            <s:Reason>
                <s:Text xml:lang="en-GB">Hmmm, it would seem that the cogs are meshed!</s:Text>
            </s:Reason>
        </s:Fault>
    </s:Body>
</s:Envelope>

【问题讨论】:

    标签: wcf custom-errors soapexception


    【解决方案1】:

    我认为您需要在操作上声明一个 FaultContract 并使用 FaultException(SoapException 是 WCF 之前的)。如果它们不属于服务合同的一部分,我相信 WCF 不会将故障发送回客户端。我从未尝试过 SoapException,但肯定抛出一个 FaultException 对我来说一直都很好。

    [ServiceContract()]    
    public interface ISomeService
    {
         [OperationContract]
         [FaultContract(typeof(MyFault))]
         CustomResponse DoSomething(CustomRequest request)
    }
    
    public class SomeService
    {
        public CustomResponse DoSomething(CustomRequest request)
        {
            ...
            throw new FaultException<MyFault>(new MyFault());
        }
    }
    

    【讨论】:

    • 这很好用,也回答了下一个问题,即在肥皂异常中使用自定义细节。干杯
    【解决方案2】:

    如果您不想定义自定义异常类型,那么试试这个

    try    
    {        
        return InternalGubbins.WithErrorHandling.ProcessRequest(request);    
    }    
    catch    
    {
        throw new FaultException("Hmmm, it would seem that the cogs are meshed.");    
    }
    

    这样做会向客户端发送以下响应

    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
      <s:Header />
      <s:Body>
        <s:Fault>
          <faultcode>s:Client</faultcode>
          <faultstring xml:lang="en-US">Hmmm, it would seem that the cogs are meshed.</faultstring>
        </s:Fault>
      </s:Body>
    </s:Envelope>
    

    【讨论】:

    • 这似乎工作得很好,但是...我正在使用 Microsoft 服务跟踪查看器来查看服务请求的跟踪。由于某种原因,使用 FaultException 时,跟踪查看器不起作用。
    猜你喜欢
    • 1970-01-01
    • 2017-07-01
    • 2011-08-20
    • 1970-01-01
    • 2019-04-25
    • 1970-01-01
    • 1970-01-01
    • 2011-09-09
    • 2012-11-24
    相关资源
    最近更新 更多