ServiceBehaviorAttribute.IncludeExceptionDetailInFaults Eigenschap

Definitie

Hiermee wordt een waarde opgehaald of ingesteld die aangeeft dat algemene niet-verwerkte uitvoeringsonderzondering moet worden geconverteerd naar een FaultException<TDetail> type ExceptionDetail en als een foutbericht moet worden verzonden. Stel deze optie alleen in op true tijdens de ontwikkeling om problemen met een service op te lossen.

public:
 property bool IncludeExceptionDetailInFaults { bool get(); void set(bool value); };
public bool IncludeExceptionDetailInFaults { get; set; }
member this.IncludeExceptionDetailInFaults : bool with get, set
Public Property IncludeExceptionDetailInFaults As Boolean

Waarde van eigenschap

true als niet-verwerkte uitzonderingen moeten worden geretourneerd als SOAP-fouten; anders, false. De standaardwaarde is false.

Voorbeelden

In het volgende codevoorbeeld ziet u de ServiceBehaviorAttribute eigenschappen. De BehaviorService klasse gebruikt het ServiceBehaviorAttribute kenmerk om aan te geven dat:

  • Implementatiemethoden worden aangeroepen op de UI-thread.

  • Er is één serviceobject voor elke sessie.

  • De service is één threaded en biedt geen ondersteuning voor nieuwe aanroepen.

Bovendien geven de OperationBehaviorAttribute waarden op bewerkingsniveau aan dat de TxWork methode automatisch wordt opgenomen in gestroomde transacties of dat er een nieuwe transactie wordt gemaakt om het werk uit te voeren en dat de transactie automatisch wordt doorgevoerd als er geen onverwerkte uitzondering optreedt.

using System;
using System.ServiceModel;
using System.Transactions;

namespace Microsoft.WCF.Documentation
{
  [ServiceContract(
    Namespace="http://microsoft.wcf.documentation",
    SessionMode=SessionMode.Required
  )]
  public interface IBehaviorService
  {
    [OperationContract]
    string TxWork(string message);
  }

  // Note: To use the TransactionIsolationLevel property, you
  // must add a reference to the System.Transactions.dll assembly.
  /* The following service implementation:
   *   -- Processes messages on one thread at a time
   *   -- Creates one service object per session
   *   -- Releases the service object when the transaction commits
   */
  [ServiceBehavior(
    ConcurrencyMode=ConcurrencyMode.Single,
    InstanceContextMode=InstanceContextMode.PerSession,
    ReleaseServiceInstanceOnTransactionComplete=true
  )]
  public class BehaviorService : IBehaviorService, IDisposable
  {
    Guid myID;

    public BehaviorService()
    {
      myID = Guid.NewGuid();
      Console.WriteLine(
        "Object "
        + myID.ToString()
        + " created.");
    }

    /*
     * The following operation-level behaviors are specified:
     *   -- The executing transaction is committed when
     *        the operation completes without an
     *        unhandled exception
     *   -- Always executes under a flowed transaction.
     */
    [OperationBehavior(
      TransactionAutoComplete = true,
      TransactionScopeRequired = true
    )]
    [TransactionFlow(TransactionFlowOption.Mandatory)]
    public string TxWork(string message)
    {
      // Do some transactable work.
      Console.WriteLine("TxWork called with: " + message);
      // Display transaction information.

      TransactionInformation info = Transaction.Current.TransactionInformation;
      Console.WriteLine("The distributed tx ID: {0}.", info.DistributedIdentifier);
      Console.WriteLine("The tx status: {0}.", info.Status);
      return String.Format("Hello. This was object {0}.",myID.ToString()) ;
    }

    public void Dispose()
    {
      Console.WriteLine(
        "Service "
        + myID.ToString()
        + " is being recycled."
      );
    }
  }
}
Imports System.ServiceModel
Imports System.Transactions

Namespace Microsoft.WCF.Documentation
  <ServiceContract(Namespace:="http://microsoft.wcf.documentation", SessionMode:=SessionMode.Required)> _
  Public Interface IBehaviorService
    <OperationContract> _
    Function TxWork(ByVal message As String) As String
  End Interface

  ' Note: To use the TransactionIsolationLevel property, you 
  ' must add a reference to the System.Transactions.dll assembly.
'   The following service implementation:
'   *   -- Processes messages on one thread at a time
'   *   -- Creates one service object per session
'   *   -- Releases the service object when the transaction commits
'   
    <ServiceBehavior(ConcurrencyMode:=ConcurrencyMode.Single, InstanceContextMode:=InstanceContextMode.PerSession, _
                     ReleaseServiceInstanceOnTransactionComplete:=True)> _
    Public Class BehaviorService
        Implements IBehaviorService, IDisposable
        Private myID As Guid

        Public Sub New()
            myID = Guid.NewGuid()
            Console.WriteLine("Object " & myID.ToString() & " created.")
        End Sub

        '    
        '     * The following operation-level behaviors are specified:
        '     *   -- The executing transaction is committed when
        '     *        the operation completes without an 
        '     *        unhandled exception
        '     *   -- Always executes under a flowed transaction.
        '     
        <OperationBehavior(TransactionAutoComplete:=True, TransactionScopeRequired:=True), TransactionFlow(TransactionFlowOption.Mandatory)> _
        Public Function TxWork(ByVal message As String) As String Implements IBehaviorService.TxWork
            ' Do some transactable work.
            Console.WriteLine("TxWork called with: " & message)
            ' Display transaction information.

            Dim info As TransactionInformation = Transaction.Current.TransactionInformation
            Console.WriteLine("The distributed tx ID: {0}.", info.DistributedIdentifier)
            Console.WriteLine("The tx status: {0}.", info.Status)
            Return String.Format("Hello. This was object {0}.", myID.ToString())
        End Function

        Public Sub Dispose() Implements IDisposable.Dispose
            Console.WriteLine("Service " & myID.ToString() & " is being recycled.")
        End Sub
    End Class
End Namespace

De onderliggende binding moet stroomtransacties ondersteunen voor het volgende codevoorbeeld om correct te kunnen worden uitgevoerd. Als u stroomtransacties wilt ondersteunen met behulp van bijvoorbeeld de WSHttpBindingTransactionFlow eigenschap true in code of in een toepassingsconfiguratiebestand. In het volgende codevoorbeeld ziet u het configuratiebestand voor het voorgaande voorbeeld.

<configuration>
  <system.serviceModel>
    <services>
      <service  
        name="Microsoft.WCF.Documentation.BehaviorService" 
        behaviorConfiguration="metadataAndDebugEnabled"
      >
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/SampleService"/>
          </baseAddresses>
        </host>
        <!--
          Note:
            This example code uses the WSHttpBinding to support transactions using the 
            WS-AtomicTransactions (WS-AT) protocol. WSHttpBinding is configured to use the  
            protocol, but the protocol is not enabled on some computers. Use the xws_reg -wsat+ 
            command to enable the WS-AtomicTransactions protocol in the MSDTC service.          
          -->
        <endpoint 
           contract="Microsoft.WCF.Documentation.IBehaviorService"
           binding="wsHttpBinding"
           bindingConfiguration="wsHttpBindingWithTXFlow"
           address="http://localhost:8080/BehaviorService"
          />
        <endpoint 
           contract="Microsoft.WCF.Documentation.IBehaviorService"
           binding="netTcpBinding"
           bindingConfiguration="netTcpBindingWithTXFlow"
           address="net.tcp://localhost:8081/BehaviorService"
          />
        <endpoint
          address="mex"
          binding="mexHttpBinding"
          contract="IMetadataExchange"
        />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="metadataAndDebugEnabled">
          <serviceDebug
            includeExceptionDetailInFaults="true"
          />
          <serviceMetadata
            httpGetEnabled="true"
            httpGetUrl=""
          />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <!-- binding configuration - configures a WSHttpBinding to require transaction flow -->
    <bindings>
      <wsHttpBinding>
        <binding name="wsHttpBindingWithTXFlow" transactionFlow="true" />
      </wsHttpBinding>
      <netTcpBinding>
        <binding name="netTcpBindingWithTXFlow" transactionFlow="true" />
      </netTcpBinding>
    </bindings>
  </system.serviceModel>
</configuration>

Opmerkingen

Ingesteld IncludeExceptionDetailInFaults om true uitzonderingsinformatie naar clients te laten stromen voor foutopsporingsdoeleinden. Voor deze eigenschap is een binding vereist die ondersteuning biedt voor aanvraag-antwoord- of duplexberichten.

In alle beheerde toepassingen worden verwerkingsfouten weergegeven door Exception objecten. In SOAP-toepassingen, zoals WCF-toepassingen, communiceren methoden die servicebewerkingen implementeren foutinformatie met behulp van SOAP-foutberichten. Omdat WCF-toepassingen worden uitgevoerd onder beide typen foutsystemen, moeten alle beheerde uitzonderingsgegevens die naar de client moeten worden verzonden, worden geconverteerd van uitzonderingen naar SOAP-fouten. Zie Opgeven en afhandelen van fouten in contracten en services voor meer informatie.

Tijdens de ontwikkeling wilt u mogelijk dat uw service ook andere uitzonderingen naar de client verzendt om u te helpen bij het opsporen van fouten. Dit is een functie voor alleen ontwikkeling en mag niet worden gebruikt in geïmplementeerde services.

Als u de ontwikkeling van foutopsporing wilt vergemakkelijken, stelt u het in IncludeExceptionDetailInFaultstrue op in code of met behulp van een toepassingsconfiguratiebestand.

Wanneer deze functie is ingeschakeld, retourneert de service automatisch veiligere uitzonderingsgegevens naar de beller. Deze fouten worden aan de client weergegeven als FaultException<TDetail> objecten van het type ExceptionDetail.

Important

Instelling IncludeExceptionDetailInFaults om clients in staat te true stellen informatie te verkrijgen over uitzonderingen voor interne servicemethoden. Het wordt alleen aanbevolen om tijdelijk fouten in een servicetoepassing op te sporen. Daarnaast bevat de WSDL voor een methode die op deze manier niet-verwerkte beheerde uitzonderingen retourneert, niet het contract voor het FaultException<TDetail> van type ExceptionDetail. Clients moeten de mogelijkheid van een onbekende SOAP-fout verwachten om de foutopsporingsgegevens correct te verkrijgen.

U kunt deze eigenschap true ook instellen met behulp van een toepassingsconfiguratiebestand en het <element serviceDebug> , zoals in het volgende codevoorbeeld wordt weergegeven.

<serviceBehaviors>
  <behavior name="metadataAndDebugEnabled">
    <serviceDebug
      includeExceptionDetailInFaults="true"
    />
    <serviceMetadata
      httpGetEnabled="true"
      httpGetUrl=""
    />
  </behavior>
</serviceBehaviors>

Van toepassing op