2011-06-24 87 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); 
}