.net assemblies and individual configuration

2019-03-04 03:39发布

问题:

My primary assembly is a web application. It uses spring.net framework to load other assemblies into its executing app domain. So naturally this means all those other assemblies must get their config values (for e.g. connection strings & app settings) from the web.config.

Is there a way I can override this? Some of the configurations can come from the web.config; others must reside separately. What kind of an approach should I take in developing such .net assemblies? The goal is, once this application goes into the client environment, and the client decides to add assemblies somewhere in the distant future, he should be able to do so without hassles, and change the configuration for that assembly without affecting the others.

So ultimately I must come out with a kind of framework-based approach? Is there already something to this effect? Or how do I go about this? What are the challenges I'd be faced with?

回答1:

The web config file allows you to create custom assembly specific configuration sections.

Check out http://msdn.microsoft.com/en-us/library/2tw134k3.aspx for details

Additionally you can specify other files as configuration files to be used and parsed by the configuration parser in .net

You can do this using the config source option in your config files: e.g.

in App.config/Web.config

<configuration>
  <system.serviceModel>
    <services configSource="Services.config" >
    </services> 
  </system.serviceModel>
</configuration>

Create a file called Services.config and in it:

<services>
  <service name="SomeNameSpace.MyService"
           behaviorConfiguration="MyServiceBehaviour">
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:1234/service"/>
      </baseAddresses>
    </host>
    <endpoint address="net.tcp://localhost:8000/service"
              binding="netTcpBinding"
              bindingConfiguration="Binding1"
              contract="SomeNameSpace.IService" />
  </service>
</services>