8

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!

Jay
  • 10,200
  • 4
  • 28
  • 36

5 Answers5

11

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:

ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = "Test.config";
Configuration newConfiguration = ConfigurationManager.OpenMappedExeConfiguration(
    fileMap,
    ConfigurationUserLevel.None);

ConfigurationChannelFactory<ICalculatorChannel> factory1 = 
    new ConfigurationChannelFactory<ICalculatorChannel>(
        "endpoint1", 
        newConfiguration, 
        new EndpointAddress("http://localhost:8000/servicemodelsamples/service"));
ICalculatorChannel client1 = factory1.CreateChannel();

As pointed out by Langdon, you can use the endpoint address from the configuration file by simply passing in null, like this:

var factory1 = new ConfigurationChannelFactory<ICalculatorChannel>(
        "endpoint1", 
        newConfiguration, 
        null);
ICalculatorChannel client1 = factory1.CreateChannel();

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 a ChannelFactory and manages the IChannelFactory for you.

Pablo Cibraro shows a nice example of this here: Getting WCF Bindings and Behaviors from any config source

Philippe
  • 4,088
  • 4
  • 44
  • 49
  • Philippe - I found Option 1 rather easy to use, but could you advice something more on Option2. All what I've found is number of unsupported @generators@ that are using Reflection, and don't cover 100% of possible configuration options. – Georgy Smirnov Feb 27 '14 at 05:09
  • @GeorgySmirnov, did you see the link to http://weblogs.asp.net/cibrax/archive/2010/05/11/getting-wcf-bindings-and-behaviors-from-any-config-source.aspx in option 2? It shows a very good example. – Philippe Feb 27 '14 at 14:50
  • Option 1 is sweet -- too bad ConfigurationChannelFactory's Dispose method is just as broken as ChannelFactory's, only this time the class is sealed so you can't extend it to make a sane version. – Medinoc Jul 02 '19 at 09:13
5

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:

<system.serviceModel>
   <bindings configSource="bindings.config" />
   <behaviors configSource="behaviors.config" />
   <client configSource="client.config" />
   <services configSource="services.config" />
  .....
</system.serviceModel>

So for each section inside <system.serviceModel>, you could specify an external config file by using the configSource= 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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • aye this is perfect! I was doing this for connectionStrings for some environment CI/CD purposes for some legacy apps. didn't know this applies to other sections. I wish more supported "file=" for external files zz – Poat Mar 26 '22 at 02:18
1

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.

Drew Marsh
  • 33,111
  • 3
  • 82
  • 100
0

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.

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
  • 1
    Welcome to Stack Overflow! Thanks for your post! Please do not use signatures/taglines in your posts. Your user box counts as your signature, and you can use your profile to post any information about yourself you like. [FAQ on signatures/taglines](http://stackoverflow.com/faq#signatures) – Andrew Barber Feb 21 '13 at 01:07
0

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?

Community
  • 1
  • 1
Ron Savage
  • 10,923
  • 4
  • 26
  • 35