MEF + WCF: Config issues
MahTweets makes extensive use of the Managed Extensibility Framework (MEF) for plugins, and recently we had the need for a plugin to use WCF to connect to an external service. The plugin was going to connect to the Bing Translation Service, and auto-translate all tweets into the language of your choice.

However, we ran into a few problems. The normal WCF magics picks up the configuration automagically from your app.config. WCF looks at the program app.config, but under MEF this gets a little trickier. It continues to look at the host programs app.config not the plugin app.config. Something about AppDomains.
There are a few solutions
1. Write all your configuration in code behind
I haven’t actually figured out how to do this for WCF clients but for hosting WCF services, its fairly easy. Adding yet another plugin, I needed to do some hosting over NetTCP bindings (named pipes). This particular service is so the TweetSaver plugin can work.
ServiceHost service = new ServiceHost(typeof(TweetSaverService), new Uri("net.tcp://localhost:3131/TweetSaverService")); service.AddServiceEndpoint(typeof(TweetSaverService), new NetTcpBinding(), "TweetSaverService"); var smb = service.Description.Behaviors.Find<ServiceMetadataBehavior>(); if (smb == null) smb = new ServiceMetadataBehavior(); smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15; service.Description.Behaviors.Add(smb); service.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexTcpBinding(), "net.tcp://localhost:3131/TweetSaverService/mex"); service.Open();
Obviously for different types of bindings there are differences, but the configuration doesn’t vary too much.
2. Use ye-olde Web Reference instead of Service Reference
In Visual Studio 2008, you can’t just right click on the project to Add Web Reference anymore, it’s hidden away a little.
Right click on your project, Add Service Reference –> Advanced –> Add Web Reference
Or in pictures (click on any image to enlarge it)…
This is the method I ended up using for the Translator plugin, simply because it was the path of least resistance/work. The older style of SOAP interaction still uses configuration files, but unlike WCF it is happy to look in the plugin configuration file.
3. Include the configuration information inside the host config
I’d highly recommend against this, as it really defeats the purpose of having a plugin in the first place, but you can get around the issue by putting the configuration from your plugin’s app.config into the host program’s app.config.




http://www.paraesthesia.com/archive/2008/11/26/reading-wcf-configuration-from-a-custom-location.aspx
Has code for clients and hosts.
http://stackoverflow.com/questions/482835/wcf-configuration-split-it-out-of-app-config
SO question of same
Aha! Thanks for that Josh, I’ll follow those up and adjust the article when I’ve got a few moments to spare :)