更新 : 2007 年 11 月
サービス コントラクトの実装の内部実行動作を指定します。
名前空間 :
System.ServiceModel
アセンブリ :
System.ServiceModel (System.ServiceModel.dll 内)
<AttributeUsageAttribute(AttributeTargets.Class)> _
Public NotInheritable Class ServiceBehaviorAttribute _
Inherits Attribute _
Implements IServiceBehavior
Dim instance As ServiceBehaviorAttribute
[AttributeUsageAttribute(AttributeTargets.Class)]
public sealed class ServiceBehaviorAttribute : Attribute,
IServiceBehavior
[AttributeUsageAttribute(AttributeTargets::Class)]
public ref class ServiceBehaviorAttribute sealed : public Attribute,
IServiceBehavior
/** @attribute AttributeUsageAttribute(AttributeTargets.Class) */
public final class ServiceBehaviorAttribute extends Attribute implements IServiceBehavior
public final class ServiceBehaviorAttribute extends Attribute implements IServiceBehavior
ServiceBehaviorAttribute 属性をサービスの実装に適用して、サービス全体の実行動作を指定します (メソッド レベルの実行動作を指定するには、OperationBehaviorAttribute 属性を使用します)。この属性を適用できるのはサービスの実装だけです。実施例については、「サービス : 動作サンプル」を参照してください。
ServiceBehaviorAttribute プロパティは、共通機能を有効にする Windows Communication Foundation (WCF) プログラミング モデルの機能で、このプロパティがない場合は、開発者がこの共通機能を実装する必要があります。これらの動作、その他の動作の詳細については、「サービスのランタイム動作の指定」を参照してください。次に示すいくつかのプロパティが設定される基になるランタイム プロパティの詳細については、「ServiceHost とサービス モデル レイヤの拡張」を参照してください。
IncludeExceptionDetailInFaults プロパティは、アプリケーション構成ファイルを使用して設定することもできます。詳細については、IncludeExceptionDetailInFaults のトピックを参照してください。
次のコード例は、ServiceBehaviorAttribute プロパティを示しています。BehaviorService クラスは、次のことを示すために ServiceBehaviorAttribute 属性を使用します。
サービス オブジェクトは、トランザクションの完了時にリサイクルされます。
セッションごとに 1 つのサービス オブジェクトが存在します。
サービスはシングル スレッドであり、再入呼び出しをサポートしません。
さらに、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."
);
}
}
}
次のコード例が適切に動作するには、基になるバインディングでフロー トランザクションがサポートされている必要があります。たとえば、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"
/>
</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>
System..::.Object
System..::.Attribute
System.ServiceModel..::.ServiceBehaviorAttribute
この型のすべてのパブリック static (Visual Basic では Shared) メンバは、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。
Windows Vista, Windows XP SP2, Windows Server 2003
.NET Framework および .NET Compact Framework では、各プラットフォームのすべてのバージョンはサポートしていません。サポートされているバージョンについては、「.NET Framework システム要件」を参照してください。
.NET Framework
サポート対象 : 3.5、3.0
参照
その他の技術情報