无法通过添加服务引用将WCF主机配置发送到客户端

时间:2012-03-14 12:42:05

标签: wcf configuration default wcf-binding nettcpbinding

我准备了一个wcf服务主机,并在下面配置了一些绑定信息。

<system.serviceModel>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  <standardEndpoints>
    <webHttpEndpoint>
    <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" />
    </webHttpEndpoint>
  </standardEndpoints>
  <bindings>
    <netTcpBinding>
    <binding name="NetTcpBinding" closeTimeout="00:02:00" openTimeout="00:02:00"
      receiveTimeout="00:20:00" sendTimeout="00:02:00" transactionFlow="true"
      transferMode="Buffered" maxBufferPoolSize="2147483647" 
      maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
        maxArrayLength="2147483647" maxBytesPerRead="2147483647" 
        maxNameTableCharCount="2147483647" />
      <security mode="None">
      <transport protectionLevel="None" />
      </security>
    </binding>
    </netTcpBinding>
  </bindings>
  <behaviors>
    <serviceBehaviors>
    <behavior name="tcpServiceBehavior">
      <serviceMetadata httpGetEnabled="false" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
    </serviceBehaviors>
  </behaviors>
  <services>
    <service behaviorConfiguration="tcpServiceBehavior" name="MyClass">
    <endpoint address="" binding="netTcpBinding" bindingConfiguration="NetTcpBinding" contract="IMyClass">
      <identity>
      <dns value="localhost" />
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
    <host>
      <baseAddresses>
      <add baseAddress="net.tcp://localhost:8732/Design_Time_Addresses/MyService/MyClass" />
      </baseAddresses>
    </host>
    </service>
  </services>
</system.serviceModel>

使用以下代码进行实例化:

ServiceHost wcfHost = new ServiceHost(typeof(MyClass));
wcfHost.Open();

使用QuickWatch检查并验证配置值。然后,我将“net.tcp:// localhost:8732 / Design_Time_Addresses / MyService / MyClass”上的服务引用添加到另一个项目中。因此,下面的新配置文件已在客户端项目中创建。

<system.serviceModel>
  <bindings>
    <netTcpBinding>
      <binding name="NetTcpBinding_IMyClass" closeTimeout="00:01:00"
        openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
        transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions"
        hostNameComparisonMode="StrongWildcard" listenBacklog="10"
        maxBufferPoolSize="524288" maxBufferSize="65536" maxConnections="10"
        maxReceivedMessageSize="65536">
        <readerQuotas maxDepth="32" maxStringContentLength="8192"
               maxArrayLength="16384" maxBytesPerRead="4096"
               maxNameTableCharCount="16384" />
        <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" />
        <security mode="None">
          <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
          <message clientCredentialType="Windows" />
        </security>
      </binding>
    </netTcpBinding>
  </bindings>
  <client>
    <endpoint address="net.tcp://localhost:8732/Design_Time_Addresses/MyService/MyClass" binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IMyClass" contract="IMyClass" name="NetTcpBinding_IMyClass">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
  </client>
</system.serviceModel>

问题是主机的绑定配置无法到达客户端。如果我更改客户端配置,它不会影响。例如,将 maxStringContentLength 值从 8192 更改为 2147483647 ,然后,看到该客户端已使用正确的配置进行实例化,但是使用旧配置进行操作。在解决方案中搜索“ 8192 ”,并在 .svcinfo 文件中找到XML方案。我怎样才能解决这种奇怪的情况?

1 个答案:

答案 0 :(得分:0)

右键单击服务引用并“更新”它,VS将重新生成svcinfo文件,或者您可以完全避免此问题,不使用VS创建服务代理,而是手动生成自己的服务代理(被认为是据我所知,最好的做法)。正如您已经指出的那样,“添加服务引用”不够智能,无法在创建客户端时从服务中获取所有这些配置值。