我有一个 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
我希望能得到任何帮助来实现这一目标!提前非常感谢。