【问题标题】:System.ServiceModel.ProtocolException: The remote server returned an unexpected response: (413) Request Entity Too LargeSystem.ServiceModel.ProtocolException:远程服务器返回意外响应:(413)请求实体太大
【发布时间】:2014-05-17 06:12:15
【问题描述】:

我正在做一个项目。该网站建立在 MVC 之上,http://www.example.com,最近,另一位开发人员在项目中添加了一个 Windows Web 服务(不是 WCF),http://www.example.com/WebServices/upload.asmx。它应该用于将图像上传到服务器。 Route 被添加到 RouteConfig,所以它可以工作:

routes.IgnoreRoute("WebServices/*/{resource}.aspx/{*pathInfo}");

但是,我注意到它适用于小尺寸文件。当文件大小达到某个点时,即 > 1MB(不确定确切的 #,但 650KB 文件有效,1.1MB 文件失败)。错误信息是:

System.ServiceModel.ProtocolException was caught
  HResult=-2146233087
  Message=The remote server returned an unexpected response: (413) Request Entity Too Large.
  Source=mscorlib
  StackTrace:
    Server stack trace: 
       at System.ServiceModel.Channels.HttpChannelUtilities.ValidateRequestReplyResponse(HttpWebRequest request, HttpWebResponse response, HttpChannelFactory`1 factory, WebException responseException, ChannelBinding channelBinding)
       at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
       at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
       at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
       at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
       at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
    Exception rethrown at [0]: 
       at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
       at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
       at SyncDatabase.eyephoto.com.upload.UploadSoap.UploadImage(UploadImageRequest request)
       ....
  InnerException: System.Net.WebException
       HResult=-2146233079
       Message=The remote server returned an error: (413) Request Entity Too Large.
       Source=System
       StackTrace:
            at System.Net.HttpWebRequest.GetResponse()
            at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
       InnerException: 

我搜索了 2 天,我能找到的所有解决方案都不起作用。现在,在网站上的 web.config 中,我有:

<httpRuntime maxRequestLength="40960000" executionTimeout="300"  />
...

<system.serviceModel>
    <client />
    <bindings>
      <basicHttpBinding>
        <binding closeTimeout="00:10:00"
          openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" maxBufferPoolSize="2147483647" maxBufferSize="2147483647"
          maxReceivedMessageSize="2147483647"  >
          <readerQuotas maxDepth="2000000" maxStringContentLength="2147483647"
            maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />

        </binding>
      </basicHttpBinding>
    </bindings>
</system.serviceModel>

我们使用桌面应用程序来使用 Web 服务。在 app.config 中,我有:

<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="UploadSoap1" maxBufferPoolSize="2147483647" maxBufferSize="2147483647"
          maxReceivedMessageSize="2147483647" closeTimeout="00:10:00"
          openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" >

          <readerQuotas maxDepth="2000000" maxStringContentLength="2147483647"
            maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />

          <security mode="None" />
        </binding>        
      </basicHttpBinding>
    </bindings>

    <client>
      <endpoint address="http://www.example.com/WebServices/Upload.asmx"
        binding="basicHttpBinding" bindingConfiguration="UploadSoap1"
        contract="mysite.com.upload.UploadSoap" name="UploadSoap1" />
    </client>
 </system.serviceModel>

奇怪的是:600KB 的文件可以工作,但是如果文件大小> 1MB,就会出现这个错误。但是,从这些配置中我找不到与这些数字的任何关系。

谁知道问题出在哪里?有什么建议?可能是因为 MVC 中的路由?

谢谢

【问题讨论】:

标签: c# asp.net-mvc web-services


【解决方案1】:

根据搜索结果多次尝试后,由于某些原因,它从未成功。最大上传文件总是600KB左右,不知道这个#从哪里来的。我怀疑是因为我在 MVC 项目中使用了传统的 asmx Web 服务。

最后,我决定这样做:

  1. 从客户端,将文件拆分为“帧”,即 512KB。
  2. 使用 Web 服务将帧传递给服务器。
  3. 服务器决定是否需要将文件保存到文件系统(即文件大小
  4. 如果收到最后一帧,则从 sql 加载帧 并将它们组合起来,然后进行其他处理。

这不是最好的方法,但至少它有效。我们可以随意选择帧大小。

谢谢

【讨论】:

  • 你好,c#中512KB的帧是怎么确定的
  • 在我们的环境中,最大上传文件大小约为 600KB。我选择512KB只是因为它是2^8 KB,没有任何特殊原因。
猜你喜欢
  • 2014-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-07
  • 1970-01-01
相关资源
最近更新 更多