I am working on a large system, for which I have to use WCF to access a web service. My test code works fine, now I need to integrate my WCF client code into the larger system. I cannot add to the existing 'app.config' file, and would like specify a separate .config file for use by my client code.
How best can I accomplish this?
Thanks!
There are 2 options.
Option 1. Working with channels.
If you are working with channels directly, .NET 4.0 and .NET 4.5 has the ConfigurationChannelFactory. The example on MSDN looks like this:
As pointed out by Langdon, you can use the endpoint address from the configuration file by simply passing in null, like this:
This is discussed in the MSDN documentation.
Option 2. Working with proxies.
If you're working with code-generated proxies, you can read the config file and load a ServiceModelSectionGroup. There is a bit more work involved than simply using the
ConfigurationChannelFactory
but at least you can continue using the generated proxy (that under the hood uses aChannelFactory
and manages theIChannelFactory
for you.Pablo Cibraro shows a nice example of this here: Getting WCF Bindings and Behaviors from any config source
Or, you can do it a simple and easy way - and implement a custom config file as in this post, which uses a DataSet / DataTable model to store / retrieve your configuration (includes working code):
(.Net) suggestions on making a config file for a program?
you can't do this as you like - you can come close, but can't do it totally.
What you could do is add this section to the main app's config file:
So for each section inside
<system.serviceModel>
, you could specify an external config file by using theconfigSource=
attribute (and don't let Visual Studio's red squiggly lines confuse it - yes, it DOES work!).You can do this for any configuration section - but unfortunately, there's no way to do this for the whole section group (
<system.serviceModel>
).Marc
So the option mentioned by marc_s DOES work. Ignore Visual Studio warning that it doesn't recognize the configSource property on bindings and all other places.
There is no built-in support for this in WCF unfortunately. You must create your own ChannelFactory subclass and load/parse configuration files yourself. Check out this post here on MSDN forums for more implementation details.