更新 : 2007 年 11 月
新しいサービス オブジェクトが作成されるかどうかを示す値を取得または設定します。
名前空間 :
System.ServiceModel
アセンブリ :
System.ServiceModel (System.ServiceModel.dll 内)
Public Property InstanceContextMode As InstanceContextMode
Dim instance As ServiceBehaviorAttribute
Dim value As InstanceContextMode
value = instance.InstanceContextMode
instance.InstanceContextMode = value
public InstanceContextMode InstanceContextMode { get; set; }
public:
property InstanceContextMode InstanceContextMode {
InstanceContextMode get ();
void set (InstanceContextMode value);
}
/** @property */
public InstanceContextMode get_InstanceContextMode()
/** @property */
public void set_InstanceContextMode(InstanceContextMode value)
public function get InstanceContextMode () : InstanceContextMode
public function set InstanceContextMode (value : InstanceContextMode)
InstanceContextMode プロパティを使用して、新しいサービス オブジェクトがいつ作成されるかを指定します。サービス オブジェクトは通信チャネルに直接的には接続されないため、サービス オブジェクトの有効期間は、クライアント アプリケーションとサービス アプリケーション間のチャネルの有効期間に依存しません。既定値の PerSession は、クライアント アプリケーションとサービス アプリケーションの間で新しい通信セッションが確立されたときに、新しいサービス オブジェクトを作成するようにサービス アプリケーションに指示します。同じセッションでの後続の呼び出しは、同じオブジェクトによって処理されます。
PerSession は、各サービス オブジェクトが、1 つのクライアント チャネルからの要求を処理することを示します。
シングルトン有効期間動作 (ホスト アプリケーションが ServiceHost コンストラクタを呼び出し、サービスとして使用するオブジェクトを渡す場合など) では、サービス クラスで InstanceContextMode を Single に設定する必要があります。これを行わなかった場合は、実行時に例外がスローされます。
次のコード例は、ServiceBehaviorAttribute プロパティを示しています。BehaviorService クラスは、次のことを示すために ServiceBehaviorAttribute 属性を使用します。
さらに、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>
- 直前の呼び出し元に対する完全な信頼。このメンバは、部分的に信頼されているコードから使用することはできません。詳細については、「部分信頼コードからのライブラリの使用」を参照してください。
Windows Vista, Windows XP SP2, Windows Server 2003
.NET Framework および .NET Compact Framework では、各プラットフォームのすべてのバージョンはサポートしていません。サポートされているバージョンについては、「.NET Framework システム要件」を参照してください。
.NET Framework
サポート対象 : 3.5、3.0
参照