12 09 2007
WCF Services – Let’s Get Started
In connection with testing WCF services I had to get the services to work under IIS on my development computer. The default WCF services library created by Visual Studio does not automatically include IIS support. There were other small glitches that I was able to overcome nicely, in spite of everything.
For starters I created a simple service that adds two figures together. What a classic, isn’t it? When creating the service, I selected WCF Service Library as the project type. The service file named MathService.cs appears as follows.
public interface IMathService
double Add(double x, double y);
public class MathService : IMathService
public double Add(double x, double y)
return x + y;
IIS was the next issue. Mapping the project directory under IIS turned out to be pretty pointless as under IIS all application binaries are searched from the /bin directory but on compilation the files are created in the /bin/Debug directory instead. As workaround I wrote some post-build commands for the WCF service project, copying all necessary files to IIS directories as required. Of course, the IIS directory has to be defined as an IIS application.
Next I dealt with the service configuration.
<servicedebug includeExceptionDetailInFaults="False" />
<servicemetadata httpGetEnabled="true" />
And now the most exciting part – how to make the service visible under IIS. Creating a file with the service’s name and svc extension is sufficient. In my case MathService.svc. The content of the file is simple – it is used to set the name of the service class.
<%@ServiceHost language=c# Debug="true" Service="WCFTest.MathService" %>
Now the service part should be up. It is quite easy to check – you only need to compile the service and enter its URL in the browser. If anything is wrong, we’ll get an error message. If everything is OK, we will see a page introducing the service, with the URL of the service’s WSDL in the upper part.
To test the service I created a simple client program that adds two figures together and displays the result.
static void Main(string args)
MathServiceClient client = new MathServiceClient();
Console.WriteLine("2 + 3 = " + client.Add(2, 3));
Console.WriteLine("Press any key to exit");
Here I discovered something new and interesting – hooking up the service as a web service behaves differently from adding it as a WCF service. In case of SOAP the wrapper methods created are completely different. They have twice as many arguments as the service method plus two additional arguments for the returned result. Every argument and returned result has an additional Boolean-type parameter indicating whether this parameter has been set or not.
When hooking up the service by adding the service reference to the project, these parameters are not generated because the WCF client is able to solve the situation by itself.
You can read more on this topic from Eugene Osovetsky’s blog posting Solving the "disappearing data" issue when using Add Web Reference or Wsdl.exe with WCF services.
Now you have everything you need to get started with WCF and test the results, so I can only wish you success with WCF.