Monday, April 02, 2012

WCF 4.5 Configuration from external source

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);
       }
   }

Read more: Offir Shvartz
QR: Inline image 1

Posted via email from Jasper-Net