WCF+Restfull服務 提交或獲取數據時數據大小限制問題解決方案

   近日在使用wcf的restfull架構服務時遭遇到了提交大數據的問題。web

    大數據包含兩種情形:json

    1)單條數據量過大。restful

    2)提交或獲取的數據條數過多。架構

    在測試時發現,默認設置下當單條JSON數據大於30K時服務便不予受理。app

    提交或獲取數據大小的限制來自兩方面,即IIS服務WCF服務ide

    這兩方面的限制均可以經過配置WCF服務端Web.config相關配置節點的方式解決。測試

    廢話不說了,直接上解決方案。大數據

  • 未配置的原始Web.config 
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="BaseConnectionString" connectionString="server=.\SQLEXPRESS;database=TLPBizF;uid=*******;pwd==*******;" providerName="System.Data.SqlClient" />
    <add name="BizDBConnectionString" connectionString="server=.\SQLEXPRESS;database=TLPBizDB;uid==******;pwd==*******;" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <appSettings>
    <add key="TimeOutMinutes" value="20" />
    <add key="BizDBName" value="TLPBizDB"/>
    <add key="aspnet:MaxJsonDeserializerMembers" value="1500000000" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web> 

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <handlers accessPolicy="Read, Execute, Script" />
    <staticContent>
      <mimeMap fileExtension=".svc" mimeType="application/octet-stream" />
    </staticContent>
  </system.webServer>

</configuration>

 

  • 已配置的Web.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="BaseConnectionString" connectionString="server=.\SQLEXPRESS;database=TLPBizF;uid=*******;pwd==*******;" providerName="System.Data.SqlClient" />
    <add name="BizDBConnectionString" connectionString="server=.\SQLEXPRESS;database=TLPBizDB;uid==*******;;pwd==*******;" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <appSettings>
    <add key="TimeOutMinutes" value="20" />
    <add key="BizDBName" value="TLPBizDB"/>
    <add key="aspnet:MaxJsonDeserializerMembers" value="1500000000" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <httpRuntime maxRequestLength="2147483644"/>
  </system.web>
  <system.web.extensions>
    <scripting>
      <webServices>
        <jsonSerialization maxJsonLength="2147483644"/>
      </webServices>
    </scripting>
  </system.web.extensions>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
        </behavior>
        <behavior name="BigDataServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    <standardEndpoints>
      <webHttpEndpoint>
        <!-- 服務節點配置 -->
        <standardEndpoint name="BigDataServiceEndPoint" 
                          transferMode="Buffered" 
                          maxBufferSize="2147483647" 
                          maxReceivedMessageSize="2147483647" 
                          defaultOutgoingResponseFormat="Json" 
                          helpEnabled="true" 
                          automaticFormatSelectionEnabled="true">
          <readerQuotas maxDepth="64" 
                        maxNameTableCharCount="2147483647" 
                        maxStringContentLength="2147483647" 
                        maxBytesPerRead="2147483647" 
                        maxArrayLength="2147483647"
                       ></readerQuotas>
        </standardEndpoint>
      </webHttpEndpoint>

    </standardEndpoints>
    <services>
      <!-- 服務對應配置 -->
      <service name="SFiresoft.TLP.Services.BizCoreService" behaviorConfiguration="BigDataServiceBehavior">
        <endpoint endpointConfiguration="BigDataServiceEndPoint" 
                  kind="webHttpEndpoint" 
                  contract="SFiresoft.TLP.Services.IBizCoreService"
                  >
        </endpoint>
      </service>
    </services>
  </system.serviceModel>

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <handlers accessPolicy="Read, Execute, Script" />
    <staticContent>
      <mimeMap fileExtension=".svc" mimeType="application/octet-stream" />
    </staticContent>
  </system.webServer>

</configuration>

對比:ui

1)system.web節點:  spa

<httpRuntime maxRequestLength="2147483644"/>

應對IIS服務請求數據大小限制的設置。

2)system.serviceModel節點下「webHttpEndpoint」的配置:

<standardEndpoint 
          name="MyPoint" 
          transferMode="Buffered" 
          maxBufferSize="2147483647" 
          maxReceivedMessageSize="2147483647" 
          defaultOutgoingResponseFormat="Json" 
          helpEnabled="false" 
          automaticFormatSelectionEnabled="true">
          <readerQuotas 
            maxDepth="64" 
            maxNameTableCharCount="2147483647" 
            maxStringContentLength="2147483647" 
            maxBytesPerRead="2147483647" 
            maxArrayLength="2147483647"
                       ></readerQuotas>
        </standardEndpoint>

此處:

  • name能夠隨意取。
  • 數據大小設置部分不說了。
  • helpEnabled屬性:設置爲true時則能夠在服務URL後+/help的方式查看服務列表。

      如服務地址:http://localhost:9900/MapService.svc

      查看服務方式:http://localhost:9900/MapService.svc/help   

         以下圖:
服務列表說明

3)system.serviceModel節點下「service」的配置:

<services>
      <service name="SFiresoft.TLP.Services.BizCoreService" behaviorConfiguration="Wcf4BigData.Web.BigDataServiceBehavior">
        <endpoint endpointConfiguration="MyPoint" kind="webHttpEndpoint" contract="SFiresoft.TLP.Services.IBizCoreService">
        </endpoint>
      </service>
    </services>    
  • Service name設置同實現服務的類名一致。
  • behaviorConfiguration 內容與behavior節點中的相應名稱一致。
  • 此處Endpoint節點中contract要和描述服務結構的接口名一致。

其餘的很少說了自悟。

着重參考:《已配置的Web.config》

南京酷得軟件- 陳朕

相關文章
相關標籤/搜索