SvcUtil Essentials: Service Validation

27. July 2007 10:00

This will probably be the last post in my mini-series that highlights some of the capabilities of svcutil.

How many times have you attempted to start up a service to discover there was a problem that causes a runtime error?  If you have done very much WCF development, the answer is probably quite a few.  Rather than fighting through a series of runtime errors, wouldn't it be nice to get a complete list of the problems with your service?  This is exactly one of the lesser known features of svcutil: service validation.

Before we get into exactly what it does, let's cover why it is necessary in the first place.  So, we will begin with an example.  Let's assume that you have a service contract that looks like the code shown below:

interface IZipCodeService
    [OperationContract(IsOneWay = true)]
    string GetCityName(string zipCode);

This snippet of code contains an interface that has been decorated with the necessary attributes to expose the operations for consumption.  If you look closely, the OperationContract for the GetCityName method has set the IsOneWay property to true.  This tells the WCF runtime that caller doesn't have to wait for a return value.  As soon as the operation begins execution, control will be returned to the caller.  However, IsOneWay can only be set to true for methods that don't return any values (a void return type). 

The GetCityName method does have a return type of string.  So, it would not be legal to set IsOneWay to true for the method.  At first glance, you might tell yourself "Eh, no big deal.  It will generate a compilation error."  Unfortunately, you would be incorrect.  WCF does not have any special interface to the C# compiler (or VB.NET).  As far as the compiler is concerned, this an attribute that can only be applied to methods, which is valid in this case.  Also, the compiler only sees that IsOneWay is a boolean and can be set to true or false, which is also valid in this case.

Consequently, it is up to the WCF runtime to ensure the attribute is valid when your service is loaded.  Basically, this means you will not know whether your service contains any invalid configuration until is has been loaded at runtime.  This is why many people run into a series of errors when they simply start the project that is hosting their service for the first time.  If you use this approach to validate the service, you essentially have to correct one problem, recomplie, and try again...which can get annoying if you have several problems with the service.

This is where svcutil can make your life a bit easier.  Rather than hosting the service to allow the WCF runtime to determine whether it is valid, the command line utility can be used to do the same thing without a hosted service.  For example, the following command could be used to validate the service from the previous code snippet.

svcutil /validate /serviceName:ZipCodeService ZipCodeService.dll

The validate switch tells the command line utility that you want to perform a service validation.  The serviceName switch is used to reference the name of the service within your corresponding configuration file.  This is one of the only limitations to using svcutil for service validation.  It must have a config file in order for it to work.  If you programmatically configure your service, you will have to validate it the good ole fashioned way.  Lastly, you specify the assembly that contains the complied service. 

When the command is execute, you will see some output similar to the following:

Warning: There was an error loading the service to be validated.
    Type: JeffBarnes.Samples.ZipCodeService, ZipCodeService, Version=
, Culture=neutral, PublicKeyToken=null.
    Details:Operations marked with IsOneWay=true must not declare output parameters, by-reference parameters or return values.

The Service 'JeffBarnes.Samples.ZipCodeService' was validated with no

As you can see, a warning was generated that indicates the problem we previous discussed concerning IsOneWay only being valid when there is no return type.  However, it also says the service did not contain any errors.  To be honest, I am a little fuzzy on the difference between a warning and an error.  Even though the problem is listed as a warning by the command line utility, you will get a runtime error if you attempt to host the service.

At any rate, this is another approach for validating your service that can be added to your toolbox.

Comments are closed

About Me

I'm a passionate software developer and advocate of the Microsoft .NET platform.  In my opinion, software development is a craft that necessitates a conscious effort to continually improve your skills rather than falling into the trap of complacency.  I was also awarded as a Microsoft MVP in Connected Systems in 2008, 2009, and 2010.

Can’t code withoutThe best C# & VB.NET refactoring plugin for Visual Studio
Follow jeff_barnes on Twitter

View Jeff Barnes's profile on LinkedIn


Shared Items


Anything you read or see on this site is solely based on my own thoughts.  The material on this site does not necessarily reflect the views of my employer or anyone else.  In other words, I don't speak for anyone other than myself.  So, don't assume I am the official spokesperson for anyone.