Thursday, March 1, 2012

#9 : WCF Service Application


Windows Communication Foundation or simply we call it WCF, is part of .Net framework that provides a programming model for building service- oriented applications. You can use it to communicate services across the web or within the enterprise applications. Consider a scenario when you want to develop an application that can be accessible by other software running inside or outside your company. WCF is the best way to develop such application.

Let's start with building a simple WCF Service Application. Open Visual Studio 2010 and create new project. Select WCF Service Application as shown below;


Type in the name of this service. In this example, we will it MyFirstWCF. When you click OK, you will notice that the selected template will automatically create Service1.svc and IService1.cs. Since we want to create from scratch, remove Service1.svc and IService1.cs. On the Solution explorer, right click on your project name and select add new item. Select WCF service and name it TestService.svc.


This will create a TestService.svc and ITestService.cs. Check you Solution explorer you should have the items as shown;



Double click on TestService.svc  and type the code below;

public string SayHi(String yourname)
        {
            return "Hi " + yourname;

        }

Go to ITService.cs and type the code below;

 [OperationContract]
        string SayHi(String yourname);

Go back to TestService.svc  and press F5. The WCF Test Client window will show;



To test the service, double click on the "SayHi" type a name under the Request value and click invoke. The service should have a Response value "Hi " + name.

Our first WCF Service Application is done. You can now deploy it in the web or in your own local IIS. Then add this service in your Client application. You can call this service by using the ff;

static void Main()
    {
        TestServiceClient client = new TestServiceClient();
        client.SayHi("YourName")
        client.Close();
    }


No comments:

Post a Comment