クリックして評価とフィードバックをお寄せください
MSDN
MSDN ライブラリ
.NET 開発
.NET Framework 3.5
.NET Framework 3.5
System.ServiceModel 名前空間
ServiceBehaviorAttribute クラス
 InstanceContextMode プロパティ

  低帯域幅での表示をオンにする
このページは次のバージョンについて記述しています。
Microsoft Visual Studio 2008/.NET Framework 3.5

その他のバージョンについては、以下の情報を参照してください。
.NET Framework クラス ライブラリ
ServiceBehaviorAttribute..::.InstanceContextMode プロパティ

更新 : 2007 年 11 月

新しいサービス オブジェクトが作成されるかどうかを示す値を取得または設定します。

名前空間 :  System.ServiceModel
アセンブリ :  System.ServiceModel (System.ServiceModel.dll 内)
Visual Basic (宣言)
Public Property InstanceContextMode As InstanceContextMode
Visual Basic (使用法)
Dim instance As ServiceBehaviorAttribute
Dim value As InstanceContextMode

value = instance.InstanceContextMode

instance.InstanceContextMode = value
C#
public InstanceContextMode InstanceContextMode { get; set; }
Visual C++
public:
property InstanceContextMode InstanceContextMode {
    InstanceContextMode get ();
    void set (InstanceContextMode value);
}
J#
/** @property */
public InstanceContextMode get_InstanceContextMode()
/** @property */
public  void set_InstanceContextMode(InstanceContextMode value)
JScript
public function get InstanceContextMode () : InstanceContextMode
public function set InstanceContextMode (value : InstanceContextMode)

プロパティ値

型 : System.ServiceModel..::.InstanceContextMode
InstanceContextMode 値のいずれか。既定値は PerSession です。
例外条件
ArgumentOutOfRangeException

値が、InstanceContextMode 値ではありません。

InstanceContextMode プロパティを使用して、新しいサービス オブジェクトがいつ作成されるかを指定します。サービス オブジェクトは通信チャネルに直接的には接続されないため、サービス オブジェクトの有効期間は、クライアント アプリケーションとサービス アプリケーション間のチャネルの有効期間に依存しません。既定値の PerSession は、クライアント アプリケーションとサービス アプリケーションの間で新しい通信セッションが確立されたときに、新しいサービス オブジェクトを作成するようにサービス アプリケーションに指示します。同じセッションでの後続の呼び出しは、同じオブジェクトによって処理されます。

PerSession は、各サービス オブジェクトが、1 つのクライアント チャネルからの要求を処理することを示します。

ms575892.alert_note(ja-jp,VS.90).gifメモ :

InstanceContextMode プロパティは、他のいくつかの設定と相互関係があります。たとえば、InstanceContextMode 値を Single に設定した場合は、ConcurrencyMode 値を Multiple に設定しない限り、サービスが一度に処理できるのは 1 つのメッセージだけです。このプロパティと ServiceContractAttribute..::.SessionMode プロパティを組み合わせて、特定の動作を設定することもできます。詳細については、「セッション、インスタンス化、および同時実行」を参照してください。

シングルトン有効期間動作 (ホスト アプリケーションが ServiceHost コンストラクタを呼び出し、サービスとして使用するオブジェクトを渡す場合など) では、サービス クラスで InstanceContextModeSingle に設定する必要があります。これを行わなかった場合は、実行時に例外がスローされます。

次のコード例は、ServiceBehaviorAttribute プロパティを示しています。BehaviorService クラスは、次のことを示すために ServiceBehaviorAttribute 属性を使用します。

  • 実装メソッドは、UI スレッド上で起動します。

  • セッションごとに 1 つのサービス オブジェクトが存在します。

  • サービスはシングル スレッドであり、再入呼び出しをサポートしません。

さらに、OperationBehaviorAttribute 値は操作レベルで、TxWork メソッドがフロー トランザクションに自動的に登録するかまたはこの処理のための新しいトランザクションを作成するかどうか、および未処理の例外が発生しない場合にトランザクションが自動的にコミットされるかどうかを示します。

C#
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
コミュニティ コンテンツ   コミュニティ コンテンツとは
新しいコンテンツの追加 RSS  注釈
Processing
© 2009 Microsoft Corporation. All rights reserved. 使用条件  |  商標  |  プライバシー
Page view tracker