通过WCF中的WSHttpBinding上传大文件

时间:2012-05-02 16:05:48

标签: wcf file-upload wcf-binding

我正在使用WSHttpBinding处理WCF文件上传器。我希望上传大约10 MB的文件(不是在那个上面玩精确的游戏,但不会让文件大得多)。一切都很好,大约5mb。之后,我开始收到HTTP 500错误。

我的绑定设置如下:

wsBinding.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
wsBinding.MaxBufferPoolSize = 52428800;
wsBinding.MaxReceivedMessageSize = 13631488;
wsBinding.ReceiveTimeout = new TimeSpan(0, 3, 0);

我的httpRuntime maxRequestLength在web.config中是20480kb

我的要求如下:

POST http://mywebsite/FileUploader.svc HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: application/soap+xml;charset=UTF-8;action="http://namespace/UploadPhoto"
Content-Length: 13979476
Host: mywebsite
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:fil="http://namespace">
   <soap:Header xmlns:wsa="http://www.w3.org/2005/08/addressing"><wsa:Action>http://namespace/IFileUploaderSvc/UploadPhoto</wsa:Action></soap:Header>
   <soap:Body>
      <fil:UploadPhoto>
         <fil:AuthorizationToken>(Password)</fil:AuthorizationToken>
         <fil:ProgramName>AProgram</fil:ProgramName>
         <fil:FileName>bigolefile.jpg</fil:FileName>
         <fil:File>(base 64 file data)</fil:File>
      </fil:UploadPhoto>
   </soap:Body>
</soap:Envelope>

使用WSHttpBinding非常重要,而不是WebHttpBinding。 提前致谢, 安德鲁

1 个答案:

答案 0 :(得分:1)

尝试在WSHttpBinding绑定中使用TextMessageEncodingBindingElement或MtomMessageEncodingBindingElement作为messageEncoding属性。

例如,以下代码段使用TextMessageEncodingBindingElement。 messageEncoding =“文字” http://msdn.microsoft.com/en-us/library/ms733742.aspx

  <wsHttpBinding>
    <binding name="wsHttpWithMessageSecurity" messageEncoding="Text"
             closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00"
             sendTimeout="00:01:00" bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"  textEncoding="utf-8"
                useDefaultWebProxy="true"
                allowCookies="false">

      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" 
                    maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
      <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" />