0

我有一个采用字节数组的数据服务。然后,我有一个网页尝试将文件发送到该数据服务。如果文件很小(比如 50kb),那么一切都按预期运行,但是如果文件很大(超过 100kb),我会在数据服务的保存更改调用中收到“BadRequest”错误消息。

有没有办法将更大的数据大小传递给数据服务?

编辑(更多详细信息):我将 maxRequestLength 设置得更高,并尝试了一些 webHttpBinding 来增加 maxReceivedMessageSize,但这些似乎没有帮助。

4

2 回答 2

5

WCF 服务可以处理的请求的最大大小由 WCF 绑定上的 MaxReceivedMessageSize 属性控制。默认值为 65536 ,超过该值会得到 400 响应码。

在托管服务的网站的 web.config 中,在部分中添加以下节点。

<system.serviceModel> 
<services> 
  <!-- The name of the service --> 
  <service name="NorthwindService"> 
    <!-- you can leave the address blank or specify your end point URI --> 
    <endpoint address ="YourServiceEndpoint" 
              binding="webHttpBinding" bindingConfiguration="higherMessageSize" 
     contract ="System.Data.Services.IRequestHandler"> 
    </endpoint> 
  </service> 
</services> 
<bindings> 
  <webHttpBinding> 
    <!-- configure the maxReceivedMessageSize  value to suit the max size of 
         the request ( in bytes ) you want the service to recieve--> 
    <binding name="higherMessageSize" maxReceivedMessageSize ="MaxMessageSize"/> 
  </webHttpBinding> 
</bindings> 
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> 
</system.serviceModel>

如果托管在 IIS 上,ASP.Net 请求大小限制也会导致大型请求被拒绝,您需要设置 HttpRuntimeSection.MaxRequestLength 属性。

<system.web> 
  <httpRuntime MaxRequestLength="ValueInKiloBytes" />
</system.web>

确定 WCF 是否在幕后引发了未在 HTTP 级别向您展示的异常。您可以在服务器端配置 WCF 跟踪以记录来自 WCF 层的必要信息。一旦您进行了跟踪设置并重现了故障,请检查日志是否包含这些异常消息中的一个或两个。

System.ServiceModel.ProtocolException
"The maximum message size quota for incoming messages (65536) has been exceeded. 
To increase the quota, use the MaxReceivedMessageSize property on the appropriate binding element."

System.Web.HttpException
"Maximum request length exceeded."

如果您看到日志确实包含此消息,那么您可以确定失败是由于消息大小造成的,并相应地应用此修复。

PD:请记住,您的表单必须使用该"POST"方法。

于 2009-01-15T16:15:53.937 回答
3

我还尝试编辑 web.config 文件,但没有结果。我仍然得到同样的例外。

一个对我有用的解决方案是编辑machine.config文件并在 System.ServiceModel 节点中添加以下行。

<standardEndpoints>
        <webHttpEndpoint>
            <standardEndpoint name="" maxReceivedMessageSize="16777216" maxBufferSize="16777216" />
        </webHttpEndpoint>
    </standardEndpoints>

我不知道这是否是正确的解决方案,但它对我有用。

于 2011-04-13T11:44:30.200 回答