在服务和客户端之间共享WCF设置

时间:2009-12-15 15:35:20

标签: wcf configuration

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>

并非所有人都在我的控制之下。 我可以确定在服务和客户端之间共享绑定(和其他部分..),无论写入什么,在服务和客户端都很顺利吗?

3 个答案:

答案 0 :(得分:4)

是的,你可以 - 在某种程度上:

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

即。将您的绑定放在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中的任何配置部分(但不适用于配置部分组)。

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

马克

答案 1 :(得分:0)

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

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

答案 2 :(得分:-1)

是。这是我使用的(简化的)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>