WCF in VS2008 – Basic Service and Client over HTTP channel

Looking through documentation for Windows Communication Foundation (WCF) on MSDN, I am surprised to find almost none information about cool  features of VS2008 added specifically to make development with WCF in VS2008 easier. Most of topics still talk about VS2005.  So I have decided to put a start into this and publish a small “Getting Started” tutorial.  The following subset of VS2008 features is mentioned in the tutorial:

1) New WCF Service wizards

2) Add Service Reference option in Solution Explorer

3) Integration of WCF Host Service and WCF Test Client with VS2008

Here is a brief outline:

1. Troubleshooting tip that may save a lot of your time.

2. Creating WCF Service using VS2008 wizards

3. Testing of WCF Service using VS2008 integration with WCF Host Service and WCF Test Client

4. Creating service client for WCF service using VS2008 support for adding references to service.

 

First, let me start with that tip which is a proven time saver.

Troubleshooting Tip#1 : If you run Windows Vista same as I do, it you may save your time if you run Visual Studio 2008 as Administrator. Otherwise you are going to face access related errors all the time. You may either start Visual Studio 2008 by right clicking on its icon and selecting Run as Administrator or modify its shortcut to always run as administrator by changing properties of the shortcut (right click on the icon, select properties, select Advanced, check the Run this program as an administrator). Hopefully your VS2008 IDE runs as admin at this moment so let’s continue.

Creating WCF Service

In this part we create a simple Web Service using wizards of VS2008 only. Here are the steps:

  1. Open Visual Studio 2008.
  2. Create a WCF Service Library project.
  • In the New Project dialog, select Visual Basic or Visual C#, then choose WCF node and choose WCF Service Library.
  • Name the project and find location for it. I have selected WcfServiceLibrary as a name for the project and WCFinVS2008 as a name for the solution.

clip_image002

  • Hit OK and IDE is going to generate project and source to it.

You are pretty much done at this moment. Hit F5 and you should see WCF Test client loading you service first and then showing it in the navigation pane as pictured below.

clip_image004

Troubleshooting Tip#2: If you see WCF Service Host coming up with an error

System.ServiceModel.AddressAccessDeniedException: HTTP could not register URL https://+:8731/Design_Time_Addresses/WcfServiceLibrary/Service1/. Your process does not have access rights to this namespace (see https://go.microsoft.com/fwlink/?LinkId=70353 for details). ---> System.Net.HttpListenerException: Access is denied

as pictured below

clip_image006

then you have missed the Troubleshooting Tip#1 and you do not run VS2008 as Administrator. Go back to IDE, stop debugging (Shift+F5), save all your work, close and re-open VS2008, load WcfServiceLibrary project again and hit F5. Now you should see the state for your service showing as started pictured above. You are ready to test the service with WCF Test client tool which is now integrated with Visual Studio 2008.

Testing WCF Service using WCF Host Service

WCF Test client provides a basic summary to what it can do in order to help you to test your service. You need to select the operation you want to test, then enter the parameters and wait for result from the service. In our example, double-click on GetData() method listed on the left and the right side of the test client should change to let you start testing the service.

clip_image008

Now you can enter value and click on Invoke to get result for the service.

clip_image010

Another cool part of the tool is the XML tab which let you see SOAP envelops used to form client to service request and service to client response.

clip_image012

Anyway, you are done testing the service with WCF tools and it is time to start building an actual client to this service. Close WCF Test client, go back to VS2008 IDE and stop debugging session (Shift+F5).

Creating client to WCF Service

If you think that it was easy to create a WCF service in VS2008, wait till you see how it is easy to create a client to a web service using VS2008 support for WCF. However first you need to understand the term "service reference". . Without going explaining all technical details, in VS2008 a service reference is a representation of a web service as class within the project. VS2008 generates all necessary code and integrates it with your project. Your code can communicate with the service by calling into methods of the class that represents service reference. Let’s demonstrate this using the service we have created in Creating WCF Service section.

1) Create a new project within WCFinVS2008 solution by doing the following steps:

a. In Solution Explorer (on the upper right), right-click WCFinVS2008 solution title (not the project), and select Add, and then New Project.

clip_image014

b. In the Add New Project dialog, choose Console Application, and name it WcfServiceClient and save it into the default location. Note, that selecting Console Application is not required. You can add service reference to pretty much any type of project. We choose to use Console application because this is the simplest type of projects offered in VS2008.

2) Add Reference to WcfService to the new created WcfServiceClient

a. In Solution Explorer (on the upper right), right-click WCFServiceClient project title and select Add Service Reference. This should open Add Service Reference dialog.

b. Click on Discover button on the right side of the dialog. This is going to discover all services that are part of the current solution and the dialog should change to dialog pictured below:

clip_image016

c. Click OK and let VS2008 generate the code for this reference and add it to WcfServiceClient project.

Troubleshooting Tip#2: If you see WCF Service Host coming up with an error

System.ServiceModel.AddressAccessDeniedException: HTTP could not register URL https://+:8731/Design_Time_Addresses/WcfServiceLibrary/Service1/. Your process does not have access rights to this namespace (see https://go.microsoft.com/fwlink/?LinkId=70353 for details). ---> System.Net.HttpListenerException: Access is denied

as pictured below

clip_image018

Then you may have already guessed that you need to follow guidance in Troubleshooting Tip#1. You may also notice that VS2008 also issues another error

There was an error downloading metadata from the address. Please verify that you have entered a valid address.

pictured below

clip_image020

Well this means that service is not accessible and VS2008 cannot download metadata required for building the service. In our case, we know the reason – someone missed the Troubleshooting Tip#1. But in general case a number of reasons may cause this error all of which boils down to one thing – VS2008 cannot connect to the service and cannot download data required for building reference.

Anyway, if you see the error, click OK in Error dialog, close WCF Service Host window, click Cancel in Add Service Reference, save all files, close and re-open VS2008 as administrator, open WCFinVS2008 solution and repeat steps 2 from above.

3) Adding calls to Web Service from the client

a. Open Program.cs by click on the name of the file in Solution Explorer (on the upper right).

b. Within Main() method calls to service as listed below

static void Main(string[] args)
{
       // create an instance of a client to a service
       ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();

       // calling into GetData()
       string data = client.GetData(1);
       Console.WriteLine(“Data returned from GetData() method: {0}”, data);

       // calling into GetDataContract()
       ServiceReference1.CompositeType composite = new ServiceReference1.CompositeType();
       composite.BoolValue = true;
       composite = client.GetDataUsingDataContract(composite);
       Console.WriteLine(“String returned from GetDataUsingDataContract() method: {0}”, composite.StringValue);
      

       //holding window open just for demonstration purposes

       Console.WriteLine("Press any key to exit the program");
       Console.ReadKey();
}

4) Select WCFServiceClient as Startup project

a. In Solution Explorer (on the upper right), right-click on WCFServiceClient project title and select Set as Startup Project.

5) Start debugging by hitting F5

a. You should a command window opening pictured below

image

That’s it. You have built a simple web service and its client using WCF in VS2008. Now, this is a very simple scenario far away from a real world scenario. However it should give a taste of what it is to create a web service in VS2008.