Creating WCF Service

This article explains how to create a simple WCF service using the scenario of our applications often requiring code to determine the number of days, such as how long the customer is associated with us, also to convert from a date of present days into days or years and so on.
In a normal application I need to write the business logic repeatedly for the same requirements so due to the requirements you can write a single WCF service for multiple applications that allow an access method on any platform used, so let us start with the basics.

What is WCF?
Windows Communication Foundation (WCF) is communication software between applications of different platforms or the same platform over the various protocols such as TCP, HTTP, HTTPS and so on.
In this article we will just learn the procedure for creating a simple WCF service instead of a theoretical explanation, so let us start creating a simple WCF application.
 Step 1: Create WCF service application as:
  1. "Start" - "All Programs" - "Microsoft Visual Studio 2010".
  2. "File" - "New Project" - "C#" - WCF Service Application.
  3. Provide the project name such as AgecalculatorService or another as you wish and specify the location.
Now the  Solution Explorer will look as:
In the preceding  Solution Explorer you have seen the three files as I have highlighted in a red box, they are:
  • IServices1.cs
This file holds the interfaces that are declared under the Service contract and method signature that are declared under the operation contract in WCF, you can also rename and add a new interface file as well.
  • Service1.svc
This is the service file that contains all the details, like page directive in .aspx, the .SVC stands for service that look as follows:
  • Service1.svc.cs 
This is a class file where IServices1.cs interfaces are implemented, all the logic is written here , the typical svc.cs file will look like as follows.
In the preceding example Service1 is the class name and IService1 is the interface name, and get data is the IService1 interface abstract method implemented in the svc.cs.
 Step 2:  Create service and Operation contract in IServices1.cs as 
 Now open the IServices1.cs  file and declare the service contract (Interface) and Operation contract (method Name) as:
In the preceding example we declare the interface named IService1 under the ServiceContract and CalculateDays method with the three parameters under the operationContract.
Step 3: Implement the IService1 interface in a Service1.svc class file.
Now open the Service.svc.cs file and implement the IService1 interface methods in a Service.svc class file as:
In the preceding example we have created the one class named calculateAge in the service.svc.cs file and implemented the IService1 interface in that. While implementing the interface after a mouse hover on the interface it shows two options of implementing the interface in a class that implements the interface that automatically Implements the method and the second one is implemented explicitly that is allowed to be implemented manually.
After implementing the interface, the following method definition is created in a class as:
Now remove the definition that is by default created as above and write the following code to implement our logic to calculate the given duration into a number of days.
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Runtime.Serialization;  
using System.ServiceModel;  
using System.ServiceModel.Web;  
using System.Text;  
namespace AgeCalculatorService  
    public class CalculateAge : IService1  
        public int calculateDays(int day, int Month, int year)  
            DateTime dt = new DateTime(year, Month, day);  
            int datetodays = DateTime.Now.Subtract(dt).Days;  
            return datetodays;  
In the code above I declared an integer method named calculateDays with the three parameters day, month and year for accepting day, month and year from the user.

Then then I created an object of date time and passed those variables that I get from the users. I declared another variable in the method that is datetodays to store the number of days remaining from the user's input date to the current date and finally I return that variable.
Step 4:  Run the WCF application
Now run the WCF application that shows the WCF Test client window to test the output of the application as:
In the preceding test client window you have seen that it shows all the details of a Service including address, binding, Contracts and Method name with input parameters details and response details of input variable values that I highlighted in a box.
Step 5: Provide Input parametes
Now in the preceding test client window the Request section we see our method Input parameters that we created in the service1.svc.cs file, provide input values for the above three parameters as I have entered my date of birth.
Step 6:  Click on the Invoke button for a response from the service
Now click on the Invoke button; it shows the following response from the service:
In the screen above you see that the output is 9129, that is the days from the input date, in other words from the preceding screen output , you see that currently, I am 9129  days old.
 Watch Video 

      From the preceding example you have seen how to create a simple WCF application. I hope this article is useful for all students and beginners. If you have any suggestion related to this article then please contact me.

      Post a Comment

      Protected by Copyscape
      Copyright © Compilemode