Friday, March 9, 2012

WCF Durable Services quick and indepth overview

I love sessions in WCF . InstanceContextMode.PerSession so simple! But we know that session ends when the Server or the client restarts. That is when durable services makes an entry (from 3.5 onwards).

What is the deal? session ID is stored in disk/database?
Nope not just session id. But half marks for the answer because database/disk file is used.

Are you telling me that the whole server instance is serialised and placed in a data store?
Yes!!

Here is the explanation: After receiving a response from the service, the client will use the InstanceId(service instance guid) value on each subsequent operation invocation so that the client associates itself with the correct service instance.

So there needs to be some connectionstring info in config naturally:
<behaviors>
      <serviceBehaviors>
        <behavior>
          <persistenceProvider type=""
                               connectionStringName=""
                               persistenceOperationTimeout = "00:00:10"
                               lockTimeout="00:01:00"
                               serializeAsText="true"/>

eg: for type="System.ServiceModel.Persistence.SqlPersistenceProviderFactory, System.WorkflowServices, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

A couple of downloadable sql statements makes the database ready for Durable Services.
  • SqlPersistenceProviderSchema.sql
  • SqlPersistenceProviderLogic.sql
Let us talk about coding.
Now that serialisation is used the service class should have a [Serializable] attribute. Changes in service class are as follows(not interface/contract)

1) persistenceProvider behavior element(already discussed)
2) [Serializable] attribute
3) [DurableService] attribute: specifies that state information for your WCF service can be persisted to a persistence store, such as a database or file.
4) [DurableOperation] attribute: specifies that the state will be saved after the operation has completed.
  • [DurableOperation(CanCreateInstance = true)]  :Indicates whether a new service instance can be created.
  • [DurableOperation(CompletesInstance = true)]  :Indicates whether the service instance will be unloaded from memory and deleted from persistence once the operation has finished executing.
Few attributes and easy to learn!

CanCreateInstance property is a bit tricky so let us spend a few seconds on that.
  • CanCreateInstane=true can work only if it is an activation message(messages without an attached instance ID)
  • If this property has default value(false) then no 'new' instance will be created. But can persist to an existing service instance entry in the persistence store.
  • if your contract disallows sessions (System.ServiceModel.SessionMode.NotAllowed) , every DurableOperationAttribute attribute must have this property(CanCreateInstance) set to true.
  • If the contract allows sessions, then all operations with CanCreateInstance = true cannot be one way(IsOneWay=true).
Finally modify the endpoint configuration settings to reference a context binding, such as wsHttpContextBinding.
<endpoint address ="" binding="wsHttpContextBinding" contract="SimpleDurableService.IService1" />
This is necessary because a DurableOperationContext is used by the client to identify a specific service instance.
-------------[The End]------Comment your thoughts please

No comments:

Post a Comment