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:
- <%@ ServiceHost Language="C#" Debug="true" Service="SimpleCalculator" CodeBehind="~/App_Code/SimpleCalculator.cs" %>
Step 4: Go to ISimpleCalculator.cs and add the below code
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.ServiceModel;
- using System.ServiceModel.Web;
- using System.Text;
- [ServiceContract]
- public interface ISimpleCalculator
- {
- [OperationContract]
- int Add(int num);
- [OperationContract]
- int Subtract(int num);
- [OperationContract]
- int Multiply(int num);
- [OperationContract]
- void EndPersistence();
- }
Step 5: Go to SimpleCalculator.cs and write the code as given below.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.ServiceModel;
- using System.ServiceModel.Web;
- using System.Text;
- using System.ServiceModel.Description;
- [Serializable]
- [DurableService()]
-
- public class SimpleCalculator : ISimpleCalculator
- {
- int currentValue = default(int);
- [DurableOperation(CanCreateInstance = true)]
- public int Add(int num)
- {
- return (currentValue += num);
- }
- [DurableOperation()]
- public int Subtract(int num)
- {
- return (currentValue -= num);
- }
- [DurableOperation()]
- public int Multiply(int num)
- {
- return (currentValue *= num);
- }
- [DurableOperation(CompletesInstance = true)]
- public void EndPersistence()
- {
- }
- }
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.
- <system.serviceModel>
- <services>
- <service name="SimpleCalculator" behaviorConfiguration="ServiceBehavior">
- <!-- Service Endpoints -->
- <endpoint address="http://localhost:5174/Durableservice/SimpleCalculator.svc" binding="wsHttpContextBinding"
- bindingConfiguration="browConfig" contract="ISimpleCalculator">
- <identity>
- <dns value="localhost"/>
- </identity>
- </endpoint>
- <endpoint address="mex" binding="mexHttpBinding"
- contract="IMetadataExchange"/>
- </service>
- </services>
- <behaviors>
- <serviceBehaviors>
- <behavior name="ServiceBehavior">
- <serviceMetadata httpGetEnabled="true"/>
- <serviceDebug includeExceptionDetailInFaults="true"/>
- <persistenceProvider
- type="System.ServiceModel.Persistence.SqlPersistenceProviderFactory,
- System.WorkflowServices, Version=3.5.0.0, Culture=neutral,
- PublicKeyToken=31bf3856ad364e35" connectionStringName="DurableServiceStore"
- persistenceOperationTimeout="00:00:10"
- lockTimeout="00:01:00"
- serializeAsText="true"/>
- </behavior>
- </serviceBehaviors>
- </behaviors>
- <bindings>
- <wsHttpContextBinding >
- <binding name="browConfig" >
- <security mode="None"></security>
- </binding>
- </wsHttpContextBinding>
- </bindings>
- </system.serviceModel>
- Step 8: Set connection string also
- <connectionStrings>
- <add name="DurableServiceStore"
- connectionString="Data Source=IT-WSPC-F10\SQL;Initial Catalog =DurableServiceStore;Integrat Security=True"/>
- </connectionStrings>
Run the code. For more details, download the attached code.