This is in continuation to my previous post: Hangfire with ASP.Net MVC : Simplest Code To Start with.
In this post we will see how Hangfire can be configured without the SQL Server as its backend for job scheduling, instead, manage all in-memory
1) Create ASP.Net MVC 4/5 Projects
2) 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
3) Run Install-Package Hangfire.MemoryStorage -Version 1.5.2 or visit here for latest version https://www.nuget.org/packages/Hangfire.MemoryStorage/
4) I am simply updating a table with a primary key (auto incremented), and string field "StampTime" with current date and time.
5) Open Global.asax.cs and paste below shown code.
6) Run the app and check the database.
In this post we will see how Hangfire can be configured without the SQL Server as its backend for job scheduling, instead, manage all in-memory
1) Create ASP.Net MVC 4/5 Projects
2)
3) Run Install-Package HangFire -Version 1.6.17 or visit here for latest version: https://www.nuget.org/packages/Hangfire/1.6.17
3) Run Install-Package Hangfire.MemoryStorage -Version 1.5.2 or visit here for latest version https://www.nuget.org/packages/Hangfire.MemoryStorage/
4) I am simply updating a table with a primary key (auto incremented), and string field "StampTime" with current date and time.
5) Open Global.asax.cs and paste below shown code.
6) Run the app and check the database.
using Hangfire; using Hangfire.Common; using Hangfire.MemoryStorage; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Web; using System.Web.Hosting; using System.Web.Mvc; using System.Web.Optimization; using System.Web.Routing; using System.Web.Services.Description; namespace HangFireDemo { public static class MyTasks { public static async Task<int> MinuteTick() { await Task.Run(() => { BackgroundJob.Schedule(() => MyTasks.Tick(), TimeSpan.FromSeconds(30)); }); 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.UseMemoryStorage(); _backgroundJobServer = new BackgroundJobServer(); RecurringJob.AddOrUpdate(() => MyTasks.MinuteTick(), Cron.Minutely); } protected void Application_End(object sender, EventArgs e) { _backgroundJobServer.Dispose(); } } }
Comments