使用错误处理

在 Windows Communication Foundation (WCF) 错误处理过程中,除非引发 FaultException (或子类型)或实现 FaultContract ,否则不会将异常消息返回到客户端。 因此,只能在这些方案中跟踪来自故障消息本身的数据。 回调实现中的异常自动作为 ServerFaultClientFault 跟踪点的错误消息返回。 但是,它将始终返回带有通用消息的通用错误。 有关 WCF 错误协定的详细信息,请参阅 https://go.microsoft.com/fwlink/?LinkId=83132

还可以通过故障跟踪点跟踪常量和上下文属性。

如果使用 IncludeExceptionDetailInFaults 属性在错误中返回异常详细信息,则可以通过 XPath 提取实际的异常消息。

错误处理由 GetServiceContractCallPoint 中定义的故障跟踪点提供:

  • 服务错误

  • 客户端故障

  • CallbackFault

    使用这些故障跟踪点时,即使在基于事务的方案中运行,故障数据也会始终保留。 事务完整性在所有非故障跟踪的数据上得以维持,而一旦发生故障,非故障跟踪的数据就会被回滚。

注释

这些跟踪点应用于回复路径,仅适用于 GetServiceContractCallPoint 提供的 ServiceReply、ClientReply 和 CallbackReply 服务协定调用点。

错误配置示例

以下示例演示当服务在 AuthorizationServiceFault 事件中抛出 FaultException 时,如何跟踪 ServiceFault 上的异常消息;否则,则跟踪在 AuthorizationServiceReply 事件中由操作返回的布尔表达式。 AuthorizationServiceReply OnEvent 或 AuthorizationServiceFault OnEvent 将被持久化。

注释

此示例的实现演示 ServiceReply 和 ServiceFault 跟踪点的相互排他性。

<ic:OnEvent IsBegin ="true" IsEnd="false" Name="AuthorizationServiceReply" Source="ESCreditCardService">

  <ic:Filter>
    <ic:Expression>
      <wcf:Operation Name="GetServiceContractCallPoint"/>
      <ic:Operation Name ="Constant">
        <ic:Argument>ServiceReply</ic:Argument>
      </ic:Operation>
      <ic:Operation Name ="Equals"/>
      <wcf:Operation Name="GetOperationName" />
      <ic:Operation Name="Constant">
        <ic:Argument>AuthorizeWithDataContract</ic:Argument>
      </ic:Operation>
      <ic:Operation Name ="Equals" />
      <ic:Operation Name ="And" />
    </ic:Expression>
  </ic:Filter>

  <ic:CorrelationID>
    <ic:Expression>
      <wcf:Operation Name="AutoGenerateCorrelationToken"/>
    </ic:Expression>
  </ic:CorrelationID>

  <ic:Update DataItemName="Status" Type="NVARCHAR">
    <ic:Expression>
      <ic:Operation Name="Constant">
        <ic:Argument>Success</ic:Argument>
      </ic:Operation>
    </ic:Expression>
  </ic:Update>

  <ic:Update DataItemName="Result" Type="NVARCHAR">
    <ic:Expression>
      <wcf:Operation Name ="XPath">
        <wcf:Argument>//s:Body/ccservice:*/ccservice:AuthorizeWithDataContractResult</wcf:Argument>
      </wcf:Operation>
    </ic:Expression>
  </ic:Update>

  <ic:Update DataItemName ="Service Call Date" Type ="DATETIME">
    <ic:Expression>
      <wcf:Operation Name ="GetContextProperty">
        <wcf:Argument>EventTime</wcf:Argument>
      </wcf:Operation>
    </ic:Expression>
  </ic:Update>

</ic:OnEvent>

<ic:OnEvent IsBegin ="true" IsEnd="false" Name="AuthorizationServiceFault" Source="ESCreditCardService">

  <ic:Filter>
    <ic:Expression>
      <wcf:Operation Name="GetServiceContractCallPoint"/>
      <ic:Operation Name ="Constant">
        <ic:Argument>ServiceFault</ic:Argument>
      </ic:Operation>
      <ic:Operation Name ="Equals"/>
      <wcf:Operation Name="GetOperationName" />
      <ic:Operation Name="Constant">
        <ic:Argument>AuthorizeWithDataContract</ic:Argument>
      </ic:Operation>
      <ic:Operation Name ="Equals" />
      <ic:Operation Name ="And" />
    </ic:Expression>
  </ic:Filter>

  <ic:CorrelationID>
    <ic:Expression>
      <wcf:Operation Name="AutoGenerateCorrelationToken"/>
    </ic:Expression>
  </ic:CorrelationID>

  <ic:Update DataItemName="Status" Type="NVARCHAR">
    <ic:Expression>
      <ic:Operation Name="Constant">
        <ic:Argument>Fault</ic:Argument>
      </ic:Operation>
    </ic:Expression>
  </ic:Update>

      <ic:Update DataItemName="Source" Type="NVARCHAR">
        <ic:Expression>
          <wcf:Operation Name ="XPath">
            <wcf:Argument>//s:Body/Fault/Reason/Text</wcf:Argument>
          </wcf:Operation>
        </ic:Expression>
      </ic:Update>

  <ic:Update DataItemName ="Service Call Date" Type ="DATETIME">
    <ic:Expression>
      <wcf:Operation Name ="GetContextProperty">
        <wcf:Argument>EventTime</wcf:Argument>
      </wcf:Operation>
    </ic:Expression>
  </ic:Update>

</ic:OnEvent>

另请参阅

配置 WCF 适配器以截获 BAM 数据