Thumb

Part-1: Hangfire Scheduler configuration into ASP.NET MVC | C# | Schedule Job

11/5/2019 6:59:50 AM

Download Project  (Full Project)

Step-1

In this part i will show how to Hangfire Scheduler configuration into ASP.NET MVC. Now  Open visual studio then create ASP.NET Web application. You can change project name and solution name. Then select MVC and click ok. Visual studio creates a default project for you. Then you need to connect database by the connection string.  Now go package manager console then type “Install-Package HangFire -Version 1.7.6” then Enter. Then this package will be Install and  go to Startup.cs file. Given bellow the code:

using exampleOfHangfire.Models;
using Hangfire;
using Microsoft.Owin;
using Owin;
using System;

[assembly: OwinStartupAttribute(typeof(exampleOfHangfire.Startup))]
namespace exampleOfHangfire
{
    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            ConfigureAuth(app);
            GlobalConfiguration.Configuration.UseSqlServerStorage("DefaultConnection");
            app.UseHangfireDashboard("/myJobDashboard", new DashboardOptions() {
                Authorization = new[] { new HangfireAthorizationFilter()}
            });
            //BackgroundJob.Enqueue(() => Console.WriteLine("Fire-and-forget!"));
            RecurringJob.AddOrUpdate(() => Console.WriteLine("Recurring!"),Cron.Minutely);
            app.UseHangfireServer();
        }
    }
}

Step-2

Controller are pass the value in the view and represent the data. In this part controller call the HangFire pre define method it is responsible to all dynamic work. Now given bellow the controller code:

using Hangfire;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace exampleOfHangfire.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}

Step-3

View is nothing but represent the content. This content came from  the home controller and also configure the  Startup.cs file. Given bellow the View code:

@{
    ViewBag.Title = "Home Page";
}

<div class="jumbotron">
    <h1>ASP.NET</h1>
    <p class="lead">ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS and JavaScript.</p>
    <p><a href="http://asp.net" class="btn btn-primary btn-lg">Learn more &raquo;</a></p>
</div>

<div class="row">
    <div class="col-md-4">
        <h2>Getting started</h2>
        <p>
            ASP.NET MVC gives you a powerful, patterns-based way to build dynamic websites that
            enables a clean separation of concerns and gives you full control over markup
            for enjoyable, agile development.
        </p>
        <p><a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkId=301865">Learn more &raquo;</a></p>
    </div>
    <div class="col-md-4">
        <h2>Get more libraries</h2>
        <p>NuGet is a free Visual Studio extension that makes it easy to add, remove, and update libraries and tools in Visual Studio projects.</p>
        <p><a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkId=301866">Learn more &raquo;</a></p>
    </div>
    <div class="col-md-4">
        <h2>Web Hosting</h2>
        <p>You can easily find a web hosting company that offers the right mix of features and price for your applications.</p>
        <p><a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkId=301867">Learn more &raquo;</a></p>
    </div>
</div>

Step-4

Now run the project.

About Teacher

Image

Reza Karim

Software Engineer

More about him