Writing my first WCF service on Vista
Earlier today I decided to try my hands at building a Windows Communication Foundation (WCF) service on my Vista machine. Since Vista (RTM) comes pre-installed with .net framework 3.0, the necessary components for WCF are already on the machine.
However to work with VS2005 and be able to create WCF based projects, you still need to install the WCF extensions. You can get the necessary files from here - http://www.netfx3.com/blogs/technology_news_and_announcements/archive/2006/12/06/net-framework-3-0-has-been-released.aspx
I started VS 2005 and selected option for creating a new Website (since I wanted to work with WCF hosted on IIS). In the new Website wizard, I selected the option for WCF Service, named it appropriately and selected OK. The wizard created the base boiler plate code which had the service contract, the service implementation and also a DataContract to handle custom types.
With this done, it looked that i was ready to go. I built the project and it built successfully. I then went to my browser and tried to access the .Svc file via the url - http://localhost/TestService/Service.svc. To my surprise I ended up getting a Http 404.3 error stating that .svc MIME map wasn't enabled.
Since .net framework 3.0 comes by default on Vista, this was surprising that WCF service wasn't working already. However I realized that IIS 7.0 doesn't comes installed by default Vista and hence it meant that the necessary settings were not configured properly post my installation of IIS 7.0. Check my earlier blog on installing IIS.
I expected something similar on lines of aspnet_regiis.exe -i to work in this case and inspecting the Windows directory reveled that a similar EXE does exsist for WCF configuration. It is ServiceModelReg.exe and can be found in "C:\Windows\Microsoft.NET\Framework\v3.0\Windows Communication Foundation" directory. I then ran this from command prompt as ServiceModelReg.exe -i and it installed the necessary scriptmaps at the IIS metabase root and folders within.
I than accessed the service URL again, but still didn't succeed. The error this time read "Metadata publishing for this service is currently disabled." and it provided detailed steps on how to solve it. It required me to specific service behavior and I did this by editing the default created Web.config file and modifying the default <behaviors>/<serviceBehaviors> section. The changes done to this section is as highlighted in bold face below
<behavior name="returnFaults" >
<serviceDebug includeExceptionDetailInFaults="true" />
<serviceMetadata httpGetEnabled="true" />
With this when I accessed the service URL again, I got the expected page displayed in IIS as shown below.
So far, so good. It was then time to test the working of the service. So I created a new Windows Application and then added a service reference to it. While doing this I again had an error while generating proxy code and it complained about proxy authentication. Making changes to my IE Connection settings, I got past this error.
Finally after adding a button to the form and writing the necessary code behind which looked something like below, I could get the service executing the returning the results.
private void button1_Click(object sender, EventArgs e)
localhost.MyServiceClient p = new ServiceClient.localhost.MyServiceClient();
string result = p.MyOperation1("Atul Gupta");
Note that the names of service contract, its methods, service class etc are all left as default and hence you would see things like MyServiceClient and MyOperation1. Needless to say that when you build your WCF services, you should change these appropriately.
In general it will also help to follow the instructions for running the WCF samples from SDK. Check the details at - http://msdn2.microsoft.com/en-us/library/ms751527.aspx.
There is one last point about intellisense in the web.config file. It wasn't working for me and I found that it was due to the xml namespace in the Web.config - xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0". The problem is described here - http://weblogs.asp.net/scottgu/archive/2005/12/02/432077.aspx