0

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.

dalcam
  • 1,027
  • 11
  • 28
  • Im wondering if hangfire should have its own container? Not sure if that would solve the issue... – dalcam Oct 14 '19 at 19:20
  • This looks more like ASP.NET Core than ASP.NET MVC - if that's the case, that's the wrong tag on your question. – Tieson T. Oct 16 '19 at 06:16
  • 1
    Hi @TiesonT. its definately ASP.NET MVC and not core :) – dalcam Oct 16 '19 at 06:17
  • 1
    That error is happening because they're trying to read HttpContext.Current ([source](https://github.com/seesharper/LightInject.Web/blob/master/src/LightInject.Web/LightInject.Web.cs#L148)) - HttpContext is most likely null at that point, given how Hangfire runs internally. Not sure why giving Hangfire it's own container fixes the problem, unless it means that HttpContext is then scoped to Hangfire and not the "host" application (which kind of makes sense). – Tieson T. Oct 16 '19 at 06:34
  • Hi @TiesonT my hangfire job wasn't referencing HttpContext.current. It was calling one of my Injected Services, which also wasn't using http.context. I assumed it wasnt finding my injected services. – dalcam Nov 04 '19 at 05:16
  • 1
    You may not be, but LightInject does seem to (that link is to their source code). – Tieson T. Nov 04 '19 at 06:03
  • Ah yes sorry @TiesonT, i didnt read your comment correctly. That is interesting. Thanks for the comments and your time! Really appreciated! – dalcam Nov 04 '19 at 20:31
  • Not need to apologize - I'm not entirely sure I'm correct, but I've seen similar issues with code that makes assumptions that HttpContext is available when the code is actually called. – Tieson T. Nov 05 '19 at 01:49

1 Answers1

0

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!

dalcam
  • 1,027
  • 11
  • 28