WCF流式传输大文件

时间:2012-11-02 09:02:35

标签: wcf wcf-data-services wcf-binding wcf-client

我读了这篇文章http://www.codeproject.com/Articles/166763/WCF-Streaming-Upload-Download-Files-Over-HTTP来通过WCF上传大文件。

我创建了相同的配置,但即使添加了属性maxReceivedMessageSize ="2147483647",我也只上传了48kb的文件。当我尝试上传超过48kb的文件时出现错误

  

远程服务器返回错误:(413)Request Entity Too   大。

我错了或错过了什么?以下是我的配置

服务器配置:

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <httpRuntime executionTimeout="4800" maxRequestLength="2097150"/>
  </system.web>
  <system.serviceModel>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    <!--BINDING-->
    <bindings>
      <basicHttpBinding>
        <binding name="TransferService" 
                 closeTimeout="00:10:00"
                 maxBufferPoolSize="2147483647"
                 maxBufferSize="2147483647"
                 maxReceivedMessageSize ="2147483647" 
                 openTimeout="00:10:00" 
                 receiveTimeout="00:10:00" 
                 sendTimeout="00:10:00" 
                 messageEncoding="Text"
                 transferMode="Streamed"
                 >
          <readerQuotas
            maxDepth="2147483647" 
            maxStringContentLength="2147483647"
            maxBytesPerRead="2147483647" 
            maxNameTableCharCount="2147483647" 
            maxArrayLength="2147483647"/>
        </binding>
        </basicHttpBinding>
    </bindings>

    <!--SERVICE-->
    <services>
      <service name="TransferService.TransferService" 
               behaviorConfiguration="TransferServiceBehavior" >
         <endpoint  
             address="" 
             binding="basicHttpBinding" 
             bindingConfiguration="TransferService" 
             contract="TransferService.ITransferService" >
        </endpoint>
      </service>
    </services>

    <!--BEHAVIOR-->
    <behaviors>
      <serviceBehaviors>
        <behavior name="TransferServiceBehavior">
          <serviceMetadata httpGetEnabled="true"  />
          <serviceDebug includeExceptionDetailInFaults="true" />
          <dataContractSerializer maxItemsInObjectGraph="2147483647" />
          <serviceThrottling maxConcurrentCalls="500" maxConcurrentSessions="500" maxConcurrentInstances="500" />
        </behavior>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
 <system.webServer>
   <security>
     <requestFiltering>
       <requestLimits maxAllowedContentLength="500000000"></requestLimits>
     </requestFiltering>
   </security>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

Web客户端配置:

<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_ITransferService" />
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint name="BasicHttpBinding_ITransferService" 
          address="http://localhost/transfer/TransferService.svc"
          binding="basicHttpBinding" 
          bindingConfiguration="BasicHttpBinding_ITransferService"
          contract="TransferService.ITransferService" />
    </client>
  </system.serviceModel>

请建议任何解决方案。

提前致谢


现在我已将配置更改为您的建议,但它仍然会引发相同的错误400或413.以下是我的测试项目。我不知道为什么? (环境:Window 7 Pro 64位,IIS7,WCF 4.0)

https://skydrive.live.com/redir?resid=BFE92959302FBAA0!105&authkey=!ANO_URChpql9gKE

我花了两周时间研究,但这也是同样的错误。

请帮帮我。提前谢谢。

3 个答案:

答案 0 :(得分:1)

嗯,客户端配置必须也定义相同的绑定配置(具有更大的传输大小),并在其端点配置中指定绑定配置!

因此,请将您的客户端配置更改为:

<system.serviceModel>
   <bindings>
      <basicHttpBinding>
        <binding name="TransferService" 
                 closeTimeout="00:10:00"
                 maxBufferPoolSize="2147483647"
                 maxBufferSize="2147483647"
                 maxReceivedMessageSize ="2147483647" 
                 openTimeout="00:10:00" 
                 receiveTimeout="00:10:00" 
                 sendTimeout="00:10:00" 
                 messageEncoding="Text"
                 transferMode="Streamed"
                 >
          <readerQuotas
            maxDepth="2147483647" 
            maxStringContentLength="2147483647"
            maxBytesPerRead="2147483647" 
            maxNameTableCharCount="2147483647" 
            maxArrayLength="2147483647"/>
        </binding>
        </basicHttpBinding>
    </bindings>

    <client>
      <endpoint name="BasicHttpBinding_ITransferService" 
          address="http://localhost/transfer/TransferService.svc"
          binding="basicHttpBinding" 
          bindingConfiguration="TransferService"
          contract="TransferService.ITransferService" />
    </client>
  </system.serviceModel>

然后你应该在客户端和服务器上具有相同的设置,然后这些设置将变得有用!

答案 1 :(得分:1)

我检查了你的项目,用这个替换你的webconfig并再次更新服务参考,

  <?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <httpRuntime executionTimeout="4800" maxRequestLength="2097150"/>
  </system.web>
  <system.serviceModel>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    <!--BINDDING-->
    <bindings>
      <basicHttpBinding>
      </basicHttpBinding>
      <customBinding>
        <binding name="LargeSilverlight" closeTimeout="00:21:00" openTimeout="00:20:00"
          receiveTimeout="00:20:00" sendTimeout="00:50:00">
          <textMessageEncoding maxReadPoolSize="2147483647" maxWritePoolSize="2147483647">
            <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
              maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
          </textMessageEncoding>
          <httpTransport maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
            maxBufferSize="2147483647" />
        </binding>
      </customBinding>
    </bindings>
    <client>
    </client>
    <!--SERVICE-->
    <services>
      <service name="TransferService.TransferService"  behaviorConfiguration="SilverlightWCFLargeDataApplication" >
        <endpoint  address="" binding="customBinding" bindingConfiguration="LargeSilverlight" behaviorConfiguration="SilverlightWCFLargeDataApplication" contract="TransferService.ITransferService" >
        </endpoint>

      </service>
    </services>

    <!--BEHAVIOR-->
    <behaviors>
      <serviceBehaviors>
        <behavior name="SilverlightWCFLargeDataApplication">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
        </behavior>

      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="SilverlightWCFLargeDataApplication">
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
        </behavior>
      </endpointBehaviors>
    </behaviors>


  </system.serviceModel>
  <system.webServer>

    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="500000000"></requestLimits>
      </requestFiltering>
    </security>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

答案 2 :(得分:0)

我面临同样的问题。我在配置文件中使用此代码然后我能够上传25MB文件  试试这个

<system.serviceModel>
      <bindings>
           <basicHttpBinding>
               <binding name="webserviceMOSSuiteSoap" 
              closeTimeout="00:01:00" 
              maxBufferPoolSize="20000000" 
              maxBufferSize="20000000" 
              maxReceivedMessageSize="20000000" 
              openTimeout="00:01:00" 
              receiveTimeout="00:10:00" 
              sendTimeout="00:01:00" 
              messageEncoding="Text" 
              transferMode="Buffered" 
              allowCookies="false" 
              bypassProxyOnLocal="false" 
              hostNameComparisonMode="StrongWildcard"                        
              textEncoding="utf-8"                       
              useDefaultWebProxy="true">
              <readerQuotas maxDepth="32" 
                            maxStringContentLength="20000000" 
                            maxArrayLength="20000000" 
                            maxBytesPerRead="20000000" 
                            maxNameTableCharCount="20000000"/>
              <security mode="None">
              <transport clientCredentialType="None" 
                    proxyCredentialType="None" 
                    realm=""/>
            <message clientCredentialType="UserName" 
                 algorithmSuite="Default"/>
                          </security> 
                    </binding>
            </basicHttpBinding>   
      </bindings>
<client>      
        <endpoint address="http://localhost/WebServiceConnect/webservice.asmx" 
                  binding="basicHttpBinding" 
                  bindingConfiguration="webserviceMOSSuiteSoap" 
                  contract="ServiceReference1.webserviceMOSSuiteSoap" 
                  name="webserviceMOSSuiteSoap"/>
    </client>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" 
                               multipleSiteBindingsEnabled="true"/>
</system.serviceModel>