locked
Need help on Hangfire configuration RRS feed

  • Question

  • User-1279939347 posted

    Hi All, 

    I need to configure Hangfire for my existing application which is having framework 4.5. I'm using Microsoft.Practices.Unity dependency injection in my application. With that i want to implement Hangfire for background process methods. So please share any examples of Hangfire configuration with Microsoft.Practices.Unity. 

    Thank you

    Kishore

    Friday, September 18, 2020 2:30 PM

All replies

  • User1686398519 posted

    Hi kishore309, 

    According to your needs, I wrote a simple example, you can refer to it.

    1. You need to install Hangfire and Hangfire.Unity, and then configure Hangfire.In the example I given, Hangfire is configured in the Startup.cs file.
      • If you don't have a Startup.cs file, you can right-click the project and select New Item, and then search for OWIN Startup Class to create a Startup.cs file.
      • You need to register the container as a current job activator before starting the Hangfire server.
      • The example uses the EF framework ,you can modify it according to your own needs.
      • You can use RecurringJobManager to operate recurring jobs.
    2. You can check Hangfire official documentation to learn more.

    Startup

        public class Startup
        {
            private IEnumerable<IDisposable> GetHangfireServers()
            {
                GlobalConfiguration.Configuration
                    .SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
                    .UseSimpleAssemblyNameTypeSerializer()
                    .UseRecommendedSerializerSettings()
                    .UseSqlServerStorage("UnityFrameworkMVCDemoContext", new SqlServerStorageOptions
                    {
                        CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
                        SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
                        QueuePollInterval = TimeSpan.Zero,
                        UseRecommendedIsolationLevel = true,
                        DisableGlobalLocks = true
                    })
                    .UseActivator(new ContainerJobActivator(UnityConfig.Container));
                yield return new BackgroundJobServer();
            }
            public void Configuration(IAppBuilder app)
            {
                app.UseHangfireAspNet(GetHangfireServers);
                app.UseHangfireDashboard(); 
            }
        }

    ContainerJobActivator

        public class ContainerJobActivator : JobActivator
        {
            private IUnityContainer _container;
            public ContainerJobActivator(IUnityContainer container)
            {
                container.RegisterType<IUserService, UserService>();
                _container = container;
            }
    
            public override object ActivateJob(Type type)
            {
                return _container.Resolve(type);
            }
        }

    UnityConfig

        public static class UnityConfig
        {
            #region Unity Container
            private static Lazy<IUnityContainer> container =
              new Lazy<IUnityContainer>(() =>
              {
                  var container = new UnityContainer();
                  RegisterTypes(container);
                  return container;
              });
            public static IUnityContainer Container => container.Value;
            #endregion
            public static void RegisterTypes(IUnityContainer container)
            {
            }
        }

    IUserService

        public interface IUserService
        {
            void test();
        }

    UserService

        public class UserService : IUserService
        {
            public void test()
            {
                Debug.WriteLine("Hello world from Hangfire!");
            }
        }

    HomeController

        public class HomeController : Controller
        {
            private IUserService userService;
    
            public HomeController(IUserService userService)
            {
                this.userService = userService;
            }
            public ActionResult Index()
            {
                var manager = new RecurringJobManager();
                manager.AddOrUpdate("1", Job.FromExpression(() => userService.test()), "*/30 * * * * ?", TimeZoneInfo.Local);
                return View();
            }
        }

    Here is the result.

    Best Regards,

    YihuiSun

    Monday, September 21, 2020 8:34 AM