如何在wcf休息服务中增加maxquerystringlength和maxquerylength?

时间:2015-04-18 07:14:16

标签: wcf

我已经在wcf服务web配置文件中编写了这段代码,我使用这个服务从android应用程序通信我的sql server,我使用httppost方法从android应用程序发送请求,但它显示的查询字符串超出了长度。我们可以通过使用任何其他属性来增加长度,因为我想发送更大的输入,以便有机会发送,否则我必须限制我的查询字符串

      <?xml version="1.0" encoding="utf-8" ?>
        <configuration>
        <system.web>
                 <httpRuntime maxQueryStringLength="2097151" maxUrlLength="2097151"/>
            </system.web>
        <connectionStrings >
            <add name="DBConnection" connectionString="Integrated Security=true;Data Source=LENOVO\SQLEXPRESS;Initial Catalog=Order160415" providerName="System.Data.SqlClient"/>
          </connectionStrings>
          <system.serviceModel>
            <services>
              <service name="JsonWcfService.GetEmployees" behaviorConfiguration="EmpServiceBehaviour">
                <endpoint address ="" binding="webHttpBinding" contract="JsonWcfService.IGetEmployees" behaviorConfiguration="web">
                </endpoint>
                <host>
                  <baseAddresses>
                    <add baseAddress="http://localhost/" />
                  </baseAddresses>
                </host>
              </service>
            </services>

            <behaviors>
              <serviceBehaviors>
                <behavior name="EmpServiceBehaviour">
                  <serviceMetadata httpGetEnabled="true"/>
                  <serviceDebug includeExceptionDetailInFaults="true"/>
                </behavior>
              </serviceBehaviors>
              <endpointBehaviors>
                <behavior name="web">
                  <webHttp/>
                </behavior>
              </endpointBehaviors>
            </behaviors>
          </system.serviceModel>
         <system.webServer>
           <security>
             <requestFiltering>
               <requestLimits maxAllowedContentLength="2147483647" />
             </requestFiltering>
           </security>
          </system.webServer>
        </configuration>   

1 个答案:

答案 0 :(得分:0)

您可以为webHttpBinding定义绑定配置,并将其分配给端点,就像任何其他绑定一样。为此,请在配置文件中的<system.serviceModel>部分添加以下内容:

<bindings>
  <webHttpBinding>
    <binding name="MyWebHttpBinding" 
             maxBufferPoolSize="2097151"
             maxBufferSize="2097151"
             maxReceivedMessageSize="2097151">
      <readerQuotas maxStringContentLength="2097151" />
    </binding>
  </webHttpBinding>
</bindings>

我将值设置为您在发布配置中的<httpRuntime>元素中设置的值。

最后一步是使用bindingConfiguration元素中的service属性将此绑定配置分配给您的服务端点:

<endpoint address="" 
          binding="webHttpBinding" 
          bindingConfiguration="MyWebHttpBinding"
          contract="JsonWcfService.IGetEmployees"
          behaviorConfiguration="web">

总结一下:

  1. 使用您想要的值在web.config文件中创建绑定配置定义。
  2. 将绑定配置定义(使用name属性)分配给bindingConfiguration元素中的<service>属性。
  3. 如果没有绑定配置定义(及其对端点的分配),WCF将使用指定绑定协议的默认值。在这种情况下,maxBufferSizemaxBufferPoolSize的默认值为524,288,maxReceivedMessageSize的默认值为65,536,maxStringContentLength的默认值为8,192,均小于您设置的2,097,151限制<httpRuntime>元素。

    这些设置特定于WCF服务(或客户端,如果它位于客户端配置中),因此即使IIS接受较大的消息,如果超过绑定定义的限制,WCF也会拒绝它。

    有关详细信息,请参阅MSDN上的