"Knowledge has to be improved, challenged, and increased constantly, or it vanishes."

Create and Consume WCF service using Visual Studio 2010

In this article I am going to demonstrate how to create a WCF service, that can be hosted inside IIS and a windows application that consume the WCF service.

To support service oriented architecture, Microsoft developed the programming model named Windows Communication Foundation (WCF). ASMX was the prior version from Microsoft, was completely based on XML and .Net framework continues to support ASMX web services in future versions also. While ASMX web services was the first step towards the service oriented architecture, Microsoft has made a big step forward by introducing WCF.

An overview of planning for WCF can be found from this link http://msdn.microsoft.com/en-us/library/ff649584.aspx .

The following are the important differences between WCF and ASMX from an asp.net developer point of view.

1. ASMX web services are easy to write, configure and consume

2. ASMX web services are only hosted in IIS

3. ASMX web services can only use http

4. WCF, can be hosted inside IIS, windows service, console application, WAS(Windows Process Activation Service) etc

5. WCF can be used with HTTP, TCP/IP, MSMQ and other protocols.

The detailed difference between ASMX web service and WCF can be found here. http://msdn.microsoft.com/en-us/library/cc304771.aspx

Though WCF is a bigger step for future, Visual Studio makes it simpler to create, publish and consume the WCF service. In this demonstration, I am going to create a service named SayHello that accepts 2 parameters such as name and language code. The service will return a hello to user name that corresponds to the language.

So the proposed service usage is as follows.

Caller: SayHello(“Sreeju”, “en”) -> return value -> Hello Sreeju

Caller: SayHello(“اسم”, “ar”) -> return value -> مرحبا اسم

Caller: SayHello(“Sreeju”, “es”) - > return value -> Hola Sreeju

Note: calling an automated translation service is not the intention of this article. If you are interested, you can find bing translator API and can use in your application. http://www.microsofttranslator.com/dev/

So Let us start

First I am going to create a Service Application that offer the SayHello Service. Open Visual Studio 2010, Go to File -> New Project, from your preferred language from the templates section select WCF, select WCF service application as the project type, give the project a name(I named it as HelloService), click ok so that visual studio will create the project for you. In this demonstration, I have used C# as the programming language.

clip_image002

Visual studio will create the necessary files for you to start with. By default it will create a service with name Service1.svc and there will be an interface named IService.cs. The screenshot for the project in solution explorer is as follows

clip_image003

Since I want to demonstrate how to create new service, I deleted Service1.Svc and IService1.cs files from the project by right click the file and select delete.

Now in the project there is no service available, I am going to create one. From the solution explorer, right click the project, select Add -> New Item

Add new item dialog will appear to you. Select WCF service from the list, give the name as HelloService.svc, and click on the Add button.

clip_image005

Now Visual studio will create 2 files with name IHelloService.cs and HelloService.svc. These files are basically the service definition (IHelloService.cs) and the service implementation (HelloService.svc). Let us examine the IHelloService interface.

clip_image006

The code state that IHelloService is the service definition and it provides an operation/method (similar to web method in ASMX web services) named DoWork(). Any WCF service will have a definition file as an Interface that defines the service.

Let us see what is inside HelloService.svc

clip_image007

The code illustrated is implementing the interface IHelloService. The code is self-explanatory; the HelloService class needs to implement all the methods defined in the Service Definition. Let me do the service as I require.

Open IHelloService.cs in visual studio, and delete the DoWork() method and add a definition for SayHello(), do not forget to add OperationContract attribute to the method. The modified IHelloService.cs will look as follows

clip_image008

Now implement the SayHello method in the HelloService.svc.cs file. Here I wrote the code for SayHello method as follows.

clip_image009

I am done with the service. Now you can build and run the service by clicking f5 (or selecting start debugging from the debug menu). Visual studio will host the service in give you a client to test it. The screenshot is as follows.

clip_image011

In the left pane, it shows the services available in the server and in right side you can invoke the service. To test the service sayHello, double click on it from the above window. It will ask you to enter the parameters and click on the invoke button. See a sample output below.

clip_image013

Now I have done with the service. The next step is to write a service client.

Creating a consumer application involves 2 steps. One generating the class and configuration file corresponds to the service. Create a project that utilizes the generated class and configuration file. First I am going to generate the class and configuration file.

There is a great tool available with Visual Studio named svcutil.exe, this tool will create the necessary class and configuration files for you. Read the documentation for the svcutil.exe here http://msdn.microsoft.com/en-us/library/aa347733.aspx .

Open Visual studio command prompt, you can find it under Start Menu -> All Programs -> Visual Studio 2010 -> Visual Studio Tools -> Visual Studio command prompt

clip_image014

Make sure the service is in running state in visual studio. Note the url for the service(from the running window, you can right click and choose copy address). Now from the command prompt, enter the svcutil.exe command as follows.

clip_image015

I have mentioned the url and the /d switch – for the directory to store the output files(In this case d:\temp). If you are using windows drive(in my case it is c: ) , make sure you open the command prompt with run as administrator option, otherwise you will get permission error(Only in windows 7 or windows vista).

The tool has created 2 files, HelloService.cs and output.config.

Now the next step is to create a new project and use the created files and consume the service. Let us do that now. I am going to add a console application to the current solution. Right click solution name in the solution explorer, right click, Add-> New Project

Under Visual C#, select console application, give the project a name, I named it TestService

clip_image017

Now navigate to d:\temp where I generated the files with the svcutil.exe. Rename output.config to app.config.

Next step is to add both files (d:\temp\helloservice.cs and app.config) to the files. In the solution explorer, right click the project, Add -> Add existing item, browse to the d:\temp folder, select the 2 files as mentioned before, click on the add button.

clip_image019

Now you need to add a reference to the System.ServiceModel to the project. From solution explorer, right click the references under testservice project, select Add reference. In the Add reference dialog, select the .Net tab, select System.ServiceModel, and click ok

clip_image020

Now open program.cs by double clicking on it and add the code to consume the web service to the main method. The modified file looks as follows

clip_image021

Right click the testservice project and set as startup project. Click f5 to run the project. See the sample output as follows

clip_image022

Publishing WCF service under IIS is similar to publishing ASP.Net application. Publish the application to a folder using Visual studio publishing feature, create a virtual directory and create it as an application. Don’t forget to set the application pool to use ASP.Net version 4.

One last thing you need to check is the app.config file you have added to the solution. See the element client under ServiceModel element. There is an endpoint element with address attribute that points to the published service URL. If you permanently host the service under IIS, you can simply change the address parameter to the corresponding one and your application will consume the service.

clip_image024

You have seen how easily you can build/consume WCF service. If you need the solution in zipped format, please post your email below.

119 Comments

Comments have been disabled for this content.