相当于asp.net框架中用于后台任务的Ihostedservice [英] Equivalent of Ihostedservice in asp.net framework for background tasks

查看:88
本文介绍了相当于asp.net框架中用于后台任务的Ihostedservice的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 .net 4.6.2 中有一个宁静的微服务(web api),我想每次在调用某些端点来做一些数据库清理工作后调用一个触发和忘记函数.我不想使用 Task.Run,​​我想有一个非常可靠的专用进程来完成这项工作.

I have a restful micro service (web api) in .net 4.6.2 and I want to call a fire and forget function each time after certain endpoints are called to do some database cleanup work. I don' want to use Task.Run and I want to have a much reliable dedicated process to do this job.

我发现了很多关于如何在 .net core 中使用 IhostedService 来处理长时间运行的后台任务的文章和讨论.

I found lot's of articles and discussions about how to use IhostedService in .net core for long running background tasks.

  1. 使用 IHostedService 和 BackgroundService 类在微服务中实现后台任务
  2. 使用 IHostedService 进行 ASP.NET Core 后台处理
  3. 后台任务ASP.NET Core 中的托管服务

但是,我在 .net 框架中没有找到太多有关等效项的信息.那是 IRegisteredObject 吗?有人可以帮助我或指导我使用正确的资源吗.

However I didn't find much information about the equivalent in .net framework. Is that IRegisteredObject? Can somebody please help me or direct me to the right resources.

注意:我们将 API 作为 Windows 服务托管,不使用 IIS

Notes: We host the API as a windows service and don't use IIS

这是我第一次在这里提问,如果问题不够清晰,请见谅.

推荐答案

我想这有点太晚了,但对于有同样问题的人..

I guess it's a bit too late, but for someone with the same issue..

你知道 Quartz.NETHangfire.io ?

在您的情况下,这两者中的一个可能是非常有用的工具.我在许多应用程序中都使用过它,但从未遇到任何问题.

Maybe one of those both could be a very useful tool in your situation. I used it in many applications, and never had any issue.

例如,在 Quartz.Net 中,您首先必须创建一个作业";(这是用于这种后台服务的术语)通过创建一个实现 IJob 接口的类:

For example, in Quartz.Net, you first have to create a "job" (it's the term used for this kind of background services) by creating a class implementing IJob interface:

public class HelloJob : IJob
{
    public async Task Execute(IJobExecutionContext context)
    {
        await Console.Out.WriteLineAsync("Greetings from HelloJob!");
    }
}

然后,您必须定义何时要检查该作业,有很多方法(例如 CRON),但我们将在这里使用一个简单的时间表:

Then, you have to define when you want to check that job, there are many ways (CRON for example) but we just gonna use a simple schedule here :

        StdSchedulerFactory factory = new StdSchedulerFactory();
        IScheduler scheduler = await factory.GetScheduler();

        await scheduler.Start();

        // define the job
        IJobDetail job = JobBuilder.Create<HelloJob>()
            .WithIdentity("job1", "group1")
            .Build();

        // Trigger the job to run now, and then repeat every 20 seconds
        ITrigger trigger = TriggerBuilder.Create()
            .WithIdentity("trigger1", "group1")
            .StartNow()
            .WithSimpleSchedule(x => x
                .WithIntervalInSeconds(20)
                .RepeatForever())
            .Build();

        // Tell quartz to schedule the job using our trigger, DON'T FORGET THIS ONE
        await scheduler.ScheduleJob(job, trigger);

您处于基于 Windows 服务的微服务架构中.你应该能够捕捉到所有优雅"的关闭您的应用程序.在这些情况下,您必须正确关闭 Quartz 调度程序:

You are in a micro services architecture based on windows service. You should be able to catch all "graceful" shutdown of your application. In these cases, you have to shutdown properly the Quartz scheduler :

await scheduler.Shutdown();

我个人非常喜欢这些方法.它是可重复使用的(您只需在此处安排一项新工作)并且您团队中的任何开发人员都可以轻松使用它.

I personally really like these kinds of approach. It's reusable (you just have to schedule a new job here) and easy to get into that for any developer on your team.

希望对您的问题有所帮助.

I hope it will help you a bit with your issue.

这篇关于相当于asp.net框架中用于后台任务的Ihostedservice的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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