wcf - 为什么我的 WCF 服务没有加载我的绑定(bind)配置?

标签 wcf wcf-binding wcf-client wcftestclient

我遇到了以下错误的问题:“ 已超出传入消息的最大消息大小配额 (65536)。要增加配额,请使用相应绑定(bind)元素上的 MaxReceivedMessageSize 属性。

所以我做了一些研究,发现我需要增加缓冲区和消息大小,这是我的 WCF 服务配置文件:

<system.serviceModel>
    <bindings>
      <wsHttpBinding>
          <binding name="default" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"/>
      </wsHttpBinding>
    </bindings>
    <services>
      <service name="WCF.Service.Service">
        <endpoint address="ws" name="ws" bindingConfiguration="default" binding="wsHttpBinding" contract="WCF.Service.Contracts.IService" />
        <endpoint address="mex" binding="mexHttpBinding" name="mex" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, 
          set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="True" />
          <!-- To receive exception details in faults for debugging purposes, 
          set the value below to true.  Set to false before deployment 
          to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="True" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

当我在 WCF 测试客户端中运行服务并查看生成的客户端配置文件时,有我的约束力:
<configuration>
<system.serviceModel>
    <bindings>
        <wsHttpBinding>
            <binding name="ws" closeTimeout="00:01:00" openTimeout="00:01:00"
                receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false"
                transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                allowCookies="false">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                    maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                <reliableSession ordered="true" inactivityTimeout="00:10:00"
                    enabled="false" />
                <security mode="Message">
                    <transport clientCredentialType="Windows" proxyCredentialType="None"
                        realm="" />
                    <message clientCredentialType="Windows" negotiateServiceCredential="true"
                        algorithmSuite="Default" />
                </security>
            </binding>
        </wsHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://localhost:37444/Service.svc/ws" binding="wsHttpBinding"
            bindingConfiguration="ws" contract="IService" name="ws">
            <identity>
                <userPrincipalName value="username@domain" />
            </identity>
        </endpoint>
    </client>
</system.serviceModel>

我不知道为什么我的绑定(bind)配置是 不是 被申请!? WCF 测试客户端也设置为始终重新生成配置。我也尝试在消费前端应用程序中更新服务引用,但它确实 不是 获取更新的绑定(bind)配置。任何建议将不胜感激。谢谢!

最佳答案

WCF 确实 不是 从您的服务器导入所有设置。也没有开关可以打开它。虽然在许多情况下可能有意义,但将所有设置从服务器端复制到客户端并不总是一个好主意。

因此,在您的情况下,您需要做的是将该绑定(bind)配置也添加到您的客户端代理,并从您的客户端端点引用它。

如果您控制线的两端,您可以稍微简化您的工作:将绑定(bind)配置外部化到一个单独的文件中并引用它。

所以创建一个文件bindings.config其中包含:

<?xml version="1.0" ?>
<bindings>
    <wsHttpBinding>
        <binding name="default" 
                 maxBufferPoolSize="2147483647" 
                 maxReceivedMessageSize="2147483647"/>
  </wsHttpBinding>
</bindings>

然后您可以将该文件复制到服务器和客户端项目中,并从您的服务和客户端配置中引用它:
<system.serviceModel>
    <bindings configSource="bindings.config" />
    <services>
      <service name="WCF.Service.Service">
        <endpoint address="ws" name="ws" bindingConfiguration="default" binding="wsHttpBinding" contract="WCF.Service.Contracts.IService" />
        <endpoint address="mex" binding="mexHttpBinding" name="mex" contract="IMetadataExchange" />
      </service>
    </services>

在您的客户端:
<system.serviceModel>
    <bindings configSource="bindings.config" />
    <client>
        <endpoint  name="ws"
            address="http://localhost:37444/Service.svc/ws" 
            binding="wsHttpBinding"
            bindingConfiguration="default" 
            contract="IService">
            <identity>
                <userPrincipalName value="username@domain" />
            </identity>
        </endpoint>
    </client>
</system.serviceModel>

这样,您可以一次性完成绑定(bind)配置,并在两个地方使用。

关于wcf - 为什么我的 WCF 服务没有加载我的绑定(bind)配置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4035846/

相关文章:

WCF 终结点配置错误 : The 'contract' attribute is invalid?

c# - 将 CDATA 与 WCF REST 初学者工具包一起使用

c# - WCF 找不到端点配置问题

node.js - NPM-node-soap 支持 WsHttpBinding 吗?

c# - 如何以编程方式将客户端连接到 WCF 服务?

c# - WCF 错误架构

c# - 在 Xamarin 跨平台应用程序中使用 WCF 服务

c# - "The creator of this fault did not specify a Reason"异常

wcf - 在发送请求之前需要帮助在我的 WCF 客户端中操作 SOAP header

WCF 代理连接打开导致错误