方法: インスタンス クエリ プロバイダーの開発

カスタム インスタンス クエリ プロバイダーを作成するには、次の手順に従います。

  1. Class Library プロジェクトを作成します。

  2. Microsoft.ApplicationServer.StoreManagement.dll への参照を追加します。さらに、このトピックで提供されているサンプル コードをコンパイルするために、System.Configuration.dll および System.Data.dll への参照を追加します。

  3. ソース ファイルの先頭に次のステートメントを追加します。

    using Microsoft.ApplicationServer.StoreManagement.Query;
    using System.Collections.Specialized; 
    using System.Data;
    using System.Data.SqlClient;
    
  4. InstanceQueryProvider クラスから派生するインスタンス クエリ プロバイダーのクラスを作成します。

        public sealed class MySqlInstanceQueryProvider : InstanceQueryProvider
        {
        }
    
  5. Initialize メソッドを実装します。このメソッドは、構成ファイルで指定された構成情報に対応するプロパティ バッグを受け付けます。このプロパティ バッグのデータはプロバイダーの作成に使用されます。Initialize メソッドは、CreateInstanceQuery メソッドまたは UniqueProviderIdentifier メソッドが呼び出される前に呼び出されます。

    ヒント

    リモートのシナリオでは、名前と値のコレクションに "EnableServiceModelMetadata" という名前の項目が含まれます。プロバイダーは、base.Initialize メソッドを呼び出す前に、このパラメーターを無視して削除するよう選択することができます。このプロパティは通常、Microsoft.Web.Administration.ServerManager オブジェクトについて SetMetadata("ServiceModel", true) を呼び出すかどうかを決定するために使用します。

    
            string storeName;
            string ConnectionString { get; set; }
    
            public override void Initialize(string name, NameValueCollection config)
            {
    
                this.storeName = name;
                this.ConnectionString= config["connectionString"];
    
                // Initialize the base class
                base.Initialize(name, config);
            }
    
  6. カスタム InstanceQuery オブジェクトを取得するために、InstanceQueryProvider クラスの CreateInstanceQuery メソッドを実装します。

            public override InstanceQuery CreateInstanceQuery()
            {
                SqlConnectionStringBuilder connectionStringBuilder = new SqlConnectionStringBuilder(this.ConnectionString);
                connectionStringBuilder.AsynchronousProcessing = true;
                return new MySqlInstanceQuery(this.storeName, connectionStringBuilder.ConnectionString);
            }
    

    ヒント

    MySqlInstanceQuery 型の実装については、次のセクションを参照してください。

  7. UniqueProviderIdentifier メソッドを実装します。このメソッドが返す一意のプロバイダー ID を使用して、異なるプロバイダー オブジェクトが、基になる同一のストアに解決されるかどうかを判断します。

            string UniqueStoreIdentifier { get; set; }
    
            public override string UniqueProviderIdentifier()
            {   
                this.UniqueStoreIdentifier = GetUniqueStoreIdentifier(this.ConnectionString); 
                return this.UniqueStoreIdentifier;
            }
    
            private string GetUniqueStoreIdentifier(string connectionString)
            {
                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    using (SqlCommand command = new SqlCommand())
                    {
                        command.CommandType = CommandType.Text;
                        command.CommandText = "SELECT TOP (1) [StoreIdentifier] FROM [Microsoft.ApplicationServer.DurableInstancing].[StoreVersion]";
                        command.Connection = connection;
    
                        command.Connection.Open();
    
                        Guid identifier = (Guid)command.ExecuteScalar();
                        return identifier.ToString();
                    }
                }
            }
    

InstanceQuery の実装

カスタム InstanceQuery 型を作成するには、次の手順に従います。

  1. InstanceQuery クラスから派生するクラスを作成します。

        public sealed class MySqlInstanceQuery : InstanceQuery
        {
            string storeName;
            string connectionString;
    
            public MySqlInstanceQuery(string storeName, string connectionString)
            {
               this.storeName = storeName;
                this.connectionString = connectionString;
            }
        }
    
  2. BeginExecuteQuery メソッドを実装します。クライアントはこのメソッドを使用してインスタンスをクエリします。

            public override IAsyncResult BeginExecuteQuery(InstanceQueryExecuteArgs args, TimeSpan timeout, AsyncCallback callback, object state)
            {
                //uncomment the following line to compile the project or implement the method
                //throw new NotImplementedException();
            }
    
  3. EndExecuteQuery メソッドを実装します。このメソッドは、InstanceInfo オブジェクトのコレクションを返します。

            public override IEnumerable<InstanceInfo> EndExecuteQuery(IAsyncResult result)
            {
                //uncomment the following line to compile the project or implement the method
                //throw new NotImplementedException();
            }
    
  4. BeginExecuteCount メソッドを実装します。クライアントはこのメソッドを使用してインスタンス数をクエリします。

            public override IAsyncResult BeginExecuteCount(InstanceQueryArgs args, TimeSpan timeout, AsyncCallback callback, object state)
            {
                //uncomment the following line to compile the project or implement the method
                //throw new NotImplementedException();
            }
    
  5. EndExecuteCount メソッドを実装します。このメソッドは、インスタンスの数を返します。

            public override int EndExecuteCount(IAsyncResult result)
            {
                //uncomment the following line to compile the project or implement the method
                //throw new NotImplementedException();
            }
    
  6. BeginExecuteGroupCount メソッドを実装します。クライアントはこのメソッドを使用して、インスタンス ストアに対してグループ化されているインスタンスの数をクエリします。

            public override IAsyncResult BeginExecuteGroupCount(InstanceQueryGroupArgs args, TimeSpan timeout, AsyncCallback callback, object state)
            {
                //uncomment the following line to compile the project or implement the method
                //throw new NotImplementedException();
            }
    
  7. EndExecuteGroupCount メソッドを実装します。このメソッドは、GroupingResult オブジェクトのコレクションを返します。

            public override IEnumerable<GroupingResult> EndExecuteGroupCount(IAsyncResult result)
            {
                //uncomment the following line to compile the project or implement the method
                //throw new NotImplementedException();
            }
    
  8. Cancel メソッドを実装します。クライアントはこのメソッドを呼び出して、既存の操作をキャンセルします。

            public override void Cancel(IAsyncResult result)
            {
                //uncomment the following line to compile the project or implement the method
                //throw new NotImplementedException();
            }
    

関連項目

概念

方法: インスタンス ストア プロバイダーの開発
方法: インスタンス コントロール プロバイダーの開発
方法: インスタンス ストア プロバイダー、インスタンス クエリ プロバイダー、およびインスタンス コントロール プロバイダーの構成
インスタンス ストア、クエリ、およびコントロール プロバイダー

  2012-03-05