Friday 14 October 2011

How to Create Durable Service( step 2 to 5)

Step 2: Create interface and decorate with Service and Operation contract.
    [ServiceContract()]
    public interface ISimpleCalculator
    {
        [OperationContract]
        int Add(int num);

        [OperationContract]
        int Subtract(int num);

        [OperationContract]
        int Multiply(int num);

        [OperationContract]
        void EndPersistence();
    }
 
Step 3: You need to add [Serializable] And [DurableService()] attribute to the service implementation. Set CanCreateInstance = true property to the operation in which instance state has to be persisted and set CompletesInstance = true when state has to be destroyed. In this implementation, we are going to persist the 'currentValue' variable value to the database.
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Description;
    [Serializable]
    [DurableService()]
    public class SimpleCalculator :ISimpleCalculator 
    {
        int currentValue = default(int);
        [DurableOperation(CanCreateInstance = true)]
        public int Add(int num)
        {
            return (currentValue += num);
        }
        [DurableOperation()]
        public int Subtract(int num)
        {
            return (currentValue -= num);
        }
        [DurableOperation()]
        public int Multiply(int num)
        {
            return (currentValue *= num);
        }
        [DurableOperation(CompletesInstance = true)]
        public void EndPersistence()
        {
        }
Step 4: Before configuring the database information in the durable service, you need to set up DataStore environment. Microsoft provides inbuilt sqlPersistance provider. To set up the database environment, run the these sql query located at following location 'C:\Windows\Microsoft.NET\Framework\v3.5\SQL\EN'
  • SqlPersistenceProviderSchema.sql
  • SqlPersistenceProviderLogic.sql
Step 5: In order to support durable service, you need to use Context binding type. <persistenceProvider> tag is used to configure the persistence provider.
<system.serviceModel>
 <services>
  <service name="SimpleCalculator" behaviorConfiguration="ServiceBehavior">
  <!-- Service Endpoints -->
  <endpoint address="" binding="wsHttpContextBinding" 
  bindingConfiguration="browConfig" contract="ISimpleCalculator">
  <identity>
  <dns value="localhost"/>
  </identity>
  </endpoint>
  <endpoint address="mex" binding="mexHttpBinding" 
  contract="IMetadataExchange"/>
  </service>
 </services>
 <behaviors>
  <serviceBehaviors>
  <behavior name="ServiceBehavior">
  <serviceMetadata httpGetEnabled="true"/>
 <serviceDebug includeExceptionDetailInFaults="true"/>
    <persistenceProvider  
     type="System.ServiceModel.Persistence.SqlPersistenceProviderFactory,
        System.WorkflowServices, Version=3.5.0.0, Culture=neutral,
         PublicKeyToken=31bf3856ad364e35" connectionStringName="DurableServiceStore" 
                               persistenceOperationTimeout="00:00:10"
                               lockTimeout="00:01:00"
                               serializeAsText="true"/>
   </behavior>
   </serviceBehaviors>
  </behaviors>
    <bindings>
      <wsHttpContextBinding >
        <binding name="browConfig" >
          <security mode="None"></security>
        </binding>
      </wsHttpContextBinding>
    </bindings>
</system.serviceModel>
<connectionStrings>
<add name="DurableServiceStore" 
connectionString="Data Source=saravanakumar;Initial Catalog
=DurableServiceStore;Integrated Security=True"/>
</connectionStrings>

No comments:

Post a Comment