Friday, December 6, 2013

Simple WCF service example for beginners.

Hi Friends,

Here I will explain what WCF (windows communication foundation) is,and how to implement simple example to understand,How to use WCf Service.

What WCF is ?

Windows Communication Foundation (WCF) is a technology for developing applications based on service-oriented architecture (SOA). WCF is implemented using a set of classes placed on top of the .NET Common Language Runtime (CLR). It addresses the problem of interoperability using .NET for distributed applications.


WCF is entirely based on the .NET framework. It is primarily implemented as a set of classes that correspond to the CLR in the .NET framework. However, WCF allows .NET application developers to build service-oriented applications. The WCF client uses Simple Object Access Protocol (SOAP) to communicate with the server. The client and server are independent of the operating system, hardware and programming platform, and communication takes place at a high level of abstraction.

Creating a simple WCF Service :-


Once you create application you got in solution explorer like below 

Now double click on Iservice1.cs and paste code below

 [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string
SampleMethod(string Name);
    } 






 Now double click on service1.svc and paste code below

public class Service1 : IService1
    {
        public string
SampleMethod(string Name)
        {
            return "Hello " + Name + " Welcome to your first WCF program";
        }
    } 





Know Press F5 and build solution




 Know right click on service1.svc and view in browser and copy url.

Now for calling your first WCF service take another new project.
To call WCF service there are many ways like using console app, windows app and web app but here I am going for console application.

Name it FirstWCFServiceConsole

 
After it in Solution explorer Right-Click on Add References and Click on Add Service refrences.

Window apperes like below paste url which you copies early in service view in browser.


Now paste code in Program.cs like below

ServiceReference1.Service1Client objService = new ServiceReference1.Service1Client();
            Console.WriteLine("Please Enter your Name");
            string Message = objService.SampleMethod(Console.ReadLine());
            Console.WriteLine(Message);
            Console.ReadLine();




 Now open app.config paste copied url in endpoint address="" like below

 
Now the console application



No comments:

Post a Comment