C# WCF Web Api 4 MaxReceivedMessageSize

发布于 2024-11-17 02:07:52 字数 1395 浏览 1 评论 0原文

我正在使用 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>

在新的 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);
        }
    }

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

老街孤人 2024-11-24 02:07:52

maxReceivedMessageSize 是您必须在正在使用的绑定上定义的属性。 .Net 4 中的 WCF 引入了简化的配置,因此如果您不配置任何内容,将使用默认值。
下面的示例对于 wshttpBinding 有效,您可以根据您使用的绑定对其进行修改,并将其注册到 web.config 中的 servicemodel-binding 部分(假设您使用的是 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>

华泰
多米尼克

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

但可醉心 2024-11-24 02:07:52

如果您尝试以编程方式执行此操作(通过将 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;
    }
}

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;
    }
}
只怪假的太真实 2024-11-24 02:07:52

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

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);
}
〃温暖了心ぐ 2024-11-24 02:07:52

这是在代码中执行此操作的方法。

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。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文