WCF HTTP/SOAP Web 服务 - 在错误中返回异常消息

发布于 2024-11-07 04:33:19 字数 2056 浏览 4 评论 0原文

我有一个简单的 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,我想要异常消息(即 嗯,看起来齿轮是啮合的!)返回到调用客户端,没有任何额外的异常详细信息(即堆栈跟踪)。

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

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

所以问题是,如何将 SoapException 消息返回给调用客户端? IE:

<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>

I have asimple WCF HTTP/SOAP web service, the service implementation looks something like this:

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);
    }
}

Now, if that SoapException is thrown I would like the exception message (i.e. Hmmm, it would seem that the cogs are meshed!) to be returned to the invoking client without any additional exception detail (i.e. stack traces).

If I set includeExceptionDetailInFaults to true (web.config on the server) then the full exception, with stack traces etc, is return to the client. However if I set it to false, I get a generic message:

The server was unable to process the
request due to an internal error. For
more information about the error,
either turn on
IncludeExceptionDetailInFaults (either
from ServiceBehaviorAttribute or from
the configuration
behavior) on the server in order to
send the exception information back to
the client, or turn on tracing as per
the Microsoft .NET Framework 3.0 SDK
documentation and inspect the server
trace logs.

So the question is, how can I get my SoapException message back to the invoking client? i.e:

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

从﹋此江山别 2024-11-14 04:33:19

我认为您需要在操作上声明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());
    }
}

I think you need to declare a FaultContract on the operation and use FaultException (SoapException is pre WCF). I believe WCF does not send faults back to the client if they are not part of the service contract. I have never tried SoapException but certainly throwing a FaultException has always worked fine for me.

[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());
    }
}
谈情不如逗狗 2024-11-14 04:33:19

如果您不想定义自定义异常类型,请尝试这样

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>

If you don't want to define a custom exception type then try this

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

Doing so would send the following response to the client

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文