C # WCF Web Api 4 MaxReceivedMessageSize

I am using WCF Web Api 4.0 and running in maxReceivedMessageSize, exceeding 65,000 errors.

I updated my webconfig to look like this, but because I am uisng WCF Web Api. I think this is not even used as I no longer use 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 to specify MaxReceivedMessageSize in the new WCF Web Api?

I also tried 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); } } 
+6
source share
4 answers

If you are trying to do this programmatically (using MapServiceRoute with HttpHostConfiguration.Create), how do you do it:

 IHttpHostConfigurationBuilder httpHostConfiguration = HttpHostConfiguration.Create(); //And add on whatever configuration details you would normally have RouteTable.Routes.MapServiceRoute<MyService, NoMessageSizeLimitHostConfig>(serviceUri, httpHostConfiguration); 

NoMessageSizeLimitHostConfig is an HttpConfigurableServiceHostFactory extension that looks something like this:

 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; } } 
+3
source

maxReceivedMessageSize is a property that you must define when the binding you are using. WCF in .Net 4 introduced a simplified configuration, so if you do not configure anything, the default values ​​will be used. The example below is valid for wshttpBinding, you have a fix according to the binding you are using and register it in your web.config (assuming you use the IIS hosting 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> 

NTN Dominik

+4
source

If you host in IIS, you can set values ​​whenever you define a route (in my case, global.asax). If you have TransferMode installed for buffering (by default), you 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); } 
+3
source

Here 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 your own host, obtained from the standard, there is a method that you can overload to configure HttpEndpoint.

+1
source

Source: https://habr.com/ru/post/891258/


All Articles