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()]
- // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service" in code, svc and config file together.
- 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()
- {
- }
- }

Sonu ChaudharyPosted Mar 18, 2016, 2:59 PM
nice
Armugam ReddyPosted Jan 4, 2016, 7:04 AM
Very Understandable & Simple Code For The Beginners...Thank You So Much... :-)