近日在使用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的方式查看服务列表。
如服务地址: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》
南京酷得软件- 陈朕