It seems many are looking for using Hangfire with ASP.Net MVC 4 / 5+ SQL Server here is the simplest code example for you to start quick.
I am using Visual Studio 2017 Community Edition
1) Create ASP.Net MVC 4/5 Projects
2) <add key="owin:AutomaticAppStartup" value="false" /> in web.config file
3)Run Install-Package HangFire -Version 1.6.17 or visit here for latest version: https://www.nuget.org/packages/Hangfire/1.6.17
4) From the sample code, make sure you create database in SQL Server DB and update the connection string.
5) I am simply updating a table with a primary key (auto incremented), and string field "StampTime" with current date and time.
6) Open Global.asax.cs and put this code.
7) Run the app and check the database.
The example code also showcase a hack on how to run jobs with few seconds interval.
I am using Visual Studio 2017 Community Edition
1) Create ASP.Net MVC 4/5 Projects
2) <add key="owin:AutomaticAppStartup" value="false" /> in web.config file
3)Run Install-Package HangFire -Version 1.6.17 or visit here for latest version: https://www.nuget.org/packages/Hangfire/1.6.17
4) From the sample code, make sure you create database in SQL Server DB and update the connection string.
5) I am simply updating a table with a primary key (auto incremented), and string field "StampTime" with current date and time.
6) Open Global.asax.cs and put this code.
7) Run the app and check the database.
The example code also showcase a hack on how to run jobs with few seconds interval.
namespace HangFireDemo { public static class MyTasks { public static async Task<int> MinuteTick() { await Task.Run(() => { BackgroundJob.Schedule(() => MyTasks.Tick(), TimeSpan.FromSeconds(15)); BackgroundJob.Schedule(() => MyTasks.Tick(), TimeSpan.FromSeconds(30)); BackgroundJob.Schedule(() => MyTasks.Tick(), TimeSpan.FromSeconds(45)); BackgroundJob.Schedule(() => MyTasks.Tick(), TimeSpan.FromSeconds(59)); }); return 1; } public static async Task<int> Tick() { await Task.Run(() => { using (HangFireDemo.Models.HangFireDBEntities db = new Models.HangFireDBEntities()) { db.Tracks.Add(new Models.Track { StampTime = DateTime.Now.ToString("dd-MMM-yyyy hh:mm:ss tt") }); db.SaveChanges(); } }); return 1; } } public class MvcApplication : System.Web.HttpApplication { private BackgroundJobServer _backgroundJobServer; protected void Application_Start() { AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); GlobalConfiguration.Configuration .UseSqlServerStorage(@"Server=.\sqlexpress2012; Database = hangfiredb; User Id = sa;Password = system456*;"); _backgroundJobServer = new BackgroundJobServer(); RecurringJob.AddOrUpdate(() => MyTasks.MinuteTick(), Cron.Minutely); } protected void Application_End(object sender, EventArgs e) { _backgroundJobServer.Dispose(); } } }
If you really want to witness documentation that looks super good and tells you too many things that you feel not-required at the beginning, read the Hangfires official long startup guide: http://docs.hangfire.io/en/latest/tutorials/send-email.html.
Please note, this is just a quick get-start sample code for learning and may not be production ready.
Also check my followup post: Hangfire with In-Memory Storage (ASP.Net MVC 4/5)
Also check my followup post: Hangfire with In-Memory Storage (ASP.Net MVC 4/5)
Comments