6

我正在使用 WCF Web Api 4.0 框架并且遇到了 maxReceivedMessageSize 已超过 65,000 错误。

我已经将我的 webconfig 更新为如下所示,但是因为我正在使用 WCF Web Api,所以我认为这甚至不再使用了,因为我不再使用 webHttpEndpoint?

<standardEndpoints>
      <webHttpEndpoint>
        <!-- 
            Configure the WCF REST service base address via the global.asax.cs file and the default endpoint 
            via the attributes on the <standardEndpoint> element below
        -->
        <standardEndpoint name="" 
                          helpEnabled="true" 
                          automaticFormatSelectionEnabled="true"
                          maxReceivedMessageSize="4194304" />       

      </webHttpEndpoint>

在新的 WCF Web Api 中哪里可以指定 MaxReceivedMessageSize?

我也尝试过 CustomHttpOperationHandlerFactory 无济于事:

  public class CustomHttpOperationHandlerFactory: HttpOperationHandlerFactory
    {       
        protected override System.Collections.ObjectModel.Collection<HttpOperationHandler> OnCreateRequestHandlers(System.ServiceModel.Description.ServiceEndpoint endpoint, HttpOperationDescription operation)
        {
            var binding = (HttpBinding)endpoint.Binding;
            binding.MaxReceivedMessageSize = Int32.MaxValue;

            return base.OnCreateRequestHandlers(endpoint, operation);
        }
    }
4

4 回答 4

4

maxReceivedMessageSize 是您必须在您正在使用的绑定上定义的属性。.Net 4 中的 WCF 引入了简化配置,因此如果您不进行任何配置,将使用默认值。下面的示例对 wshttpBinding 有效,您可以根据使用的绑定对其进行修改,并将其注册到 web.config(假设您使用的是 IIS 托管服务)的 servicemodel-binding 部分。

<wsHttpBinding>
    <binding name="CalculatorBinding" maxBufferPoolSize="2000000" maxReceivedMessageSize="2000000000" >
      <security mode="Transport" >
        <transport clientCredentialType="Windows" />
      </security>
      <readerQuotas maxDepth="2000000" maxStringContentLength="2000000"
      maxArrayLength="2000000"
      maxBytesPerRead="2000000"
      maxNameTableCharCount="2000000" />
    </binding>
  </wsHttpBinding>

HTH 多米尼克

于 2011-06-24T06:43:56.553 回答
3

如果您尝试以编程方式执行此操作(通过使用带有 HttpHostConfiguration.Create 的 MapServiceRoute),您的操作方式如下:

IHttpHostConfigurationBuilder httpHostConfiguration = HttpHostConfiguration.Create(); //And add on whatever configuration details you would normally have

RouteTable.Routes.MapServiceRoute<MyService, NoMessageSizeLimitHostConfig>(serviceUri, httpHostConfiguration);  

NoMessageSizeLimitHostConfig 是 HttpConfigurableServiceHostFactory 的扩展,类似于:

public class NoMessageSizeLimitHostConfig : HttpConfigurableServiceHostFactory
{
    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {
        var host = base.CreateServiceHost(serviceType, baseAddresses);  

        foreach (var endpoint in host.Description.Endpoints)
        {
            var binding = endpoint.Binding as HttpBinding;    

            if (binding != null)
            {
                binding.MaxReceivedMessageSize = Int32.MaxValue;
                binding.MaxBufferPoolSize = Int32.MaxValue;
                binding.MaxBufferSize = Int32.MaxValue;
                binding.TransferMode = TransferMode.Streamed;
            }
        }
        return host;
    }
}
于 2011-08-13T01:41:51.283 回答
3

如果您在 IIS 中托管,则可以在定义路由的任何位置设置值(在我的例子中是 global.asax)。如果您已TransferMode设置为缓冲(默认),您还需要设置MaxBufferSize为与MaxReceivedMessageSize.

protected void Application_Start()
{
   var config = new HttpConfiguration(); 
   config.MaxReceivedMessageSize = int.MaxValue;
   config.MaxBufferSize = int.MaxValue;
   RouteTable.Routes.MapServiceRoute<MyService>("api", config);
}
于 2011-10-24T19:22:02.140 回答
1

这就是你如何在代码中做到这一点

var endpoint = ((HttpEndpoint)host.Description.Endpoints[0]);  //Assuming one endpoint
endpoint.TransferMode = TransferMode.Streamed;
endpoint.MaxReceivedMessageSize = 1024 * 1024 * 10;  // Allow files up to 10MB

如果您创建自己的自定义主机,从标准主机派生,则可以重载一种方法来配置 HttpEndpoint。

于 2011-06-24T21:14:14.210 回答