欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

在web.config里使用configSource分隔各类配置

程序员文章站 2022-06-25 15:53:02
configsource简介一: 在web.config 中:

configsource简介一:
在web.config 中:

<myconfig  configsource="path\xxx.config" />


在自定义path\xxx.config中:

<myconfig>
    <mmm> dd </mmm>
</myconfig>

注意,configsouce中的文件路径只能为相对物理路径,也就是只能为反斜杠(\),不能用斜杠(/)。
在网站运行时,如果修改 web.config 文件会引起站点的重启,而修改 xxx.config 文件则不会.

 
configsource简介二:
大型项目中,可能有多个service,也就是会有一堆配置,而这些配置文件如果散落在各个项目的web.config里,那维护量将是非常巨大的,所以有人使用configsource将各咱文件分开放,然后copy到各处,这样以来配置文件看起来就很模块化了,但这样copy维护起来还是很麻烦,所以有高人提出公共配置的概念,将这些配置文件放到一个公用目录,然后在各个目录下建立一个虚拟文件夹指向那个公共目录(junctionhttp://technet.microsoft.com/en-us/sysinternals/bb896768.x,如想了解更多可访问:多站点共享:共享配置)天下太平了,要添加修改只用改这一个文件夹的内容就可以了。

我们用到的有:

 

    <appsettings configsource="vconfigs\dev\appsettings.config" />     <system.servicemodel>
      <services  configsource="vconfigs\dev\wcf.services.config" />
      <bindings  configsource="vconfigs\dev\wcf.bindings.config" />
      <behaviors configsource="vconfigs\dev\wcf.behaviors.config" />
      <client    configsource="vconfigs\dev\wcf.client.config" />
    </system.servicemodel>参考内容:configsource attribute on system.servicemodel section
the configsource attribute was firstly introduced in .net framework 2.0 to support external configuration files.

this attribute can be added to any configuration section to specify a an external file for that section. using an external configuration source can be useful in many scenarios. for instance, you could place a section into an external configsource if you need an easy method to swap settings for the section depending on the environment (development, test, or production), or  you need granular control over permissions.

unfortunately, the system.servicemodel section group does not support this attribute. if you try to add it, you will receive the following exception:

the attribute 'configsource' cannot be specified because its name starts with the reserved prefix 'config' or 'lock'

"configsource" attribute is a property comes from the  section information class.
every configurationsection in .net framework 2.0 configuration scheme system
has a property of a section information where the config source is declared.

 you can use this attribute on the different sections under system.servicemodel such as services, behaviors or bindings.

for instance, the configuration file could look like this,

<configuration>

  <system.servicemodel>

    <services configsource="services.config" />


    <bindings configsource="bindings.config" />


    <behaviors configsource="behaviors.config" />


  </system.servicemodel>

</configuration>

 and then, each file contains the corresponding section.

services.config

<services>

  <service name="microsoft.servicemodel.samples.calculatorservice"

          behaviorconfiguration="calculatorservicebehavior">

    <host>

      <baseaddresses>

        <addbaseaddress=">

      </baseaddresses>

    </host>

    <!-- this endpoint is exposed at: net.tcp://localhost:9000/servicemodelsamples/service  -->

    <endpointaddress="net.tcp://localhost:9000/servicemodelsamples/service"

              binding="nettcpbinding"

              bindingconfiguration="binding1"

             contract="microsoft.servicemodel.samples.icalculator" />

    <!-- the mex endpoint is exposed at http://localhost:8000/servicemodelsamples/service/mex -->

    <endpoint address="mex"

              binding="mexhttpbinding"

              contract="imetadataexchange" />

  </service>

</services>

bindings.config

<bindings>

  <nettcpbinding>

    <binding name="binding1"

            closetimeout="00:01:00"

            opentimeout="00:01:00"

            receivetimeout="00:10:00"

            sendtimeout="00:01:00"

            transactionflow="false"

            transfermode="buffered"

            transactionprotocol="oletransactions"

            hostnamecomparisonmode="strongwildcard"

            listenbacklog="10"

            maxbufferpoolsize="524288"

            maxbuffersize="65536"

            maxconnections="10"

            maxreceivedmessagesize="65536">

      <readerquotas maxdepth="32"

                    maxstringcontentlength="8192"

                    maxarraylength="16384"

                    maxbytesperread="4096"

                    maxnametablecharcount="16384" />

      <reliablesession ordered="true"

                      inactivitytimeout="00:10:00"

                      enabled="false" />

      <security mode="transport">

        <transport clientcredentialtype="windows"protectionlevel="encryptandsign" />

      </security>

    </binding>

  </nettcpbinding>

</bindings>

behaviors.config

<behaviors>

  <servicebehaviors>

    <behavior name="calculatorservicebehavior">

      <servicemetadata httpgetenabled="true" />

      <servicedebug includeexceptiondetailinfaults="false" />

    </behavior>

  </servicebehaviors>

</behaviors>

 


摘自 小草旁的大树