Base solution for your next web application
Open Closed

dotnet-core, angular - has anyone integrated Hangfire #5156


User avatar
0
BobIngham created

Anyone - best practices for integrating Hangfire in dotnet-core, angular version. Follow the instructions at [https://aspnetboilerplate.com/Pages/Documents/Hangfire-Integration]). Normally in an MVC project I place this in StartUp.cs:

Services.Hangfire.ConfigureHangfire(app);

And then in my services layer I would place the service:

public class Hangfire
    {
        public static void ConfigureHangfire(IAppBuilder app)
        {
            GlobalConfiguration.Configuration.UseSqlServerStorage("DefaultConnection");

            //see https://blog.entelect.co.za/view/7580/dependency-injection-with-hangfire
            var hangfireContainer = new UnityContainer();
            GlobalConfiguration.Configuration.UseActivator(new UnityJobActivator(hangfireContainer));

            app.UseHangfireServer();
            app.UseHangfireDashboard("/backgroundjobs", new DashboardOptions
            {
                Authorization = new[] { new HangfireAuthorization() }
            });

            if (ConfigurationManager.AppSettings["IsProduction"] == "true")
            {
                InitializeJobs();
            }
        }

        public static void InitializeJobs()
       {
            //place any recurring jobs here....
            RecurringJob.AddOrUpdate<Services.Workers.RebuildCoursesAndEventsJob>(job => job.Execute(), "0 7  * * *");
        }
    }

    public class HangfireAuthorization : IDashboardAuthorizationFilter
    {
        public bool Authorize([NotNull] DashboardContext context)
        {
            if (HttpContext.Current.User != null && HttpContext.Current.User.Identity.IsAuthenticated)
            {
                var authorized = (HttpContext.Current.User.IsInRole("Developer") || HttpContext.Current.User.IsInRole("Admin"));
                return authorized;
            }
            return false;
        }
    }

And RebuildCoursesAndEventsJob would be run as follows (note: the system uses CQS):

public class RebuildCoursesAndEventsJob
    {
        private static readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private ICommandHandler<RebuildCoursesAndEventsCommand> _rebuildCoursesAndEvents;

        public RebuildCoursesAndEventsJob(
            ICommandHandler<RebuildCoursesAndEventsCommand> rebuildCoursesAndEvents)
        {
            _rebuildCoursesAndEvents = rebuildCoursesAndEvents;
        }

        public void Execute()
        {
            logger.Info("executing job: " + DateTime.Now.ToShortTimeString(), null);
            using (DomainContext _db = new DomainContext())
            {
                var service = new RebuildCoursesAndEventsCommand();
                _rebuildCoursesAndEvents.Handle(service);
                return;
            }
        }
    }

Now how the hell would I do that in Zero? I get the part about not being able to use the dashboard because of MVC's cookies. I'm not sure how Castle works, I had to create a UnityContainer for Hangfire when placing it in my service project in MVC as the code above shows. So the question is;

what do I place in my Startup.cs to fire the service, where do I put my service, where do I place my workers (jobs) and what code do I need to make it all work with Zero and Castle?


1 Answer(s)