Compartilhar via


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

Comments

  • Anonymous
    December 04, 2006
    We ask Microsoft about this as well.  We want to load the configuration from stream - why create the file? - and basically where told it was not do-able.

  • Anonymous
    December 04, 2006
    I wanted to host configuration information outside of the file - <a href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=512579&SiteID=1">in a policy store</a> - and couldn't find a simple way to do it. WCF knows all about config so why can we not pass it a valid config file and let it chew its way through it? Will this make it into .NET 3.01? Ciaran

  • Anonymous
    December 04, 2006
    HTTP namespace sharing allows you to build different pieces of your web site using different technologies.

  • Anonymous
    December 04, 2006
    Ciaran, It's too soon to say whether a feature of moderate size definitely will or won't be in the next release. Filing suggestions through Connect can help communicate what you want to see in the product. http://connect.microsoft.com/wcf/Feedback

  • Anonymous
    January 22, 2007
    Last time we were looking at the problem of securing a dynamically generated message header . We saw

  • Anonymous
    May 19, 2008
    In the past I've written about overriding ApplyConfiguration on a service to take control of the configuration