Share via


How To: Develop an Instance Store Provider

The following procedure has steps to create a custom instance store provider. For general information about instance store, query, and control providers, see Instance Store, Query, and Control Providers.

  1. Create a class library project.

  2. Add reference to Microsoft.ApplicationServer.StoreProvider.dll, System.Activities.DurableInstancing.dll, and System.Runtime.DurableInstancing.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 using statements at the beginning of the source file.

    using Microsoft.ApplicationServer.StoreProvider;
    using System.Runtime.DurableInstancing;
    using System.Activities.DurableInstancing;
    
    
    //for the sample code provided in the methods
    using System.Collections.Specialized; 
    using System.Data;
    using System.Data.SqlClient;
    
  4. Create a class for the instance store provider deriving from the InstanceStoreProvider class.

        public sealed class MySqlWorkflowInstanceStoreProvider : InstanceStoreProvider
        {
        }
    
  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 CreateInstanceStore or UniqueProviderIdentifier method is called.

    
            string ConnectionString { get; set; }
            public override void Initialize(string name, NameValueCollection config)
            {
                this.ConnectionString= config["connectionString"];
    
                // Initialize the base class
                base.Initialize(name, config);
            }
    
  6. Implement the CreateInstanceStore method of the InstanceStoreProvider class to return a System.Runtime.DurableInstancing.InstanceStore object, which the client will use to execute commands against the instance store.

    
            public override InstanceStore CreateInstanceStore()
            {
                // the following line creates an instance of the SqlWorkflowInstanceStore class
                    SqlWorkflowInstanceStore store = new SqlWorkflowInstanceStore(this.ConnectionString);
                return store;
            }
    
  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();
                    }
                }
            }
    

The Workflow Management Service (WMS) or a workflow host invokes this method on the instance store provider to obtain a store-specific InstanceStore object. AppFabric includes an instance store provider for the SQL Workflow Instance Store, which creates a SqlWorkflowInstanceStore object when the CreateInstanceStore method is called.

Important

See How To: Register Custom Providers for information about registering the provider with Windows Server AppFabric.