ServiceBehaviorAttribute.IncludeExceptionDetailInFaults 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置一个值,该值指定是否要将常规未处理执行异常转换为 FaultException<TDetail> 类型的 ExceptionDetail 并将其作为错误消息发送。 仅在开发阶段将其设置为 true 以便对服务进行故障排除。
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
属性值
如果未经处理的异常将作为 SOAP 错误返回,则为 true;否则为 false。 默认值为 false。
示例
下面的代码示例演示了 ServiceBehaviorAttribute 属性。 BehaviorService 类使用 ServiceBehaviorAttribute 属性以指示:
在 UI 线程上调用实现方法。
每个会话都有一个服务对象。
服务是单线程服务,不支持可重入调用。
而且,在操作级别,OperationBehaviorAttribute 值指示 TxWork 方法将自动登记到流事务中或创建新事务来做这项工作,并指示如果没有发生未经处理的异常,则自动提交此事务。
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
为了正确执行下面的代码示例,基础绑定必须支持流事务。 例如,若要使用 WSHttpBinding 支持流事务,请在代码或应用程序配置文件中将 TransactionFlow 属性设置为 true。 下面的代码示例演示上述示例的配置文件。
<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>
注解
将 IncludeExceptionDetailInFaults 设置为 true,可以使异常信息流入客户端,以便进行调试。 此属性需要支持请求响应或双工消息传递的绑定。
在所有托管应用程序中,处理错误由 Exception 对象表示。 在基于 SOAP 的应用程序(如 WCF 应用程序)中,实现服务操作的方法使用 SOAP 错误消息传达错误信息。 由于 WCF 应用程序在两种类型的错误系统下执行,因此需要发送到客户端的任何托管异常信息都必须从异常转换为 SOAP 错误。 有关详细信息,请参阅在协定和服务中指定和处理错误。
在开发期间,可能需要服务将其他异常发送回客户端,以协助调试。 此功能仅用于开发,不应在已部署的服务中使用。
若要方便调试开发,请将 IncludeExceptionDetailInFaults 代码中的设置为 true 或使用应用程序配置文件。
当启用时,服务会自动将较安全的异常信息返回到调用方。 这些错误在客户端显示为 FaultException<TDetail> 类型的 ExceptionDetail 对象。
重要
true设置为IncludeExceptionDetailInFaults使客户端能够获取有关内部服务方法异常的信息;建议将其作为暂时调试服务应用程序的方式。 此外,以这种方式返回未处理的托管异常的方法的 WSDL 并不包含类型为 FaultException<TDetail> 的 ExceptionDetail 的协定。 客户端必须预见到发生未知 SOAP 错误的可能性,以便正确获取调试信息。
将此属性设置为 true 也可以使用应用程序配置文件和 <serviceDebug> 元素来完成,如下面的代码示例所示。
<serviceBehaviors>
<behavior name="metadataAndDebugEnabled">
<serviceDebug
includeExceptionDetailInFaults="true"
/>
<serviceMetadata
httpGetEnabled="true"
httpGetUrl=""
/>
</behavior>
</serviceBehaviors>