【问题标题】:C# WCF Web Api 4 MaxReceivedMessageSizeC# WCF Web API 4 MaxReceivedMessageSize
【发布时间】:2023-03-16 02:54:01
【问题描述】:

我正在使用 WCF Web Api 4.0 框架并且遇到了 maxReceivedMessageSize has exceeded 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);
        }
    }

【问题讨论】:

    标签: c# .net wcf-web-api


    【解决方案1】:

    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 多米尼克

    【讨论】:

    • 嗨 nextgenneo,你的代码运行到现在了吗?
    【解决方案2】:

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

    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。

    【讨论】:

    • 我应该把这段代码放在哪里?不知道楼主在哪里。我尝试将它放在我的 HttpOperationHandlerFactory OnCreateRequestHandlers 中,但只能访问端点。
    • @nextgenneo 啊,你不是自托管。好点子。不确定。如果我知道了,我会告诉你的。
    • 有没有办法为每个 API 或每个控制器增加这个?我不想打开整个 API 的消息大小?
    • @ShawnWildermuth 在新的 ASP.NET Web API 测试版中,我相信目前只有一个 GlobalConfiguration.Configuration 所以答案是否定的。然而,一直在谈论改变这一点。使用 MessageHandler 重新添加保护将非常容易。
    【解决方案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;
        }
    }
    

    【讨论】:

      【解决方案4】:

      如果您在 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);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-11-13
        • 2023-03-22
        • 2011-10-11
        • 2013-09-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多