C# WCF Web Api 4 MaxReceivedMessageSize

本文关键字:MaxReceivedMessageSize Api Web WCF | 更新日期: 2023-09-27 18:00:28

我正在使用WCF Web Api 4.0框架,并且正在运行maxReceivedMessageSize已超过65000错误。

我已经更新了我的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# WCF Web Api 4 MaxReceivedMessageSize

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

<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>

HTHDominik

如果你试图用程序(通过使用带有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是HttpConfigurationServiceHostFactory的扩展,看起来像:

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;
    }
}

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

这就是在代码中实现的方法

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。

相关文章:
  • 没有找到相关文章