Bagikan melalui


ServiceBehaviorAttribute.IncludeExceptionDetailInFaults Properti

Definisi

Mendapatkan atau menetapkan nilai yang menentukan bahwa pengecualian eksekusi umum yang tidak tertangani akan dikonversi menjadi FaultException<TDetail> jenis ExceptionDetail dan dikirim sebagai pesan kesalahan. Atur ini ke true hanya selama pengembangan untuk memecahkan masalah layanan.

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

Nilai Properti

true jika pengecualian yang tidak tertangani akan dikembalikan sebagai kesalahan SOAP; jika tidak, false. Defaultnya adalah false.

Contoh

Contoh kode berikut menunjukkan ServiceBehaviorAttribute properti. Kelas BehaviorService menggunakan ServiceBehaviorAttribute atribut untuk menunjukkan bahwa:

  • Metode implementasi dipanggil pada utas UI.

  • Ada satu objek layanan untuk setiap sesi.

  • Layanan ini berutas tunggal dan tidak mendukung panggilan masuk kembali.

Selain itu, pada tingkat operasi, OperationBehaviorAttribute nilai menunjukkan bahwa TxWork metode secara otomatis mendaftar dalam transaksi yang mengalir atau membuat transaksi baru untuk melakukan pekerjaan, dan bahwa transaksi dilakukan secara otomatis jika pengecualian yang tidak tertangani tidak terjadi.

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

Pengikatan yang mendasar harus mendukung transaksi yang mengalir agar contoh kode berikut dapat dijalankan dengan benar. Untuk mendukung transaksi yang mengalir menggunakan WSHttpBinding, misalnya, atur TransactionFlow properti ke true dalam kode atau dalam file konfigurasi aplikasi. Contoh kode berikut menunjukkan file konfigurasi untuk sampel sebelumnya.

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

Keterangan

Atur IncludeExceptionDetailInFaults ke true untuk mengaktifkan informasi pengecualian agar mengalir ke klien untuk tujuan penelusuran kesalahan. Properti ini memerlukan pengikatan yang mendukung permintaan-respons atau pesan dupleks.

Di semua aplikasi terkelola, kesalahan pemrosesan diwakili oleh Exception objek. Dalam aplikasi berbasis SOAP seperti aplikasi WCF, metode yang menerapkan operasi layanan mengomunikasikan informasi kesalahan menggunakan pesan kesalahan SOAP. Karena aplikasi WCF dijalankan di bawah kedua jenis sistem kesalahan, informasi pengecualian terkelola apa pun yang perlu dikirim ke klien harus dikonversi dari pengecualian menjadi kesalahan SOAP. Untuk informasi selengkapnya, lihat Menentukan dan Menangani Kesalahan dalam Kontrak dan Layanan.

Selama pengembangan, Anda mungkin ingin layanan Anda juga mengirim pengecualian lain kembali ke klien untuk membantu Anda dalam penelusuran kesalahan. Ini adalah fitur khusus pengembangan dan tidak boleh digunakan dalam layanan yang disebarkan.

Untuk memfasilitasi pengembangan penelusuran kesalahan, atur IncludeExceptionDetailInFaults ke true dalam kode atau menggunakan file konfigurasi aplikasi.

Saat diaktifkan, layanan secara otomatis mengembalikan informasi pengecualian yang lebih aman ke pemanggil. Kesalahan ini muncul kepada klien sebagai FaultException<TDetail> objek jenis ExceptionDetail.

Penting

Pengaturan IncludeExceptionDetailInFaults untuk true memungkinkan klien mendapatkan informasi tentang pengecualian metode layanan internal; hanya disarankan sebagai cara untuk men-debug sementara aplikasi layanan. Selain itu, WSDL untuk suatu metode yang mengembalikan pengecualian terkelola yang tidak tertangani dengan cara ini tidak mencakup kontrak untuk FaultException<TDetail> dari tipe ExceptionDetail. Klien harus mengharapkan kemungkinan kesalahan SOAP yang tidak diketahui untuk mendapatkan informasi penelusuran kesalahan dengan benar.

Mengatur properti ini juga true dapat dilakukan menggunakan file konfigurasi aplikasi dan <elemen serviceDebug> , seperti yang ditunjukkan oleh contoh kode berikut.

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

Berlaku untuk