0

I am trying to swap configuration files in and out of my web api to interact with different WCF webservices. I found the solution to change my webconfig files at run time in the following link.

Config Swapper Class

This works great when called from a command line application substituting in my new configuration file but when i call the config file changer from my API I Get the following Error.

"Could not find default endpoint element that references contract 'OldDominionWebServices.RateDelegate' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element."

This is the endpoint I am trying to reference

<endpoint address="https://www.odfl.com:443/wsRate_v4/RateService" binding="basicHttpBinding" bindingConfiguration="RatePortBinding" contract="OldDominionWebServices.RateDelegate" name="OldDominionWebServices" />

I know what the error says, but am unsure as to why this would be thrown in a web api with the configuration file swapped, but not thrown when using a console application that does the same thing. In the case of the command line app the assembly that uses this web service is referenced as part of the solution, where as with the api the assembly is loaded via reflection. I have seen some suggestions that I need to use the Fully qualified domain name in the contract for the endpoint, but this still results in the same error.

I'm hoping this is something simple that I have overlooked, Thanks in advance!

Community
  • 1
  • 1
theDarse
  • 749
  • 5
  • 13
  • So you're trying to swap config file from inside your Web API code? – Ilya Luzyanin Jul 23 '14 at 14:54
  • That's correct yes, the config file is swapped out for the assembly specific config file for the duration of the call made by the assembly – theDarse Jul 23 '14 at 14:55
  • Could it be that you're using a relative path to new config and it could not be found at the moment of swapping? – Ilya Luzyanin Jul 23 '14 at 15:12
  • It's actually an absolute path. The problem seems to be that the config file does not actually get swapped out. I can put the endpoint into the API's web config file and it works, the problem is I want to avoid this since the system has some 400 different endpoints it can use – theDarse Jul 23 '14 at 15:16
  • Have you tried to debug it? What does AppDomain.CurrentDomain.GetData("APP_CONFIG_FILE") show? – Ilya Luzyanin Jul 23 '14 at 15:30
  • it shows the correct path to the added file, I'm wondering if i need to do an Appdomain reset after swapping the config file to reload it in IIS? – theDarse Jul 23 '14 at 16:30

1 Answers1

1

Good question!
Short answer:
You can't swap configurations, because web applications use a hierarchy of configuration files that is used in initialization when AppDomain starts, so this would require the restart of AppDomain, and all your in-memory changes would be lost (and this is how that "Config Swapper Class" assembly is intended to work - do everything in memory).

Details:
I did some digging and here is what I've found:

  1. It is not related to the way you load assembly with "Config Swapper Class" assembly, there is nothing there that could be affected by this.
  2. There is no point in restarting AppDomain if your actual configuration lives in memory - all changes would be lost. Probably you could think of another idea of where to store your configuration other than in web.config?
  3. The mentioned assembly (great stuff, BTW!) works in console application, because internally it uses ConfigurationManager, which itself uses ClientConfigurationSystem realization of IInternalConfigSystem interface. Web applications use WebConfigurationManager, that uses other realization of that interface - HttpConfigurationSystem.
    So, the "Config Swapper Class" assembly will not help you in web application because it operates on different set of internal classes.
Ilya Luzyanin
  • 7,910
  • 4
  • 29
  • 49