zoukankan      html  css  js  c++  java
  • 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的方式查看服务列表。

          如服务地址: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》

    南京酷得软件- 陈朕

  • 相关阅读:
    HDU 4832(DP+计数问题)
    mongodb安装与使用
    (hdu step 6.3.7)Cat vs. Dog(当施工方规则:建边当观众和其他观众最喜爱的东西冲突,求最大独立集)
    dba_dependencies查询结果视图
    情绪一点点
    c#基于这些,你已经看到了?(一)-----谁才刚刚开始学习使用
    九. 200创业教训万元获得--“神刻”这是忽悠?
    初步swift语言学习笔记6(ARC-自己主动引用计数,内存管理)
    采用CSS3 Media Query技术适应Android平板屏幕分辨率和屏幕像素密度
    线程的上下文
  • 原文地址:https://www.cnblogs.com/sucsy/p/3152376.html
Copyright © 2011-2022 走看看