Introduction
In this article we will learn about durable services and how it can be implemented in Windows Communication Foundation (WCF). I will also demonstrate steps to create a durable service in WCF along with the source code. WCF durable service concept was only introduced in .NET Framework 3.5 version.
After reading through this article you should be having a good idea on WCF durable services, how it works and how to build it.
What is a Durable Service in Windows Communication Foundation (WCF)?
Generally web applications, web services and WCF services are stateless. In other words one can say HTTP is a stateless protocol. The word stateless here denotes that a state of the object in WCF service will not be persisted between multiple requests to the service. WCF durable service paves way for building long running applications with a persisted state. WCF durable service helps in persisting the state of the service instance between multiple requests or even if the service/client application is restarted.
This state persistence is done by XML serializing the instance of the server object and storing them in the database with a GUID as a unique key for the record. This GUID will be sent to the service by the client so that the service would retrieve the corresponding instance from the database for that request. Hence the state is persisted. WCF durable service also provides an option to remove the instance from the database.
How it is Different From Sessions in WCF?
As a reader you will be definitely dubious about the fact that what makes the durable service state persistence different from the session state persistence in WCF using the InstanceContextMode.PerSession
option. The answer is simple, session state persistence in WCF
will only be effective till the client or WCF service
is restarted. Once the service or client is restarted the persisted state will be lost as the storage of session is in-memory. On the other hand with WCF durable service state persistence is not based on the user context rather it is based on the GUID passed by the client. Here the state will be persisted even if the client or WCF service is restarted since the instance is stored in the database.
Implementing WCF Durable Services: Prerequisites
In this section we will look at the things need to be done or to be noted while developing a WCF durable service. Check out the below points.
- Need to have SQLServer express installed on the machine and create a database to store the serialized instance in it.
- Go to C:WINDOWSMicrosoft.NETFrameworkv3.5SQLEN path on your file system; fetch the SQL scripts from the files SqlPersistenceProviderLogic.sql and SqlPersistenceProviderSchema.sql. Here is a brief description for the file contents.
- SqlPersistenceProviderSchema.sql: This file contains the script for creating the tables and required schema for storing the object instance.
- SqlPersistenceProviderLogic.sql: This file contains the stored procedure script for creating the serialized instance in the database, retrieving the instance and deleting the instance.
Sample Application
In this section we will create a sample WCF durable service
. I will also be providing the description along with the source code.
- As a first step create a solution named
WCFDurableServices
solution - Add a
WCF
Service Application to the solution and name it asWCFDurableService
. - Add a Console Application to the solution and name it as
ClientApplication
.
WCF Service
Let us now implement the durable WCF service. Add a ServiceContract
named IAccountingService
. Below is the source code:
namespace WcfDurableService { [ServiceContract] public interface IAccountingService { [OperationContract] void PersistInstance(); [OperationContract] void AddAmount(long amount); [OperationContract] long GetTotalAmount(); [OperationContract] void ClearInstance(); } }
There is nothing different with the service contract for developing durable service as you can witness in the above code.
Let us now create a .svc
file named AccountingService.svc
. Inherit the AccountingService
class in AccountingService.svc.cs
file and implement the same. Below is the code:
namespace WcfDurableService { [Serializable] //Attribute require to mark this implementation as a durable service [DurableService] public class AccountingService : IAccountingService { private long _totalAmount; #region IAccountingService Members //Attribute to decorate the service methods [DurableOperation(CanCreateInstance=true)] public void PersistInstance() { } [DurableOperation] public void AddAmount(long amount) { _totalAmount += amount; } [DurableOperation] public long GetTotalAmount() { return _totalAmount; } [DurableOperation(CompletesInstance=true)] public void ClearInstance() { } #endregion } }
In the above code you will notice the Serializable
attribute. It is required since the instance of this implementation class will be XML serialized and stored in the database. Also you could see there are two new attributes decorating the implementation class and the service method.
- DurableService: To decorate the implementation class of the durable service.
- DurableOperation: To decorate the service method of the durable services. This attribute allows specifying two Boolean properties.
- CanCreateInstance: If this property is set to true then the respective method will create and store the instance in the database when called by the client.
- CompletesInstance: If this property is set to true then the respective method will delete the instance in the database when called by the client.
DurableService
and DurableOperation
attribute will be available only after adding the reference to System.WorkflowServices
is added to the WCF service application.