C#WCF Web Api 4 MaxReceivedMessageSize

Luk*_*ina 6 .net c# wcf-web-api

我正在使用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>
Run Code Online (Sandbox Code Playgroud)

在新的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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

mwe*_*ber 3

如果您尝试以编程方式执行此操作(通过将 MapServiceRoute 与 HttpHostConfiguration.Create 结合使用),则执行方式如下:

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

RouteTable.Routes.MapServiceRoute<MyService, NoMessageSizeLimitHostConfig>(serviceUri, httpHostConfiguration);  
Run Code Online (Sandbox Code Playgroud)

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;
    }
}
Run Code Online (Sandbox Code Playgroud)