1. 程式人生 > >WCF+Restfull服務 提交或獲取資料時資料大小限制問題解決方案

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

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

    大資料包含兩種情形:

    1)單條資料量過大。

    2)提交或獲取的資料條數過多。

    在測試時發現,預設設定下當單條JSON資料大於30K時服務便不予受理。

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

    這兩方面的限制都可以通過配置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>

對比:

1)system.web節點:

<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的方式檢視服務列表。

         如下圖:
服務列表說明

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》