WCF configuration – using the correct service name

August 4, 2011

no comments

In the last couple of weeks, I’ve been seeing several questions posted in the MSDN’s WCF forum about problems loading or consuming services, which eventually are found to be because of a misconfigured service name in the WCF configuration section (the <system.serviceModel>|<services> section).

This is an example of a thread that ended by fixing the namespace in the configuration:


And this thread was a bit trickier, but also ended with a “small” mismatch problem:


So let me just recap the purpose of the name attribute in the service section – when a service host is opened for your service type (which you pass in the constructor), the host searches the configuration file for the configuration of the service. A configuration file can contain configuration for numerous services, so the host tries to find the matching <service> element by checking the name attribute. The name attribute therefore should be set to one of the following:

The service class name:

The name attribute should contain the full name of the service class, including its namespace, so for example, if your service class is called Service1, and it is defined under the namespace TestApplication.Services, then the service configuration will be:
<service name=”TestApplication.Services.Service1” …>…</service>

A fixed name:

Some people don’t like the concept of writing the name of the class and the namespace in the configuration files, especially when a long namespace is used. Therefore, there is another option – you can set a special “configuration name” for your service, and use that name instead of the namespace+class technique.
To set a special configuration name for your service, decorate your service class with the ServiceBehavior attribute, and set the ConfigurationName property to a fixed name, for example “myService”:
public class Service1 : IService1 {…}

Now, all you need to do in the configuration file, is to set the name attribute to myService:
<service name=”myService” …>…</service>

Side note – you can also use the ConfigurationName parameter in the ServiceContract attribute in order to give the contract a fixed name, and use it in your endpoint configuration, instead of setting the contract attribute to the full name of the contract interface.

kick it on DotNetKicks.com Shout it
Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>