0

我有一个 ASP.NET MVC 应用程序,我最近升级为使用 LightInject DI。但是,即使使用 LightInject 扩展,我似乎也无法让 Hangfire 正确运行!

我在 Startup.cs 中的 Hangfire 设置:

        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();
        }

但是,在运行 hangfire 作业时出现以下错误:

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

我希望能得到任何帮助来实现这一目标!提前非常感谢。

4

1 回答 1

0

我实际上通过给hangfire自己的容器来解决这个问题。所以我的 ConfigureHangfire 方法的开始变成了:

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

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

            GlobalConfiguration.Configuration etc....

我不确定这是否完全正确,如果不是,我真的很想纠正!但无论如何,我希望这对某人有所帮助!

于 2019-10-14T19:31:53.707 回答