Creating WCF Service

Background

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 WCF is

Windows Communication Foundation (WCF) is communication sotware 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 Servicecontract and method signature that are declared under the operation contract in WCF, you can also rename and add a new interface file as well.

e.g
 
  • 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.
 
Service.svc.cc
  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.   
  9. namespace AgeCalculatorService  
  10. {  
  11.      
  12.     public class CalculateAge : IService1  
  13.     {  
  14.   
  15.         public int calculateDays(int day, int Month, int year)  
  16.         {  
  17.             DateTime dt = new DateTime(year, Month, day);  
  18.             int datetodays = DateTime.Now.Subtract(dt).Days;  
  19.             return datetodays;  
  20.         }  
  21.   
  22.     }  
  23.   
  24. }  
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 ed 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.
 
Note:
  • For detailed code please download the Zip file attached above.

Summary

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.
 

Up Next
    Ebook Download
    View all
    Learn
    View all