WCF EntLib Validation - change default SOAP Fault

2019-06-14 02:04发布

问题:

I'm using the Enterprise Library Validation Application Block for my WCF service. All is fine, and .Net consumers can catch the FaultException<ValidationFault> exception to get a collection of human-readable business errors. However, it doesn't look quite as great for non-.Net consumers, especially those that are going to be looking at the raw SOAP message. The SOAP Reason text is always "The creator of this fault did not specify a Reason." This isn't very helpful, as there is a reason, it's specified under the <Detail> element, as shown in the example Fault message below.

Is there any way to change the text "The creator of this fault did not specify a Reason." to something more helpful like "See ValidationFault Details"?

<s:Body>
   <s:Fault>
      <s:Code>
         <s:Value>s:Sender</s:Value>
      </s:Code>
      <s:Reason>
         <s:Text xml:lang="en-GB">The creator of this fault did not specify a Reason.</s:Text>
      </s:Reason>
      <s:Detail>
         <ValidationFault xmlns="http://www.microsoft.com/practices/EnterpriseLibrary/2007/01/wcf/validation" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
            <Details xmlns:b="http://schemas.datacontract.org/2004/07/Microsoft.Practices.EnterpriseLibrary.Validation.Integration.WCF">
               <b:ValidationDetail>
                  <b:Key i:nil="true"/>
                  <b:Message>Value Validator</b:Message>
                  <b:Tag>request</b:Tag>
               </b:ValidationDetail>
            </Details>
         </ValidationFault>
      </s:Detail>
   </s:Fault>
</s:Body>

回答1:

Well, it seems like the EntLib people didn't think of this one. I've noted where the change in the EntLib code needs to be and raised an issue at their CodePlex site. I guess this could also be done by anyone as part of the EntLibContrib project, but they seem to still be on Enterprise Library 3.1, whereas I'm using 4.1.

I guess if anyone's desperate, the solution would be to download the EntLib source code, and modify the BeforeCall method in the ValidationParameterInspector class (in the Microsoft.Practices.EnterpriseLibrary.Validation.Integration.WCF namespace). This is where the FaultException is created. An overload to this constructor can specify the FaultReason.



回答2:

How is your WCF service generating these faults?

When you look at the FaultException class in WCF, there are numerous ways you can construct one of those - including some constructors which allow you to specify a FaultReason for the SOAP fault.

Marc



标签: wcf soapfault