电竞比分网-中国电竞赛事及体育赛事平台

分享

WCF+Restfull服務(wù) 提交或獲取數(shù)據(jù)時(shí)數(shù)據(jù)大小限制問題解決方案

 昵稱10504424 2013-07-11
近日在使用wcf的restfull架構(gòu)服務(wù)時(shí)遭遇到了提交大數(shù)據(jù)的問題。

    大數(shù)據(jù)包含兩種情形:

    1)單條數(shù)據(jù)量過大。

    2)提交或獲取的數(shù)據(jù)條數(shù)過多。

    在測試時(shí)發(fā)現(xiàn),默認(rèn)設(shè)置下當(dāng)單條JSON數(shù)據(jù)大于30K時(shí)服務(wù)便不予受理。

    提交或獲取數(shù)據(jù)大小的限制來自兩方面,即IIS服務(wù)WCF服務(wù)。

    這兩方面的限制都可以通過配置WCF服務(wù)端Web.config相關(guān)配置節(jié)點(diǎn)的方式解決。

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

  • 未配置的原始Web.config 
復(fù)制代碼
<?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>
復(fù)制代碼

 

  • 已配置的Web.config
復(fù)制代碼
<?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>
<!-- 服務(wù)節(jié)點(diǎn)配置 -->
<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>
<!-- 服務(wù)對應(yīng)配置 -->
<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>
復(fù)制代碼

對比:

1)system.web節(jié)點(diǎn):  

<httpRuntime maxRequestLength="2147483644"/>

應(yīng)對IIS服務(wù)請求數(shù)據(jù)大小限制的設(shè)置。

2)system.serviceModel節(jié)點(diǎn)下“webHttpEndpoint”的配置:

復(fù)制代碼
<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>
復(fù)制代碼

此處:

  • name可以隨意取。
  • 數(shù)據(jù)大小設(shè)置部分不說了。
  • helpEnabled屬性:設(shè)置為true時(shí)則可以在服務(wù)URL后+/help的方式查看服務(wù)列表。

      如服務(wù)地址:http://localhost:9900/MapService.svc

      查看服務(wù)方式:http://localhost:9900/MapService.svc/help   

         如下圖:
服務(wù)列表說明

3)system.serviceModel節(jié)點(diǎn)下“service”的配置:

復(fù)制代碼
<services>
<service name="SFiresoft.TLP.Services.BizCoreService" behaviorConfiguration="Wcf4BigData.Web.BigDataServiceBehavior">
<endpoint endpointConfiguration="MyPoint" kind="webHttpEndpoint" contract="SFiresoft.TLP.Services.IBizCoreService">
</endpoint>
</service>
</services>    
復(fù)制代碼
  • Service name設(shè)置同實(shí)現(xiàn)服務(wù)的類名一致。
  • behaviorConfiguration 內(nèi)容與behavior節(jié)點(diǎn)中的相應(yīng)名稱一致。
  • 此處Endpoint節(jié)點(diǎn)中contract要和描述服務(wù)結(jié)構(gòu)的接口名一致。

    本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多