【问题标题】:Share WCF settings between service and client在服务和客户端之间共享 WCF 设置
【发布时间】:2010-12-26 20:46:57
【问题描述】:

WCF 服务和客户端是否可以共享有关绑定等的相同设置(来自相同的配置文件)等等,不管是什么?换句话说,我可以写一个单独的绑定部分并放入任何东西并确保它对服务和客户端都有好处吗?

我会解释得更好。 我有一个这样的配置文件:

<services>
  <service name="TestClass1">
    <endpoint binding="basicHttpBinding" address="http://dev00:4322/host1/TestApplication1" contract="myApp.Interface.ITestApplication"/>
    <endpoint binding="netTcpBinding" bindingConfiguration="Binding1" address="net.tcp://dev00:4321/host1/TestApplication1" contract="myApp.Interface.ITestApplication"/>
    <endpoint binding="netNamedPipeBinding" address="net.pipe://localhost/host1/TestApplication1" contract="myApp.Interface.ITestApplication"/>
  </service>

  <service name="ManagementClass1">
    <endpoint binding="netNamedPipeBinding" address="net.pipe://localhost/host1/ManagementApplication1" contract="myApp.Interface.IManagementApplication"/>
    <endpoint binding="netTcpBinding" bindingConfiguration="Binding1" address="net.tcp://dev00:4321/host1/ManagementApplication1" contract="myApp.Interface.IManagementApplication"/>
  </service>
</services>

<client>
  <endpoint name="clientTestClass1Tcp"
      address="net.tcp://dev00:4321/host1/TestApplication1"
      binding="netTcpBinding" 
      bindingConfiguration="Binding1" 
      contract="myApp.Interface.ITestApplication"/>

  <endpoint name="clientManagementClass1Tcp"
      address="net.tcp://dev00:4321/host1/ManagementApplication1"
      binding="netTcpBinding" 
      bindingConfiguration="Binding1" 
      contract="myApp.Interface.IManagementApplication"/>
</client>

<bindings>
  <netTcpBinding>
    <binding name="Binding1" 
         closeTimeout="00:00:10"
         openTimeout="00:00:10" 
         receiveTimeout="00:01:00" 
         sendTimeout="00:01:00"
         transactionFlow="false" 
         transferMode="Buffered" 
         transactionProtocol="OleTransactions"
         hostNameComparisonMode="StrongWildcard" 
         listenBacklog="10"
         maxBufferPoolSize="524288" 
         maxBufferSize="65536" 
         maxConnections="30"
         maxReceivedMessageSize="65536">
      <security mode="None">
        <transport clientCredentialType="None" />
      </security>
    </binding>
  </netTcpBinding>
</bindings>

并非一切都在我的控制之下。 我可以确定在服务和客户端之间共享绑定(和其他部分..),无论编写什么,在服务和客户端中都运行良好?

【问题讨论】:

    标签: wcf configuration


    【解决方案1】:

    如果您控制两端的代码,另一种选择是在代码中进行整个配置,除了您从自己的配置文件中读取的服务器名称。

    然后您可以在客户端和服务器中使用该程序集;当您已经使用共享程序集(而不是生成的代理类)来定义 WCF 接口时,这可以很好地工作。

    【讨论】:

      【解决方案2】:

      是的。这是我使用的(简化的)app.config 文件。

      <?xml version="1.0" encoding="utf-8" ?>
      <configuration>
          <system.serviceModel>
              <client>
                  <endpoint name="MyServiceClient"
                      address="net.pipe://localhost/MyService"
                      contract="IMyService"
                      binding="netNamedPipeBinding" />
              </client>
              <services>
                  <service name="MyService">
                      <endpoint name="MyService"
                          address="net.pipe://localhost/MyService"
                          contract="IMyService"
                          binding="netNamedPipeBinding" />
                  </service>
              </services>
          </system.serviceModel>
      </configuration>
      

      【讨论】:

        【解决方案3】:

        是的,你可以 - 在一定程度上:

        • 将您的绑定、行为、扩展信息放入单独的配置文件中
        • 引用您应用的客户端和服务器部分的内容

        即把你的绑定放在bindings.config:

        <?xml version="1.0" encoding="utf-8"?>
        <bindings>
          <basicHttpBinding>
            <binding name="Default" useDefaultWebProxy="false">
              <security mode="TransportCredentialOnly">
                <transport clientCredentialType="Basic" 
                           proxyCredentialType="None" realm="" />
              </security>
            </binding>
          </basicHttpBinding>
        </bindings>
        

        然后从您的服务的 app.config 或 web.config 引用该文件:

        <system.serviceModel>
            <bindings configSource="bindings.config" />
        </system.serviceModel>
        

        Visual Studio 会抱怨“configSource”——但相信我,它可以工作。这是用于验证的 Visual Studio XML 架构中的一个缺陷 - 但该功能有效。这实际上适用于 web.config / app.config 中的 any 配置部分(但不适用于配置部分组)。

        您可以对 &lt;system.serviceModel&gt; 配置组的任何“子部分”执行此操作 - 客户端、服务器、行为、扩展,您可以命名。

        马克

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-02-17
          • 2021-03-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多