Overriding the Default Configuration File

How do I override the default configuration file?

There's a number of different answers to this question depending on what you really intend to do with the configuration. Most of the time, the answer is a little harder than you would expect unless you've worked with configuration in the past.

The default configuration file for your application is based on the executable name and location. If what you want to do is have your application use a different configuration file, then that is mostly straightforward. You can create your own application domain and use the AppDomainSetup to point at the desired application configuration file. Your service runs in that application domain and gets the right configuration.

Next, you may want your application to use the standard configuration file but override some of the settings. There are two options for this. The first option is to override ApplyConfiguration in ServiceHost. After calling the base ApplyConfiguration method, you have the default configuration loaded. You can do whatever additional processing you want at that point. The second option is to rewrite the configuration using the ConfigurationManager class. This is a part of the standard configuration package and doesn't know anything about WCF. That means you have to be knowledgeable yourself in how WCF configuration is laid out.

Finally, you may want your application to not use a file-based configuration system at all. For instance, your configuration may be stored in a database or dynamically generated. In this case, you still need to override ApplyConfiguration, but this time you're not going to call the base ApplyConfiguration method. Instead, you have to apply the configuration from scratch using the external source. I don't know of an easy way to parse a standard WCF configuration file in memory for your custom ApplyConfiguration method. If you can write a temporary configuration file to disk, then you can alternatively just use the first method of creating a new application domain. Otherwise, you need to have a parser for your external configuration file format.

Next time: Incompatible Addressing with Reliable Messaging