Managing WCF 4.5 Configuration Files in WCF Service Applications

In the last few posts I’ve been looking at the new options that WCF 4.5 gives you for configuring your services. The first two posts showed how to configure a Web Service in an ASP.NET application and how to configure a service both as a Web Service and as higher-performance TCP-based service in a Service Library. My last post was about how to load your service’s configuration from a central file location. However, I only discussed how that process worked in a Service Library—other than a brief note, I ignored a WCF service in an ASP.NET application (a Service Application). In this post, I’m going to address how to load a service’s configuration file from something other than the web.config file.

One note: I am working with the beta version of Visual Studio 11 and the .NET Framework 4.5 so it’s entirely possible that what I say here may change by the time both products get released to manufacturing.

To begin with, the code you use to load the configuration file in a WCF Service Application isn’t any different from the code you’d use in a WCF Service Library. You still add a static/Shared method called Configure to your service class and put this code in it, pointing to the file containing your service’s configuration:

PublicSharedSub Configure(sc AsServiceConfiguration)  Dim cfMap As New System.Configuration.ExeConfigurationFileMap With {  .ExeConfigFilename = "c:configsService1.config"}  Dim cf As System.Configuration.Configuration cf = System.Configuration.ConfigurationManager.  OpenMappedExeConfiguration(cfMap,  System.Configuration.ConfigurationUserLevel.None)  sc.LoadFromConfiguration(cf) End Sub 

The problem is what you should put in that Service1.config file. In the Service Library, you could fundamentally just copy the system.servicemodel element to the configuration file—the only change you had to make was to convert any relative addresses to absolute addresses. You could avoid even that change by leaving the host/baseaddress elements in your app.config file (you didn’t even have to remove the host element from the config file you were loading—WCF 4.5 just ignored them).

Unfortunately, with a Service Application in WCF 4.5 you don’t have a complete set of tags in your Web.config file to copy. This is the result of another change in WCF 4.5: “WCF simplification”. If an element in the config file is setting the default value for the element then, by default, that element is omitted from the Web.config file. This means that a Web.config file in the .NET Framework 4.5 set up for testing/development can look as simple as this:

<system.serviceModel>  <behaviors> <serviceBehaviors> <behavior> <serviceMetadata httpGetEnabled="true"  httpsGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="true"/> </behavior> </serviceBehaviors>  </behaviors>  </system.serviceModel>

If you just copy this to your config file (Service1.config in my sample code) you won’t be able to use the WCF Test Client to test your application because the Test Client won’t be able to retrieve the metadata it needs to figure out what methods your service exposes. To fix that problem, you need to add a service tag (within a services element) that references your service by namespace and name. And, as long as you’re doing that, during development you probably also want to include all the exception detail in any faults your service raises so you should add the serviceDebug element that lets you specify that with its includeExceptionDetailInFaults attribute.

Putting that altogether, your configuration file should look like this:

<configuration>  <system.serviceModel>  <services>  <service name="WcConfigWeb.Service1"/>  </services>  <behaviors>  <serviceBehaviors>  <behavior name="HttpGetMetadata">  <serviceMetadata httpGetEnabled="true" />  <serviceDebug includeExceptionDetailInFaults="true" /> </behavior>  </serviceBehaviors>  </behaviors>  </system.serviceModel>  </configuration> 

That file will be successfully loaded by the code I showed earlier and make your service available for testing.

Peter Vogel

Type to search

Do you mean "" ?

Sorry, no results were found for your query.

Please check your spelling and try your search again.