WCF 4.5 Configuration from external source

1 בApril 2012

3 comments

WCF 4.5

There is a lot new in WCF 4.5 you can get the full list Here. It always amaze me how much is added from version to version – and the additions are important (UDP, Web Socket binding ext.)

One specific feature caught my eye – Configuring WCF Services in Code. It adds the possibility to setup WCF parameters after it was deployed or when you don’t have access to SerivceHost object.

This is done by a simple static function you can add to your service implementation:

public static void Configure(ServiceConfiguration config)

ServiceConfiguration allowing to add endpoints, behaviors ext. This is nice but why is it so interesting… let’s see the following problem and solution.

Problem – WCF Configuration in enterprise application

I think almost every one that developed enterprise application with WCF service as the server side entry point felt this pain. You want to be able to manage your WCF services configurations from single place – if you have more then one service defined you don’t want to use files for configuration and you want to persist the WCF configuration in one place. sure you can configure WCF in code (if you have access to ServiceHost) but you cannot use the same system.serviceModel configuration section – load it from external source.

Solutions – before WCF 4.5

I seen a lot of solutions along the way:

  • Implementing your own structure for WCF configuration and write a code that apply it on the service host object – the major downside here is that you have to duplicate the code that exists somewhere inside WCF that reads system.serviceModel configuration and build the matching WCF objects: Bindings, Behaviors ext.
  • I saw complicated solution that use reflection of the ServiceHost object and inject the system.serviceModel configuration section – the downside here is the risk (you can fail on runtime and the internal may change)
  • Nice solution by Alon Fliess was to create another app domain and associate specific file to the new domain allowing to read configuration from external file – read about it here.

All the solution above has some downside and this is because ServiceHost was not build to get the configuration from external source – all the solution above are workarounds.

Solve it WCF 4.5 style

This solution is not perfect too (read limitation) but it uses given WCF API which means it is the best we currently have. So as I mention the static function Configure was added but what is doing the trick it the function LoadFromConfiguration of the ServiceConfiguration object. This allows us to write this code:

public class EchoService : IEchoService
{
public static void Configure(ServiceConfiguration config)
{
var cfg = MyConfigurationManager.GetConfigurationByServiceType(typeof(EchoService));
config.LoadFromConfiguration(cfg);
}

public string Echo(string input)
{
return string.Format("Echo:{0}", input);
}
}

All the magic is done in the “MyConfigurationManager” which can have is own logic – reading from DB by given parameters (could be service type, machine name ext.) and returns System.Configuration object that is loaded using the LoadFromConfiguration.

to complete my example my configuration manager read it from a file:

public static class MyConfigurationManager
{
public static Configuration GetConfigurationByServiceType(Type serviceType)
{
var fileMap = new ExeConfigurationFileMap() { ExeConfigFilename = Path.Combine(Environment.CurrentDirectory, "ExternalConfiguration.xml") };
return ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

}
}

Limitation:Note that LoadFromConfiguration ignores <host> settings within the <service> tag of <system.serviceModel>. Conceptually, <host> is about host configuration, not service configuration, and it gets loaded before the Configure method executes.

Summery

I would like to be able to pass to ServiceHost Configuration object. we are still not there…maybe at WCF 5.0 Smile

  Cheers, Offir

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>

*

3 comments

  1. JanivZ1 בApril 2012 ב 21:30

    great! thank u!

    Reply
  2. Y.L12 בAugust 2012 ב 10:37

    Great 1. Thanks!

    Reply
  3. Jose3 בSeptember 2012 ב 12:04

    Hi Thomas,Love your work keep it up!I’ve been planning a XMPP (“Jabber”) Transport Channel with surpopt for XEP-0072; SOAP over XMPP for some time (or, the idea dawned on me some time ago, and I’m about to pick it up again).Do you have any advice (in general) where to look for good resources on custom transport channels? I’ve read Dr. Nick’s blog posts but do you know of any good sample implementations?Coming to think of it, I know that the ActiveMQ project have a WCF channel, so I better check it out. larsw

    Reply