ServiceBehaviorAttribute.IncludeExceptionDetailInFaults Egenskap
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Hämtar eller anger ett värde som anger att allmänna ohanterade körningsfel ska konverteras till en FaultException<TDetail> typ ExceptionDetail och skickas som ett felmeddelande. Ange detta till true endast under utveckling för att felsöka en tjänst.
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
Egenskapsvärde
trueom ohanterade undantag ska returneras som SOAP-fel; annars . false Standardvärdet är false.
Exempel
I följande kodexempel visas ServiceBehaviorAttribute egenskaperna. Klassen BehaviorService använder attributet ServiceBehaviorAttribute för att indikera att:
Implementeringsmetoder anropas i användargränssnittstråden.
Det finns ett tjänstobjekt för varje session.
Tjänsten är enkeltrådad och stöder inte återaktiveringsanrop.
På åtgärdsnivå OperationBehaviorAttribute anger dessutom värdena att TxWork metoden automatiskt registrerar flödestransaktioner eller skapar en ny transaktion för att utföra arbetet, och att transaktionen utförs automatiskt om ett ohanterat undantag inte inträffar.
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
Den underliggande bindningen måste ha stöd för flödestransaktioner för att följande kodexempel ska kunna köras korrekt. Om du vill stödja flödade transaktioner med hjälp av WSHttpBindinganger du TransactionFlow till exempel egenskapen i true kod eller i en programkonfigurationsfil. Följande kodexempel visar konfigurationsfilen för föregående exempel.
<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>
Kommentarer
Ange IncludeExceptionDetailInFaults till true för att aktivera undantagsinformation för att flöda till klienter i felsökningssyfte. Den här egenskapen kräver en bindning som stöder antingen begärandesvar eller duplex-meddelanden.
I alla hanterade program representeras bearbetningsfel av Exception objekt. I SOAP-baserade program, till exempel WCF-program, kommunicerar metoder som implementerar tjänståtgärder felinformation med hjälp av SOAP-felmeddelanden. Eftersom WCF-program körs under båda typerna av felsystem måste all hanterad undantagsinformation som måste skickas till klienten konverteras från undantag till SOAP-fel. Mer information finns i Ange och hantera fel i kontrakt och tjänster.
Under utvecklingen kanske du vill att tjänsten även ska skicka tillbaka andra undantag till klienten för att hjälpa dig att felsöka. Det här är en funktion endast för utveckling och bör inte användas i distribuerade tjänster.
För att underlätta felsökningsutveckling IncludeExceptionDetailInFaults anger du till true antingen i kod eller med hjälp av en programkonfigurationsfil.
När den är aktiverad returnerar tjänsten automatiskt säkrare undantagsinformation till anroparen. Dessa fel visas för klienten som FaultException<TDetail> objekt av typen ExceptionDetail.
Important
true Inställningen IncludeExceptionDetailInFaults gör det möjligt för klienter att hämta information om interna tjänstmetodsfel. Det rekommenderas endast som ett sätt att tillfälligt felsöka ett tjänstprogram. Dessutom innehåller WSDL för en metod som returnerar ohanterade undantag på det här sättet inte kontraktet för FaultException<TDetail> av typen ExceptionDetail. Klienter måste förvänta sig att risken för ett okänt SOAP-fel hämtar felsökningsinformationen korrekt.
Du kan också ställa in den här egenskapen på true med hjälp av en programkonfigurationsfil och <serviceDebug-elementet> , vilket visas i följande kodexempel.
<serviceBehaviors>
<behavior name="metadataAndDebugEnabled">
<serviceDebug
includeExceptionDetailInFaults="true"
/>
<serviceMetadata
httpGetEnabled="true"
httpGetUrl=""
/>
</behavior>
</serviceBehaviors>