C# WCF Web Api 4 MaxReceivedMessageSize

橙三吉。 提交于 2020-01-02 03:34:26

问题


I am using the WCF Web Api 4.0 framework and am running into the maxReceivedMessageSize has exceeded 65,000 error.

I've updated my webconfig to look like this but because I am uisng the WCF Web Api I think this isn't even used anymore as I am no longer using a 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>

Where do I specify MaxReceivedMessageSize in the new WCF Web Api?

I've also tried a CustomHttpOperationHandlerFactory to no avail:

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

回答1:


If you're trying to do this programatically (via using MapServiceRoute with HttpHostConfiguration.Create) the way you do it is like this:

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

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

The NoMessageSizeLimitHostConfig is an extension of HttpConfigurableServiceHostFactory that looks something like:

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



回答2:


the maxReceivedMessageSize is a property you have to define on the binding you´re using. WCF in .Net 4 introduced the simplified configuration, so if you don´t configure anything, default values will be used. The example below is valid for the wshttpBinding, you have the ammend it according to your used binding and register it in your web.config (assuming you´re using an IIS hosted service) in the servicemodel-binding section.

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




回答3:


If you are hosting in IIS, you can set the values where ever you define the route (in my case, the global.asax). If you have TransferMode set to buffered (the default), you will also need to set MaxBufferSize to the same value as MaxReceivedMessageSize.

protected void Application_Start()
{
   var config = new HttpConfiguration(); 
   config.MaxReceivedMessageSize = int.MaxValue;
   config.MaxBufferSize = int.MaxValue;
   RouteTable.Routes.MapServiceRoute<MyService>("api", config);
}



回答4:


This is how you can do it in code

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

If you create a your own custom host, derived from the standard one, there is a method that you can overload to configure the HttpEndpoint.



来源:https://stackoverflow.com/questions/6462571/c-sharp-wcf-web-api-4-maxreceivedmessagesize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!