根据环境更改WCF服务引用URL

Ric*_*ett 7 .net c# wcf web-config

我有一个使用许多WCF服务的Web应用程序.我在各种环境(开发,UAT,生产等)中部署我的Web应用程序.每个WCF服务的URL对于每个环境都是不同的.我使用的是.NET 3.5和basicHttpBindings

Web应用程序使用框架在我的web.config文件中支持特定于计算机的设置.在实例化WCF服务客户端的实例时,我调用一个函数,该函数使用带有参数的构造函数重载来创建WCF服务客户端的实例:

System.ServiceModel.Channels.Binding binding,
System.ServiceModel.EndpointAddress remoteAddress
Run Code Online (Sandbox Code Playgroud)

实质上<system.serviceModel><bindings><basicHttpBinding><binding>,web.config中的配置已在C#代码中复制.

这种方法效果很好.

但是,我现在必须增强此方法以使用使用X509证书的WCF服务.这意味着我必须在C#代码中复制web.config中的以下附加设置:

<!-- inside the binding section -->
<security mode="Message">
  <transport clientCredentialType="None" proxyCredentialType="None" realm="" />
  <message clientCredentialType="Certificate" algorithmSuite="Default" />
</security>


<behaviors>
  <endpointBehaviors>
    <behavior name="MyServiceBehaviour">
      <clientCredentials>
        <clientCertificate storeLocation="LocalMachine" storeName="My"
          x509FindType="FindByThumbprint" findValue="1234abcd" />
        <serviceCertificate>
          <defaultCertificate storeLocation="LocalMachine" storeName="My"
            x509FindType="FindByThumbprint" findValue="5678efgh" />
          <authentication trustedStoreLocation="LocalMachine"
            certificateValidationMode="None" />
        </serviceCertificate>
      </clientCredentials>
    </behavior>
  </endpointBehaviors>
</behaviors>
Run Code Online (Sandbox Code Playgroud)

我在查找如何在C#中编写此配置时遇到一些困难.

两个问题

  • 任何人都可以推荐一种更好的方法来管理多个环境中的WCF服务参考URL吗?
  • 或者,有关如何在C#中复制上述web.config部分的任何建议都将受到欢迎

mar*_*c_s 5

一种可能的方法是将 <system.serviceModel> 配置的某些部分“外部化”到外部文件中,每个环境一个。

例如,我们有“bindings.dev.config”和“bindings.test.config”,然后我们在我们的主 web.config 中像这样引用它们:

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

这样,您需要从 DEV 更改为 PROD 的只是这一行配置 XML。

基本上,在 .NET 2.0 配置中,任何配置元素都可以“外部化”。但是,您不能直接将 configGroups(例如“system.serviceModel”)外部化 - 您必须处于“配置元素”级别。

马克

编辑:好的,所以没有配置编辑更改以在环境之间切换.....在这种情况下,您可能必须想出一个命名方案,例如以这样的方式命名您的绑定、行为和端点,以便您可以区分它们在运行时。

就像是:

<bindings>
  <binding name="Default_DEV">
    .....
  </binding>
  <binding name="Default_PROD">
    .....
  </binding>
</bindings>
Run Code Online (Sandbox Code Playgroud)

这样,你可以从你的代码和你正在运行的环境中建立你想要的元素的名称(例如绑定“Default_PROD”),然后从包含所有配置设置的配置文件中获取相应的配置所有环境。


Ric*_*ett 1

以下代码复制了我原来问题中的配置:

myClient.ClientCredentials.ClientCertificate.SetCertificate(
    StoreLocation.LocalMachine,
    StoreName.My,
    X509FindType.FindByThumbprint,
    "1234abcd");

myClient.ClientCredentials.ServiceCertificate.SetDefaultCertificate(
    StoreLocation.LocalMachine,
    StoreName.My,
    X509FindType.FindByThumbprint,
    "5678efgh");

myClient.ClientCredentials.ServiceCertificate.Authentication.TrustedStoreLocation = StoreLocation.LocalMachine;
myClient.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
Run Code Online (Sandbox Code Playgroud)

在生产代码中,两个指纹值存储在appSettingsweb.config 文件中。