BadHttpRequestException由于MinRequestBodyDataRate和连接不良 [英] BadHttpRequestException due to MinRequestBodyDataRate and poor connection

查看:186
本文介绍了BadHttpRequestException由于MinRequestBodyDataRate和连接不良的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET Core Web应用程序,该应用程序接受经过身份验证的用户上传的文件,并提供一些错误处理功能,以便在发生异常时通知我(

I have an ASP.NET Core web application that accepts file uploads from authenticated users, along with some error handling to notify me when an exception occurs (Exceptional). My problem is I'm periodically getting alerts of BadHttpRequestExceptions due to users accessing the application via mobile devices in areas with unreliable coverage. I used to get this with 2.0 too, but until the exception description got updated in 2.1, I never knew it was specifically related to MinRequestBodyDataRate, or that it was configurable. I've up'd the defaults (240 bytes over 5 seconds) with the following

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseKestrel(options =>
            {
                options.Limits.MinRequestBodyDataRate = new MinDataRate(240.0, TimeSpan.FromSeconds(10.0));
            }) 
            .UseStartup<Startup>();

这使等待客户端/浏览器的时间延长了一倍,但是我仍然收到错误警报.我无法控制用户的不满意,但是我仍然希望尝试找到一种减轻错误的方法.我可以进一步扩展最小值,但是如果可能的话,我不想在整个应用程序中都这样做.理想情况下,应该是处理上传的特定路径/操作.我能够找到的文档指示可以使用IHttpMinRequestBodyDataRateFeature来配置此最低要求.我曾考虑将其放入动作中,但直到模型绑定将整个文件上载以将其绑定到我的参数之前,我什至都不认为该动作被调用.

This doubles the duration it will wait for the client/browser, but I'm still getting error alerts. I can't control the poor reception of the users, but I'm still hoping to try and find a way to mitigate the errors. I can extend the minimum even more, but I don't want to do it for the entire application if possible. Ideally it'd be to the specific route/action handling the upload. The documentation I was able to find indicated this minimum can be configured using the IHttpMinRequestBodyDataRateFeature. I thought about putting this inside the action, but I don't think the action is even called until the modelbinding has gotten the entire file uploaded in order to bind it to my parameter.

public async Task<IActionResult> Upload(IFormFile file)
{
    var minRequestBodyDataRateFeature = HttpContext.Features.Get<IHttpMinRequestBodyDataRateFeature>();
    minRequestBodyDataRateFeature.MinDataRate = new MinDataRate(240, TimeSpan.FromSeconds(30));
    myDocumentService.SaveFile(file);
}

我以前实现了分块上载,并且仅在最后一个块到来时才将文件保存到服务/数据库中(此文件在它们之间的临时位置逐渐建立),这在一定程度上缓解了这种情况.但是即使通过CreateWebHostBuilder完成了较长的持续时间,我仍然会得到这些BadHttpRequestException(未显示分块,因为它不相关).

This is somewhat mitigated by the fact that I had previously implemented chunked uploading, and only save the file to the service/database when the last chunk comes it (building up the file gradually in a temporary location in between). But I'm still get these BadHttpRequestExceptions even with the extended duration done via CreateWebHostBuilder (chunking not shown, as it's not relevant).

我认为最好的选择可能是尝试将其连接到设置中间件的configure方法中,但是我不确定将其仅应用于一个操作的最佳方法.网址也许?我想知道如果我将最小费率设置为null完全禁用最低费率会带来什么影响.

I'm thinking the best bet might be to try and wire it up in the configure method that sets up the middleware, but I'm not sure about the best way to only apply it to one action. Url maybe? And I'm wondering what implications there would be if I disabled the min rate entirely by setting it to null.

我基本上只是在寻求使连接对低质量的连接更加宽容,同时又不应对应用程序的其他方面造成太多麻烦.

I'm basically just looking to make the connection more tolerant of poor quality connections while not upsetting too much of the day to day for other aspects of the application.

  1. 我需要一种方法来应用更改(可能在中间件Startup.Configure()中),以使其仅适用于受影响的路由/URL/操作.
  2. 是否有必要考虑针对该路由/网址/操作完全禁用它(而不是放大它)?整个申请?
  3. 如果没有任何一项,简单地忽略这些错误并永不记录它们是否安全?我是否为与应用程序/服务器的恶意交互创建了一个盲点?
  1. I need a way to apply the change (potentially in the middleware Startup.Configure()), in such a way that it only applies to the affected route/url/action.
  2. Are there implications to consider if I disable it entirely (versus enlarging it) for that route/url/action? Entire application?
  3. Failing any of that, is it safe to simply ignore these errors and never log them? Do I create a blind spot to a malicious interactions with the application/server?

推荐答案

我注意到了同样的问题.我有较大的文件,用户可以从我的网站上下载,有些人(取决于区域)下载速度较慢,并且下载会在一段时间后停止.我还在日志中看到了异常.

I have noticed the same issue. I have bigger files that users can download from my site and some people, depending on the region, have slow download rates and the download stops after a while. I also see the exceptions in the log.

我不建议禁用此功能,因为某些连接阻塞或连接速度非常慢,这可能会合计.只需增加时间并将速率降低到一个可以说您的网页仍然可用的值即可.

I do not recommend to disable the feature, because of some stuck or very slow connections, that maybe sum up. Just increase the time and lower the rate to a value where you can say that your web page is still usable.

对于特定的URL,我不会这样做.除非您注意到某些性能问题,否则在页面具有相同设置的情况下不会受到损害.

I would not do that for specific URL's. It does not hurt when the pages have the same settings unless you notice some performance issues.

还请记住,还有一个MinResponseDataRate选项.

Also keep in mind that there is also a MinResponseDataRate option.

最好的方法是在应用程序启动时为所有路由设置一些合理的值.人们在日志中总是会出现例外情况,即失去与互联网的连接,并且红k必须在一段时间后关闭连接以释放资源.

The best way is to set some reasonable values at the application startup for all routes. There will always be exceptions in the log from people that losing the connection to the internet and kestrel has to close the connection after a while to free up resources.

       public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseKestrel(options =>
            {
                options.Limits.MinRequestBodyDataRate =
                    new MinDataRate(bytesPerSecond: 80, gracePeriod: TimeSpan.FromSeconds(20));
                options.Limits.MinResponseDataRate =
                    new MinDataRate(bytesPerSecond: 80, gracePeriod: TimeSpan.FromSeconds(20));
            })
            .ConfigureLogging((hostingContext, logging) =>
            {
                logging.ClearProviders();
                logging.SetMinimumLevel(LogLevel.Trace);
            })
            .UseNLog()
            .UseStartup<Startup>()
            .Build();
}

也请查看您的异步上载方法.确保您正在等待或返回任务.我认为它不是这样编译的.

Please also have a look at your async Upload method. Make sure you have an await or return a task. I don't think it compiles this way.

这篇关于BadHttpRequestException由于MinRequestBodyDataRate和连接不良的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆