WCF服务仅接收65536 String.Length大小,不大于

时间:2018-06-25 14:41:24

标签: c# wcf app-config servicecontract operationcontract

我创建了一个WCF服务,该服务将String(Text / Json)作为请求接收,但是问题是它只能接收65536(String.Length)的字符串长度。我在下面尝试通过谷歌搜索(绑定maxReceivedMessageSize =“ 2147483647”),但没有任何变化,它只能接收65536的String.Length大小,仅此而已。我是dotNet和WCF一词的新手,有人可以帮助我向该服务发送大数据(我想向WCF服务发送大约10MB的数据)。

   My Server Config:

   <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.serviceModel>
        <services>
          <service name="[my-service-name]">
            <endpoint address="http://localhost:8005/MyService"
                  binding="webHttpBinding"
                  contract="[my-service-contract-name]"/>
          </service>
        </services>
        <bindings>
          <basicHttpBinding>
            <binding maxReceivedMessageSize="2147483647"
            maxBufferSize="2147483647"
            maxBufferPoolSize="2147483647">
              <readerQuotas maxDepth="32"
              maxArrayLength="2147483647"
              maxStringContentLength="2147483647"/>
            </binding>
          </basicHttpBinding>
        </bindings>
        <behaviors>
          <serviceBehaviors>
            <behavior>
              <serviceMetadata httpGetEnabled="true"/>
              <serviceDebug includeExceptionDetailInFaults="true"/>
              <dataContractSerializer maxItemsInObjectGraph="2147483647" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
      </system.serviceModel>
      <startup> 
          <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
      </startup>
    </configuration>


    My Service:

        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "MyService")]
        void MyService(String input)
        {
            Console.WriteLine("Request data = " + input.Length);
        }



Client Config:    

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
    </startup>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="name-here" maxReceivedMessageSize="2147483647">
        </binding>
      </basicHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="behavior-here">
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

2 个答案:

答案 0 :(得分:1)

服务的

binding属性设置为webHttpBinding,并且在basicHttpBinding下添加了诸如maxReceivedMessageSize之类的属性。我相信这只是默认的webHttpbinding属性。我建议在您的httpBinding下创建一个绑定,并在新的绑定下进行所有所需的更改maxReceivedMessageSize,并将您的服务与绑定名称绑定在一起,如下所示:                                                            

  <basicHttpBinding>
    <binding name="testMessageBinding" maxReceivedMessageSize="30000000" openTimeout="00:00:02" receiveTimeout="00:00:02" sendTimeout="00:00:02" closeTimeout="00:00:02">
          <readerQuotas maxDepth="32"
          maxArrayLength="2147483647"
          maxStringContentLength="2147483647"/>
    </binding>
  </basicHttpBinding>

希望这会有所帮助,如果您没有查看链接,请阅读下面的链接以获取有关自定义绑定,系统提供的更多知识

https://docs.microsoft.com/en-us/dotnet/framework/wcf/system-provided-bindings

答案 1 :(得分:0)

In your config, please add
"<dataContractSerializer maxItemsInObjectGraph="2147483647"/>"

Service Config:
<behaviors>
  <serviceBehaviors>
    <behavior>
      <serviceMetadata httpGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="true"/>
      <dataContractSerializer maxItemsInObjectGraph="2147483647" />
    </behavior>
  </serviceBehaviors>
</behaviors>

Client Config:
<behaviors>
  <serviceBehaviors>
    <behavior name="MyServiceBehavior">
      <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
    </behavior>
  </serviceBehaviors>
</behaviors>