[英]Get Hangfire working with ASP.NET MVC and LightInject
我有一個 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
我希望得到任何幫助來實現這一目標。 提前非常感謝。
我實際上通過給hangfire自己的容器來解決這個問題。 所以我的 ConfigureHangfire 方法的開始變成了:
private void ConfigureHangfire(ServiceContainer container, IAppBuilder app)
{
var hangfireConnString = ConfigurationManager.ConnectionStrings["HfConnString"].ConnectionString;
var container = new ServiceContainer();
ConfigureServices(container);
GlobalConfiguration.Configuration etc....
我不確定這是否完全正確,如果不是,我真的很想糾正! 但無論如何,我希望這對某人有所幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.