Asp.Net 之HangFire定时任务配置使用

1.程序集

Install-Package Hangfire.Core
Install-Package Hangfire.SqlServer
Install-Package Hangfire.AspNet

2.web.config加配置

<connectionStrings>
  <add name="sqlserver_connection" connectionString="Data Source=.;Initial Catalog=HangFire;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>

3.项目右击添加项目 Own Startup.cs

using Hangfire;
using Microsoft.Owin;
using Owin;
using System;
using System.Threading.Tasks;

[assembly: OwinStartup(typeof(HangFireDemo.Startup))]

namespace HangFireDemo
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            //有关如何配置应用程序的详细信息,请访问 https://go.microsoft.com/fwlink/?LinkID=316888
            //运用SqlServer存储,对应web.config中的connectionStrings中的name
            GlobalConfiguration.Configuration.UseSqlServerStorage("sqlserver_connection"); 

            //注意,当你使用的是MySql作为存储时,需要如下配置
            //运用MySql存储,对应web.config中的connectionStrings中的name
            //GlobalConfiguration.Configuration.UseStorage(new MySqlStorage("mysql_connection"));

            app.UseHangfireDashboard();//配置后台仪表盘
            app.UseHangfireServer();//开始使用Hangfire服务
        }
    }
}

4.浏览器访问地址:

http://localhost:4046/hangfire/

 

实现效果:

 sqlserver数据库会生成表结构:

 

5.附Gitee源代码

源代码

 

posted on 2024-10-29 11:06  RookieBoy666  阅读(6)  评论(0编辑  收藏  举报