简体   繁体   English

WCF故障异常不会在客户端上显示soap服务的详细信息

[英]WCF fault exception doesn't surface detail on client for soap service

We've got REST and SOAP endpoints for our service so we use WebFaultException to pass friendly messages. 我们为服务提供了REST和SOAP端点,因此我们使用WebFaultException传递友好消息。 This works great for the REST calls not so much for the SOAP calls. 这对于REST调用非常有用,而不是SOAP调用。 Below is the trace which clearly shows the friendly message in the "detail" element. 下面是跟踪,清楚地显示了“detail”元素中的友好消息。 But the FaultException that is raised on the client has the http status code description in the message - not the real message thrown from the service. 但是在客户端上引发的FaultException在消息中具有http状态代码描述 - 而不是从服务引发的真实消息。 Is there any way to surface the intended message on the client? 有没有办法在客户端上显示预期的消息?

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Header></s:Header>
   <s:Body>
      <s:Fault>
         <faultcode xmlns:a="http://schemas.microsoft.com/2009/WebFault" xmlns="">a:BadRequest</faultcode>
         <faultstring xml:lang="en-US" xmlns="">Bad Request</faultstring>
         <detail xmlns="">
            <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">Country code must be 3 characters.</string>
         </detail>
      </s:Fault>
   </s:Body>
</s:Envelope>

Also, this is in .net 4.0 and we are using Castle's WCF facility (DefaultServiceModel and RestServiceModel). 此外,这是在.net 4.0中,我们正在使用Castle的WCF工具(DefaultServiceModel和RestServiceModel)。

WCF will - by default and by design - not report back detailed error info for security reasons. 出于安全原因,WCF将 - 默认情况下并按设计 - 不报告详细的错误信息。 It basically will only tell you "something went wrong on the server - bad luck". 它基本上只会告诉你“服务器出了问题 - 运气不好”。

You can - for development and testing purposes - enable more detailed error info, but you should turn that off for production. 您可以 - 出于开发和测试目的 - 启用更详细的错误信息,但您应该将其关闭以进行生产。

To enable that, use a service behavior on your server: 要启用它,请在服务器上使用服务行为:

<system.serviceModel>
   <behaviors>
      <serviceBehaviors>
         <behavior name="DetailedDebugging">
             <serviceDebug includeExceptionDetailInFaults="True" />
         </behavior>
      </serviceBehaviors>
   </behaviors>

   <services>
      <service name="YourService"
               behaviorConfiguration="DetailedDebugging" >
          ....
      </service>
   </services>
</system.serviceModel>

Now your service should report back the detailed SOAP fault including all details, all the way back to your client app. 现在,您的服务应该报告详细的SOAP错误,包括所有详细信息,一直回到您的客户端应用程序。

Update: if I remember correctly, when handling a standard (non-typed) FaultException , you have easy access to stuff like the FaultCode and FaultReason etc., but the message details are a bit cumbersome to get it - try something like this: 更新:如果我没记错的话,在处理标准(非类型) FaultException ,你可以轻松访问诸如FaultCodeFaultReason类的东西,但是获取它的消息细节有点麻烦 - 尝试这样的事情:

try
{
   // your service call here
}
catch(FaultException fe)
{
   FaultCode fc = fe.Code;
   FaultReason fr = fe.Reason;

   MessageFault mf = fe.CreateMessageFault();
   if(mf.HasDetail)
   {
      string detailedMessage = mf.GetDetail<string>();
   }
}

Does that give you access to the detailed description of your SOAP fault?? 这是否允许您访问SOAP错误的详细描述?

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

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