Create Durable Service In C#.NET

Step 1: Start Visual Studio and click File->New->Web Site. Select the 'WCF Service' and give name Durableservice
 
Step 2: Rename Service.svc to SimpleCalculator.svc and also change Service.cs to SimpleCalculator.cs and IService.cs to ISimpleCalculator.cs
 
Step 3: Go to SimpleCalculator.svc and change the code as given below: 
  1. <%@ ServiceHost Language="C#" Debug="true" Service="SimpleCalculator" CodeBehind="~/App_Code/SimpleCalculator.cs" %>  
Step 4: Go to ISimpleCalculator.cs and add the below code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Serialization;  
  5. using System.ServiceModel;  
  6. using System.ServiceModel.Web;  
  7. using System.Text;  
  8. [ServiceContract]  
  9. public interface ISimpleCalculator  
  10. {  
  11. [OperationContract]  
  12. int Add(int num);  
  13. [OperationContract]  
  14. int Subtract(int num);  
  15. [OperationContract]  
  16. int Multiply(int num);  
  17. [OperationContract]  
  18. void EndPersistence();  
  19. }  
Step 5: Go to SimpleCalculator.cs and write the code as given below.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Serialization;  
  5. using System.ServiceModel;  
  6. using System.ServiceModel.Web;  
  7. using System.Text;  
  8. using System.ServiceModel.Description;  
  9. [Serializable]  
  10. [DurableService()]  
  11. // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service" in code, svc and config file together.  
  12. public class SimpleCalculator : ISimpleCalculator  
  13. {  
  14. int currentValue = default(int);  
  15. [DurableOperation(CanCreateInstance = true)]  
  16. public int Add(int num)  
  17. {  
  18. return (currentValue += num);  
  19. }  
  20. [DurableOperation()]  
  21. public int Subtract(int num)  
  22. {  
  23. return (currentValue -= num);  
  24. }  
  25. [DurableOperation()]  
  26. public int Multiply(int num)  
  27. {  
  28. return (currentValue *= num);  
  29. }  
  30. [DurableOperation(CompletesInstance = true)]  
  31. public void EndPersistence()  
  32. {  
  33. }  
  34. }  
Step 6: 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 and SqlPersistenceProviderLogic.sql
 
Step 7: Go to web.config and do same as written here. 
  1. <system.serviceModel>  
  2. <services>  
  3. <service name="SimpleCalculator" behaviorConfiguration="ServiceBehavior">  
  4. <!-- Service Endpoints -->  
  5. <endpoint address="http://localhost:5174/Durableservice/SimpleCalculator.svc" binding="wsHttpContextBinding"  
  6. bindingConfiguration="browConfig" contract="ISimpleCalculator">  
  7. <identity>  
  8. <dns value="localhost"/>  
  9. </identity>  
  10. </endpoint>  
  11. <endpoint address="mex" binding="mexHttpBinding"  
  12. contract="IMetadataExchange"/>  
  13. </service>  
  14. </services>  
  15. <behaviors>  
  16. <serviceBehaviors>  
  17. <behavior name="ServiceBehavior">  
  18. <serviceMetadata httpGetEnabled="true"/>  
  19. <serviceDebug includeExceptionDetailInFaults="true"/>  
  20. <persistenceProvider  
  21. type="System.ServiceModel.Persistence.SqlPersistenceProviderFactory,  
  22. System.WorkflowServices, Version=3.5.0.0, Culture=neutral,  
  23. PublicKeyToken=31bf3856ad364e35" connectionStringName="DurableServiceStore"  
  24. persistenceOperationTimeout="00:00:10"  
  25. lockTimeout="00:01:00"  
  26. serializeAsText="true"/>  
  27. </behavior>  
  28. </serviceBehaviors>  
  29. </behaviors>  
  30. <bindings>  
  31. <wsHttpContextBinding >  
  32. <binding name="browConfig" >  
  33. <security mode="None"></security>  
  34. </binding>  
  35. </wsHttpContextBinding>  
  36. </bindings>  
  37. </system.serviceModel>  
  38. Step 8: Set connection string also  
  39. <connectionStrings>  
  40. <add name="DurableServiceStore"  
  41. connectionString="Data Source=IT-WSPC-F10\SQL;Initial Catalog =DurableServiceStore;Integrat Security=True"/>  
  42. </connectionStrings>  
Run the code. For more details, download the attached code.