This article discusses some of the new features that Visual Studio 2008 provides us for WCF. This article assumes that the reader has a basic knowledge on how WCF Services are built and used.
You can find more on how to get started with WCF here.
Visual Studio 2008 itself has many good and notable features like Refactoring, WPF support etc. and one such feature is also Multi-targeting. With Visual Studio 2008 you can build projects against .NET Framework 2.0 or 3.0 or 3.5.
Yes, one IDE for all the three framework versions! Gone are the days where you had to keep the earlier version of Visual Studio for the earlier .NET versions. Now you can use your Visual Studio 2005 projects which were targeted towards .NET 2.0 versions in Visual Studio 2008.
This means that you can also target your WCF Service to any of these versions. You can also change the target version later by going into the Project Properties page.
Default WCF Project Templates
Unlike in Visual Studio 2005, Visual Studio 2008 now has WCF Project Templates installed by default.
There is a separate section called WCF which also lists Windows Workflow Foundation templates along with WCF Service Library template.
And there is also a separate WCF Service Application template which can be used for creating Services that are hosted in web space.
Once you choose a template, the necessary project files are created for you.
For this blog post, I am using WCF Service Application and going to host it in IIS 7.
SimpleCalculator – Our WCF Example
We are going to create a simple calculator service which can
1) Add two numbers
2) Subtract two numbers
3) Multiply two numbers, and,
4) Return a set of numbers
So, we have two projects, mainly
1) SimpleCalculatorService – WCF Service
2) SimpleCalculatorClient – WCF Client for the above SimpleCalculatorService
Here is the class diagram for our Service
And for our Client
With Visual Studio 2008, we have a new tool called WcfTestClient which is a Windows application which can be used to test our WCF Services. We could use this to invoke service operations and check whether they are working properly or not. It is similar to how we test our ASMX Web Service applications, but here it is a separate Windows Application.
Open up Visual Studio command prompt. It can be found in your Visual Studio 2008 Tools folder in the Start menu
You can now test our CalculatorService by giving the WSDL address to WcfTestClient: http://localhost:7476/CalculatorService.svc?wsdl
The WcfTestClient then adds the service and loads all the operations available.
To test the operations, you can just double click on each operation and then supply the parameters. Here is for the Add operation:
It’s now easy to see whether your Service works or not before actually starting to write a Client.
WCF Services are Self Hosted now
With Visual Studio 2008 now you can actually debug a WCF Service and that is possible because they are self hosted when you start debugging (when you hit F5).
The WcfSvcHost.exe does the trick for you. If it’s a WCF Service Library, then it’s hosted via WcfSvcHost.exe and if it’s WCF Service Application, it’s hosted in the local ASP.NET Development Server and that’s how we got http://localhost:7476/CalculatorService.svc?wsdl working.
Adding Service Reference
Let’s start building our Client. Our Client is a console application. Visual Studio 2008 provides you an improved Add Service Reference option when compared with what was available in Visual Studio 2005.
Now with the Add Service Reference wizard’s Advanced Options, you can also generate asynchronous operations for your Service!
Adding the Reference generates a new folder called Service References in your project where the Service resides.
To configure/update our Service Reference later, we could right click on our service and say Update or Configure. I find this feature to be more stable than in Visual Studio 2005.
Our SimpleCalculatorClient also uses asynchronous calls to our SimpleCalculatorService to add and here is the code for that
In our Service, we have an operation which could return the numbers, the client requested from a start number to end number. The signature of that method in our Service is
List[int] GetNumbers(int from, int to);
And how has the Client interpreted it?
If a WCF Service returns IEnumerable[T] or IList[T] or ICollection[T], the proxy by default converts them to array type and thus we got int in place of List[int].
Can we change this behavior? Yes, you can. In your Add Service Reference Wizard’s Advanced Options, there is an option which lets you specify this behavior.
Deploying our Service
With IIS 7 deploying our WCF Service is simple thanks to WAS (Windows Process Activation Services)! Read here on how to configure IIS 7 for WCF.
Open up your IIS Manager and traverse to your Default Website and Add Application
And then add an Alias to our Service and give the path where we have our Service files
And now we are ready and deployed our SimpleCalculatorService! You can check in your browser by typing
You can also add the CalculatorService.svc as your Default Document, and now with Visual Studio 2008 and IIS 7 you can do it in your web.config:
And now you can just type http:///SimpleCalculatorService
You can download the sample SimpleCalculatorService and SimpleCalculatorClient application here.
Download Visual Studio 2008 90 day trial
For detailed information and to request a free 90-day trial DVD of Visual Studio 2008 Team Suite to be sent out to you, go to the Microsoft Visual Studio webpage.
About the Author
Chakkaradeep (a.k.a Chaks among his friends) is currently doing his Masters at University Of Otago, Dunedin. He is also the current Microsoft Student Partner for University of Otago. He writes two blogs: Chaks' Corner and Chaks' Geekzone Corner.
Other related posts:
The New Zealand ALM Conference 2011 (Application Life Cycle Management)
Writing your own Html Helpers for the ASP.NET MVC Framework
Automating Visual Studio 2008
Comment by syed ahmed ali, on 14-Mar-2008 00:39
hi i want an example to create a WCF service in website application . i also want to know how to call the service and test the functionality. plz send the steps in an easy manner THANKS
Comment by Alex, on 25-Mar-2008 04:27
Comment by Per Mikkelsen, on 28-Mar-2008 11:56
Hi Great - I have spend the hole day at the job trying to figure this out myselfe and had no luck. And then when I came home I saw your tips and trick and now after 30 min everything works perfectly. Thank you
Comment by saravana, on 19-Jun-2008 23:57
Hi Chakkaradeep, I want to know whether my CE5.0 and WM5.0 terminal supports WCF or not. Could you please tell me if there is any application to check that? Or to include this feature what are things i need to take into consideration? Please give me inputs and do the needful. Saravana
Comment by Anil Patel, on 22-Oct-2008 06:18
Hi, I am using Vs2008 and using Service References. I note that when I use the "Update Service Reference", it generates new entries in my config file. Do you why this occurs and if there is a way of controlling this. Best Regards, Anil.
Comment by yeketaz, on 20-Apr-2009 19:36
please give me an example of application WCF in C#.thank you