Aug 8, 2015

Create and consume simple WCF service in C#.NET

The acronym WCF stands for Windows Communication Foundation. It’s a architectural style to exchange data across two applications. The beauty of any service is that any client can able to consume it. It might be any client written by C# or other (probably Java/PHP etc) programming language.
In this article we will implement one simple WCF service and we will consume it by one console application. Let’s create the calculator application step by step.
Step 1:- Create one WCF application in Visual Studio
Once we create any WCF service template, we will find one interface and one class which implement the interface.
Here is our interface

sing System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;


 namespace PriceCalculator



{  

  // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "ICalculator" in both code and config file together.   

 [ServiceContract]
    public interface ICalculator  



 {     
   [OperationContract]

        string Calculate(int price, int Qty);


    }

 } 

We have placed the ServiceContract attributes over interface. It implies that this is our interface and it’s going to expose to outer world and the function Calculate is specified as OperationContract which means that it’s out function which will be called by external client. 


Step 2:- Implement interface to class
Now, we have to implement this interface to class. Have a look on below code


using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
 namespace PriceCalculator



{    


 public class Calculator : ICalculator    

{     

   public string Calculate(int price, int Qty)

        {         

   return Convert.ToString(price * Qty);
        }  

  } 
} 



This is very simple class where we have implemented the interface. Now, the client can call this service by using the object of this class.
Hence we have finished the server creation part. If we want to check the service we can see it in browser. Just run the WCF application and we will find that service is running in browser, if everything is OK.
Here is output of service application.




OK, It’s running fine . We can go to next step .
Step 3:- consume service by client.
In this step we have to consume WCF service by client. We will create one small console application to do same. Let’s create one console application and add service reference.
Go to Add Service reference option and discover the service. We will get below screen.






Once you press OK ,it will add proxy in our client to consume service. And we will see that the service reference has added in our solution explorer.
Step 4:- Call to service
This is the last step of application. Here we will consume the service

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Client.PriceCalculator;


namespace Client


{   

 class Program 

   {       

 static void Main(string[] args)
       

 {           

 PriceCalculator.CalculatorClient O = new CalculatorClient();
           

 Console.WriteLine("Price is :- " +  O.Calculate(100 ,10));
          

  Console.ReadLine();
        } 

    } 
} 


The consumption part is very simple. Just we have created the object of service class and like normal function invocation we are calling service method.
The output is here.





No comments:

Post a Comment