<small id='vEULO'></small><noframes id='vEULO'>

    • <bdo id='vEULO'></bdo><ul id='vEULO'></ul>
    <legend id='vEULO'><style id='vEULO'><dir id='vEULO'><q id='vEULO'></q></dir></style></legend>
  • <tfoot id='vEULO'></tfoot>

      1. <i id='vEULO'><tr id='vEULO'><dt id='vEULO'><q id='vEULO'><span id='vEULO'><b id='vEULO'><form id='vEULO'><ins id='vEULO'></ins><ul id='vEULO'></ul><sub id='vEULO'></sub></form><legend id='vEULO'></legend><bdo id='vEULO'><pre id='vEULO'><center id='vEULO'></center></pre></bdo></b><th id='vEULO'></th></span></q></dt></tr></i><div id='vEULO'><tfoot id='vEULO'></tfoot><dl id='vEULO'><fieldset id='vEULO'></fieldset></dl></div>

        .NET Core 中 HostingEnvironment.QueueBackgroundWorkItem 的替代解决

        时间:2023-07-11

        <small id='3LM9Q'></small><noframes id='3LM9Q'>

            <bdo id='3LM9Q'></bdo><ul id='3LM9Q'></ul>

                <i id='3LM9Q'><tr id='3LM9Q'><dt id='3LM9Q'><q id='3LM9Q'><span id='3LM9Q'><b id='3LM9Q'><form id='3LM9Q'><ins id='3LM9Q'></ins><ul id='3LM9Q'></ul><sub id='3LM9Q'></sub></form><legend id='3LM9Q'></legend><bdo id='3LM9Q'><pre id='3LM9Q'><center id='3LM9Q'></center></pre></bdo></b><th id='3LM9Q'></th></span></q></dt></tr></i><div id='3LM9Q'><tfoot id='3LM9Q'></tfoot><dl id='3LM9Q'><fieldset id='3LM9Q'></fieldset></dl></div>
              1. <tfoot id='3LM9Q'></tfoot>
                    <tbody id='3LM9Q'></tbody>
                1. <legend id='3LM9Q'><style id='3LM9Q'><dir id='3LM9Q'><q id='3LM9Q'></q></dir></style></legend>

                  本文介绍了.NET Core 中 HostingEnvironment.QueueBackgroundWorkItem 的替代解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我们正在使用 .NET Core Web Api,并寻找一种轻量级的解决方案来将可变强度的请求记录到数据库中,但不希望客户端等待保存过程.
                  不幸的是,在 dnx 中没有实现 HostingEnvironment.QueueBackgroundWorkItem(..),并且 Task.Run(..) 并不安全.
                  有什么优雅的解决方案吗?

                  We are working with .NET Core Web Api, and looking for a lightweight solution to log requests with variable intensity into database, but don't want client's to wait for the saving process.
                  Unfortunately there's no HostingEnvironment.QueueBackgroundWorkItem(..) implemented in dnx, and Task.Run(..) is not safe.
                  Is there any elegant solution?

                  推荐答案

                  QueueBackgroundWorkItem 不见了,但我们得到的是 IApplicationLifetime 而不是 IRegisteredObject,这是前一个正在使用的.我认为,在这种情况下,它看起来很有希望.

                  QueueBackgroundWorkItem is gone, but we've got IApplicationLifetime instead of IRegisteredObject, which is being used by the former one. And it looks quite promising for such scenarios, I think.

                  这个想法(我仍然不太确定,如果它是一个非常糟糕的一个;因此,当心!)是注册一个单例,它产生 观察新任务.在该单例中,我们还可以注册一个停止事件",以便正确等待仍在运行的任务.

                  The idea (and I'm still not quite sure, if it's a pretty bad one; thus, beware!) is to register a singleton, which spawns and observes new tasks. Within that singleton we can furthermore register a "stopped event" in order to proper await still running tasks.

                  这个概念"可用于短期运行的东西,如日志记录、邮件发送等.事情,应该不会花费太多时间,但会对当前请求产生不必要的延迟.

                  This "concept" could be used for short running stuff like logging, mail sending, and the like. Things, that should not take much time, but would produce unnecessary delays for the current request.

                  public class BackgroundPool
                  {
                      protected ILogger<BackgroundPool> Logger { get; }
                  
                      public BackgroundPool(ILogger<BackgroundPool> logger, IApplicationLifetime lifetime)
                      {
                          if (logger == null)
                              throw new ArgumentNullException(nameof(logger));
                          if (lifetime == null)
                              throw new ArgumentNullException(nameof(lifetime));
                  
                          lifetime.ApplicationStopped.Register(() =>
                          {
                              lock (currentTasksLock)
                              {
                                  Task.WaitAll(currentTasks.ToArray());
                              }
                  
                              logger.LogInformation(BackgroundEvents.Close, "Background pool closed.");
                          });
                  
                          Logger = logger;
                      }
                  
                      private readonly object currentTasksLock = new object();
                  
                      private readonly List<Task> currentTasks = new List<Task>();
                  
                      public void SendStuff(Stuff whatever)
                      {
                          var task = Task.Run(async () =>
                          {
                              Logger.LogInformation(BackgroundEvents.Send, "Sending stuff...");
                  
                              try
                              {
                                  // do THE stuff
                  
                                  Logger.LogInformation(BackgroundEvents.SendDone, "Send stuff returns.");
                              }
                              catch (Exception ex)
                              {
                                  Logger.LogError(BackgroundEvents.SendFail, ex, "Send stuff failed.");
                              }
                          });
                  
                          lock (currentTasksLock)
                          {
                              currentTasks.Add(task);
                  
                              currentTasks.RemoveAll(t => t.IsCompleted);
                          }
                      }
                  }
                  

                  这样的 BackgroundPool 应该注册为单例,并且可以通过 DI 被任何其他组件使用.我目前正在使用它来发送邮件,它工作正常(在应用关闭期间也测试过邮件发送).

                  Such a BackgroundPool should be registered as a singleton and can be used by any other component via DI. I'm currently using it for sending mails and it works fine (tested mail sending during app shutdown too).

                  注意: 在后台任务中访问诸如当前 HttpContext 之类的东西应该不起作用.旧解决方案 使用 UnsafeQueueUserWorkItem无论如何都要禁止.

                  Note: accessing stuff like the current HttpContext within the background task should not work. The old solution uses UnsafeQueueUserWorkItem to prohibit that anyway.

                  你怎么看?

                  更新:

                  有了 ASP.NET Core 2.0,后台任务有了新的东西,ASP.NET Core 2.1 变得更好:使用 IHostedService 和 BackgroundService 类在 .NET Core 2.x webapps 或微服务中实现后台任务

                  With ASP.NET Core 2.0 there's new stuff for background tasks, which get's better with ASP.NET Core 2.1: Implementing background tasks in .NET Core 2.x webapps or microservices with IHostedService and the BackgroundService class

                  这篇关于.NET Core 中 HostingEnvironment.QueueBackgroundWorkItem 的替代解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:我可以先使用 EF 代码和 .net 核心生成迁移脚本吗 下一篇:在 ASP.NET MVC Core 中使用枚举作为下拉列表

                  相关文章

                  1. <tfoot id='emacA'></tfoot>

                    <small id='emacA'></small><noframes id='emacA'>

                    <legend id='emacA'><style id='emacA'><dir id='emacA'><q id='emacA'></q></dir></style></legend>

                  2. <i id='emacA'><tr id='emacA'><dt id='emacA'><q id='emacA'><span id='emacA'><b id='emacA'><form id='emacA'><ins id='emacA'></ins><ul id='emacA'></ul><sub id='emacA'></sub></form><legend id='emacA'></legend><bdo id='emacA'><pre id='emacA'><center id='emacA'></center></pre></bdo></b><th id='emacA'></th></span></q></dt></tr></i><div id='emacA'><tfoot id='emacA'></tfoot><dl id='emacA'><fieldset id='emacA'></fieldset></dl></div>
                      <bdo id='emacA'></bdo><ul id='emacA'></ul>