Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在服务和客户端之间共享WCF设置_Wcf_Configuration - Fatal编程技术网

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

在服务和客户端之间共享WCF设置,wcf,configuration,Wcf,Configuration,WCF服务和客户端是否可以共享关于绑定等的相同设置(来自同一配置文件)?换句话说,我可以编写一个绑定部分并放入任何内容,确保它对服务和客户机都有好处吗 我会解释得更清楚。 我有这样一个配置文件: <services> <service name="TestClass1"> <endpoint binding="basicHttpBinding" address="http://dev00:4322/host1/TestApplication1" cont

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>


不是所有的事情都在我的控制之下。
我能确保在服务和客户机之间共享绑定(以及其他部分…)吗?无论编写什么,都能在服务和客户机中顺利进行?

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

  • 将绑定、行为和扩展信息放入单独的配置文件中
  • 从应用程序的客户端和服务器部分引用这些内容
例如,将绑定放入
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>

VisualStudio会抱怨“configSource”-但相信我,它是有效的。这是用于验证的VisualStudioXML模式中的一个缺陷,但该功能可以正常工作。这实际上适用于web.config/app.config中的任何配置节(但不适用于配置节组)

您可以为
配置组的任何“子部分”执行此操作-客户机、服务器、行为、扩展,您可以随意命名

马克是的。下面是我使用的(简化的)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>

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

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