ConfigSource attribute on system.serviceModel section

本文介绍如何在.NET Framework中使用configSource属性来指定配置文件的不同部分,实现环境间配置的灵活切换,并提供具体示例。

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'

What I found out is that 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>
    <servicesconfigSource="Services.config" >
    </services>
    <bindingsconfigSource="Bindings.config">
    </bindings>
  <behaviorsconfigSource="Behaviors.config">
    </behaviors>
  </system.serviceModel>
</configuration>

 And then, each file contains the corresponding section.

Services.config

<services>

  <servicename="Microsoft.ServiceModel.Samples.CalculatorService"

          behaviorConfiguration="CalculatorServiceBehavior">

    <host>

      <baseAddresses>

        <addbaseAddress="http://localhost:8000/servicemodelsamples/service"/>

      </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-->

    <endpointaddress="mex"

              binding="mexHttpBinding"

              contract="IMetadataExchange" />

  </service>

</services>


Bindings.config

<bindings>

  <netTcpBinding>

    <bindingname="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">

      <readerQuotasmaxDepth="32"

                    maxStringContentLength="8192"

                    maxArrayLength="16384"

                    maxBytesPerRead="4096"

                    maxNameTableCharCount="16384" />

      <reliableSessionordered="true"

                      inactivityTimeout="00:10:00"

                      enabled="false" />

      <securitymode="Transport">

        <transportclientCredentialType="Windows"protectionLevel="EncryptAndSign" />

      </security>

    </binding>

  </netTcpBinding>

</bindings>


Behaviors.config

<behaviors>

  <serviceBehaviors>

    <behaviorname="CalculatorServiceBehavior">

      <serviceMetadatahttpGetEnabled="true" />

      <serviceDebugincludeExceptionDetailInFaults="False" />

    </behavior>

  </serviceBehaviors>

</behaviors>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值