Thursday, June 27, 2013

Durable Service in wcf

Durable services are WCF services that persist service state information even after service host is restarted or Client. It means that durable services have the capability to restore their own state when they are recycled. It can use data store like SQL database for maintain instance state. It is new feature in .Net 3.5
You might think that we can also maintain session using WCF sessions, but content in the session environment is not persisted by default. If the service is shut down or client closes the proxy, data will be lost. But in case of Durable service it is still maintained.

Working:

When Durable service is created with database as data store, it will maintain all its state information in the table.
When a client make a request to the service, instance of the service is serialized, a new GUID is generated. This serialized instance xml and key will be saved in the database. We will call this GUID as instanceID. Service will send the instanceID to the client, so later it can use this id to get the instance state back. Even when client is shut down, instanceId will be saved at the client side. So when ever client opening the proxy, it can get back the previous state.

Defining the Durable Service

Durable service can be implemented using [DurableService()] attribute. It takes 'CanCreateInstance' and 'CompletesInstance' property to mention on which operation instance state has to be saved and destroyed.
  • CanCreateInstance = true: Calling this operation results in creating the serialization and inserting it into the datastore.
  • CompletesInstance = true: Calling this operation results in deleting the persisted instance from the datastore.
    [Serializable]
    [DurableService()]
    public class MyService :IMyservice
    {
        [DurableOperation(CanCreateInstance = true)]
        public int StartPersistance()
        {
            //Do Something
        }

     [DurableOperation(CompletesInstance = true)]
        public void EndPersistence()
        {
            //Do Something
        }
    }

1 comment: