Get Hangfire working with ASP.NET MVC and LightInject

494 Views Asked by At

I have an ASP.NET MVC App and I recently upgraded to use LightInject DI. However I cant seem to get Hangfire running correctly, even using the LightInject Extension!

My Hangfire setup in Startup.cs:

        public void Configuration(IAppBuilder app)
        {
            var container = new ServiceContainer();
            container.RegisterControllers(typeof(Web.Controllers.DashboardController).Assembly);

            ConfigureServices(container);

            ConfigureHangfire(container,app);

            container.EnableMvc();

        }

        private void ConfigureHangfire(ServiceContainer container, IAppBuilder app)
        {
            var hangfireConnString = ConfigurationManager.ConnectionStrings["HfConnString"].ConnectionString;

            GlobalConfiguration.Configuration
                .SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
                .UseSimpleAssemblyNameTypeSerializer()
                .UseRecommendedSerializerSettings()
                .UseLightInjectActivator(container)
                .UseSqlServerStorage(hangfireConnString, new SqlServerStorageOptions
                {
                    CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
                    SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
                    QueuePollInterval = TimeSpan.FromSeconds(10),
                    UseRecommendedIsolationLevel = true,
                    UsePageLocksOnDequeue = true,
                    DisableGlobalLocks = true
                });

            var options = new DashboardOptions()
            {
                Authorization = new[] {new SystemAuthorizationFilter()}
            };
            app.UseHangfireDashboard("/hangfire",options);

            app.UseHangfireServer();
        }

However I get the following error when running a hangfire job:

System.NullReferenceException: Object reference not set to an instance of an object.
   at LightInject.Web.PerWebRequestScopeManager.GetOrAddScope() in C:\projects\lightinject-web\build\tmp\Net46\Binary\LightInject.Web\LightInject.Web.cs:line 148
   at LightInject.Web.PerWebRequestScopeManager.get_CurrentScope() in C:\projects\lightinject-web\build\tmp\Net46\Binary\LightInject.Web\LightInject.Web.cs:line 129
   at LightInject.ScopeManager.BeginScope() in C:\projects\lightinject\src\LightInject\LightInject.cs:line 6091

I would love any help to get this going! Thanks so much in advance.

1

There are 1 best solutions below

0
dalcam On

I actually fixed this by giving hangfire its own container. So the start of my ConfigureHangfire method became:

        private void ConfigureHangfire(ServiceContainer container, IAppBuilder app)
        {
            var hangfireConnString = ConfigurationManager.ConnectionStrings["HfConnString"].ConnectionString;

            var container = new ServiceContainer();
            ConfigureServices(container);

            GlobalConfiguration.Configuration etc....

Im not sure that this is entirely correct, and if its not i would really like to be corrected! But in any case I hope this helps someone!