WCF Tutorial
www.Learn2Expert.net A new ASP.Net MVC 4, SSIS, Interview Q/A tutorial - Visit - www.Learn2Expert.net
Skip Navigation LinksHomeDurable Service No of Views: 127577

Durable Service

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
        }
    }
        

Tips!

  • Always create the service with Interface->Implementation format, mention the contract in Interface.
  • Define the service in Class library and refer the class library in Host project. Don’t use service class in host project.
  • Change the instance mode to per call as default.
  • Always catch exception using try/catch block and throw exception using FaultException < T >.
  • Logging and Include exception should be enable while compiling the project in debug mode. While in production deployment disable the logging and Include exception details.