2011-06-24 59 views
6

我正在使用WCF Web Api 4.0框架,并且正在运行到maxReceivedMessageSize已超过65,000错误。C#WCF Web Api 4 MaxReceivedMessageSize

我已经更新了我的webconfig,看起来像这样,但因为我是uisng 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网络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); 
     } 
    } 

回答

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

的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

的maxReceivedMessageSize是你必须定义在使用绑定you're的属性。 .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> 

HTH 多米尼克

+0

没有带之所以能得到这个与我的工作服务嗨 –

+0

nextgenneo,你设法得到运行到现在你的代码? – Dominik

1

这是如果你创建自己的自定义的主机,从标准的一个派生你如何能做到这一点的代码

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。

+0

我究竟在哪里放这段代码?我不知道主机在哪里。我试着把它放在我的HttpOperationHandlerFactory的OnCreateRequestHandlers中,但只能访问端点。 –

+0

@nextgenneo啊,你不是自己托管。好点子。不确定。如果我知道,我会通知你。 –

+0

有没有一种方法可以增加每个API或每个控制器?我不想打开整个API的消息大小? –

3

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