<legend id='iV4xA'><style id='iV4xA'><dir id='iV4xA'><q id='iV4xA'></q></dir></style></legend>
<tfoot id='iV4xA'></tfoot>

    • <bdo id='iV4xA'></bdo><ul id='iV4xA'></ul>

      1. <small id='iV4xA'></small><noframes id='iV4xA'>

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

        在 C# 中延迟任务 - 数据库上下文已处置

        时间:2023-10-07

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

                <legend id='rtFtf'><style id='rtFtf'><dir id='rtFtf'><q id='rtFtf'></q></dir></style></legend>
                  <bdo id='rtFtf'></bdo><ul id='rtFtf'></ul>

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

                  本文介绍了在 C# 中延迟任务 - 数据库上下文已处置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我需要在某个时间执行某些代码(在我的 ASP .NET Core 项目中).

                  I have a situation where I need certain code to execute at a certain time (in my ASP .NET Core project).

                  我知道延迟任务不是一个很好的解决方案,但这就是我所拥有的,我想知道如何让它发挥作用:

                  I know that delaying a task is not a great solution, but this is what I have and I'd like to know how to make it work:

                  async Task MyMethod()
                  {
                    // do something
                    // Create a new thread that waits for the appropriate time
                        TimeSpan time = dbAppointment.ScheduledOn - TimeSpan.FromMinutes(5.0) - DateTime.UtcNow;
                        _ = Task.Delay(time).ContinueWith(async x => await 
                            notificationManager.CreateReminder());
                    // continue doing something
                  }
                  

                  当我尝试运行代码时,它会在正确的时间进入应该执行的方法:

                  When I try to run the code, it enters the method that is supposed to be executed, at the right time:

                  public async Task CreateReminder() {}
                  

                  但是当它尝试使用我使用 DI 注入到 NotificationManager 构造函数中的 dbContext 时失败,说明它已被释放.

                  but fails when it tries to use my dbContext which I injected using DI into the NotificationManager constructor, stating that it was disposed.

                  这就是流";依赖项:

                  public class MyClass
                  {
                    private readonly MyContext dbContext;
                    private readonly INotificationManager notificationManager;
                    public MyClass(MyContext context, INotificationManager nm) 
                    { 
                      dbContext = context;
                      notificationManager = nm;
                    }
                  
                    public async Task MyMethod() // the method shown in the snippet above
                    { 
                      // method does something using the dbContext
                  
                      _ = Task.Delay(time).ContinueWith(async x => await 
                            notificationManager.CreateReminder());
                    }
                  }
                  
                  public class NotificationManager: INotificationManager
                  {
                    private readonly MyContext dbContext;
                    public NotificationManager(MyContext context) { dbContext = context;}
                    public async Task CreateReminder() { // this method uses the dbContext}
                  }
                  

                  startup.cs 中的 DI 设置:

                  DI setup in startup.cs:

                  services.AddDbContext<MyContext>(); 
                  services.AddScoped<INotificationManager, NotificationManager>();
                  

                  推荐答案

                  选项

                  • 使用作业调度程序(如 Hangfire、Quartz.Net, Jobbr,...)
                  • 使用 后台服务 如果你的 .net 核心版本是 >= 2
                  • Use a job scheduler (Like Hangfire, Quartz.Net, Jobbr, ...)
                  • Use a background service if your .net core version is >= 2

                  在这两种情况下,您都需要在作业类中注入 DatabaseContext,否则您将收到 ObjectDisposedException.

                  In both cases you'll need to inject the DatabaseContext in the job class otherwise you'll receive an ObjectDisposedException.

                  当您需要横向扩展至多台机器时,您需要一个带有状态存储的作业服务器,例如 SQL Server、MSMQ、RabbitMQ、Redis...

                  When you need to scale-out to multiple machines you'll need a job server with a state store like SQL Server, MSMQ, RabbitMQ, Redis,...

                  Hangfire 示例

                  Sample with Hangfire

                  public class MyDelayJob
                  {
                     private readonly MyContext dbContext;
                     private readonly INotificationManager notificationManager;
                     public MyDelayJob(MyContext context, INotificationManager nm)
                     {
                         dbContext= context;
                         notificationManager = nm;
                     }
                  
                     public async Task Run(/*parameters*/)
                     {
                        await notificationManager.CreateReminder()
                     }
                  }
                  
                  /*Shedule code in MyMethod
                      IBackgroundJobClient can be injected
                      you need to register MyDelayJob with your IOC container.
                   */
                  
                  backgroundJobClient.Schedule<MyDelayJob>(j => j.Run(), TimeSpan.FromSeconds(60))
                  

                  请参阅 IBackgroundJobClient

                  这篇关于在 C# 中延迟任务 - 数据库上下文已处置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:用于服务器的 C# GC 下一篇:C# 基本程序中的析构函数不起作用(输出缺失)

                  相关文章

                • <small id='8Q76n'></small><noframes id='8Q76n'>

                  <tfoot id='8Q76n'></tfoot>

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

                      <legend id='8Q76n'><style id='8Q76n'><dir id='8Q76n'><q id='8Q76n'></q></dir></style></legend>