How To: Develop an Instance Query Provider

The following procedure provides steps to create a custom instance query provider.

  1. Create a Class Library project.

  2. Add a reference to Microsoft.ApplicationServer.StoreManagement.dll. In addition, add references to System.Configuration.dll and System.Data.dll to compile the sample code provided in this topic.

  3. Add the following statement at the beginning of the source file.

    using Microsoft.ApplicationServer.StoreManagement.Query;
    using System.Collections.Specialized; 
    using System.Data;
    using System.Data.SqlClient;
    
  4. Create a class for the instance query provider deriving from the InstanceQueryProvider class.

        public sealed class MySqlInstanceQueryProvider : InstanceQueryProvider
        {
        }
    
  5. Implement the Initialize method. This method accepts a property bag that corresponds to configuration information specified in the configuration file. The data in this property bag is used to construct the provider. The Initialize method is called before the CreateInstanceQuery or UniqueProviderIdentifier method is called.

    Note

    In remoting scenarios, the name-value collection would contain an item named “EnableServiceModelMetadata”. The provider can choose to ignore and remove this parameter before invoking the base.Initialize method. This property is typically used to determine whether to invoke SetMetadata(“ServiceModel”, true) on the Microsoft.Web.Administration.ServerManager object.

    
            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. Implement the CreateInstanceQuery method of the InstanceQueryProvider class to return a custom InstanceQuery object.

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

    Note

    See the next section for the implementation of the MySqlInstanceQuery type.

  7. Implement the UniqueProviderIdentifier method. The unique provider ID that this method returns is used to determine whether different provider objects resolve to the same underlying store.

            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();
                    }
                }
            }
    

Implementing InstanceQuery

The following procedure provides steps to create a custom InstanceQuery type.

  1. Create a class deriving from the InstanceQuery class.

        public sealed class MySqlInstanceQuery : InstanceQuery
        {
            string storeName;
            string connectionString;
    
            public MySqlInstanceQuery(string storeName, string connectionString)
            {
               this.storeName = storeName;
                this.connectionString = connectionString;
            }
        }
    
  2. Implement the BeginExecuteQuery method. A client uses this method to query for instances.

            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. Implement the EndExecuteQuery method. This method should return a collection of InstanceInfo objects.

            public override IEnumerable<InstanceInfo> EndExecuteQuery(IAsyncResult result)
            {
                //uncomment the following line to compile the project or implement the method
                //throw new NotImplementedException();
            }
    
  4. Implement the BeginExecuteCount method. A client uses this method to query for an instance count.

            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. Implement the EndExecuteCount method. This method should return a count of instances.

            public override int EndExecuteCount(IAsyncResult result)
            {
                //uncomment the following line to compile the project or implement the method
                //throw new NotImplementedException();
            }
    
  6. Implement the BeginExecuteGroupCount method. A client uses this method to query for a grouped instance count against the instance store.

            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. Implement the EndExecuteGroupCount method. This method should return a collection of GroupingResult objects.

            public override IEnumerable<GroupingResult> EndExecuteGroupCount(IAsyncResult result)
            {
                //uncomment the following line to compile the project or implement the method
                //throw new NotImplementedException();
            }
    
  8. Implement the Cancel method. A client invokes this method to cancel the existing operation.

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

See Also

Concepts

How To: Develop an Instance Store Provider
How To: Develop an Instance Control Provider
How To: Configure Instance Store, Query, and Control Providers
Instance Store, Query, and Control Providers

  2012-09-12