ServiceBehaviorAttribute.IncludeExceptionDetailInFaults Właściwość

Definicja

Pobiera lub ustawia wartość określającą, że ogólne nieobsługiwane wyjątki wykonywania mają być konwertowane na FaultException<TDetail> typ ExceptionDetail i wysyłane jako komunikat o błędzie. Ustaw tę wartość true tylko podczas programowania, aby rozwiązać problemy z usługą.

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

Wartość właściwości

Boolean

true jeśli nieobsługiwane wyjątki mają zostać zwrócone jako błędy protokołu SOAP; w przeciwnym razie , false. Wartość domyślna to false.

Przykłady

Poniższy przykład kodu przedstawia ServiceBehaviorAttribute właściwości. Klasa BehaviorService używa atrybutu ServiceBehaviorAttribute , aby wskazać, że:

  • Metody implementacji są wywoływane w wątku interfejsu użytkownika.

  • Dla każdej sesji istnieje jeden obiekt usługi.

  • Usługa jest jednowątkowa i nie obsługuje wywołań reentrant.

Ponadto na poziomie operacji wartości wskazują, OperationBehaviorAttribute że TxWork metoda automatycznie zarejestrowała się w przepływanych transakcjach lub tworzy nową transakcję do wykonania tej pracy i że transakcja jest zatwierdzana automatycznie, jeśli nie wystąpi nieobsługiwany wyjątek.

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

Powiązanie bazowe musi obsługiwać transakcje przepływane, aby w celu poprawnego wykonania poniższego przykładu kodu. Aby obsługiwać przepływy transakcji przy użyciu elementu WSHttpBinding, na przykład ustaw TransactionFlow właściwość na true w kodzie lub w pliku konfiguracji aplikacji. Poniższy przykład kodu przedstawia plik konfiguracji dla poprzedniego przykładu.

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

Uwagi

Ustaw IncludeExceptionDetailInFaults wartość , aby true umożliwić przepływ informacji o wyjątkach do klientów na potrzeby debugowania. Ta właściwość wymaga powiązania, które obsługuje wysyłanie komunikatów dwukierunkowych lub żądań.

We wszystkich zarządzanych aplikacjach błędy przetwarzania są reprezentowane przez Exception obiekty. W aplikacjach opartych na protokole SOAP, takich jak aplikacje WCF, metody implementujące operacje usługi komunikują informacje o błędach przy użyciu komunikatów o błędach protokołu SOAP. Ponieważ aplikacje WCF są wykonywane w obu typach systemów błędów, wszelkie informacje o wyjątkach zarządzanych, które należy wysłać do klienta, muszą zostać przekonwertowane z wyjątków na błędy protokołu SOAP. Aby uzyskać więcej informacji, zobacz Określanie i obsługa błędów w umowach i usługach.

Podczas programowania możesz chcieć, aby usługa wysyłała również inne wyjątki do klienta, aby ułatwić debugowanie. Jest to funkcja tylko do programowania i nie powinna być stosowana w wdrożonych usługach.

Aby ułatwić programowanie debugowania, ustaw wartość IncludeExceptionDetailInFaults true na w kodzie lub przy użyciu pliku konfiguracji aplikacji.

Po włączeniu usługa automatycznie zwraca bezpieczniejsze informacje o wyjątku do wywołującego. Te błędy są wyświetlane klientowi jako FaultException<TDetail> obiekty typu ExceptionDetail.

Ważne

Ustawienie IncludeExceptionDetailInFaults w celu true umożliwienia klientom uzyskiwania informacji o wyjątkach metody usługi wewnętrznej. Jest to zalecane tylko jako sposób tymczasowego debugowania aplikacji usługi. Ponadto język WSDL dla metody zwracającej nieobsługiwane wyjątki zarządzane w ten sposób nie zawiera kontraktu ExceptionDetailtypu FaultException<TDetail> . Klienci muszą oczekiwać możliwości poprawnego uzyskania informacji o debugowaniu przez nieznany błąd protokołu SOAP.

Ustawienie tej właściwości można również wykonać true przy użyciu pliku konfiguracji aplikacji i <elementu serviceDebug> , jak pokazano w poniższym przykładzie kodu.

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

Dotyczy