我可以将system.serviceModel拆分为单独的.config文件吗?

Mr *_*ell 20 wcf web-config asp.net-3.5

我想将web.config的system.serviceModel部分分成一个单独的文件,以方便一些环境设置.我的努力没有结果.当我尝试使用这种方法时.wcf代码抛出一个异常:"'System.ServiceModel.ClientBase的类型初始值设定项1引发了异常.任何人都可以告诉我我做错了什么?

Web.config文件:

<configuration>
  <system.serviceModel configSource="MyWCF.config" />
  ....
Run Code Online (Sandbox Code Playgroud)

MyWCF.config:

  <system.serviceModel>
    <extensions>
      ...
    </extensions>

    <bindings>
      ...
    </bindings>

    <behaviors>
      ...
    </behaviors>

    <client>
       ...
    </client>

  </system.serviceModel>
Run Code Online (Sandbox Code Playgroud)

mar*_*c_s 36

你不能"外化" <system.serviceModel>节组 - 因为它是一个配置节 - 但你绝对可以外化其中的每个位:

<system.serviceModel>
    <behaviors configSource="behaviors.config" />
    <bindings configSource="bindings.config" />
    <extensions configSource="extensions.config" />
    <client configSource="client.config" />
    <services configSource="services.config" />
</system.serviceModel>
Run Code Online (Sandbox Code Playgroud)

在.NET配置系统中,任何配置部分都可以外部化 - 每个配置部分都有一个configSource属性(即使Visual Studio有时会抱怨并声称相反......) - 但不是配置部分组.

不幸的是,这两者很难区分 - 您需要查阅MSDN库或文档才能找到答案.

您还应该在CodeProject上查看Jon Rista关于.NET配置系统的三部分系列.

强烈推荐,写得很好,非常有帮助!