![](/img/trans.png)
[英]Is there any better way using hangfire with structuremap on ASP.net core?
[英]How to configure hangfire in asp.net core using structuremap targeting .net framework and resolve bi-directional dependency
我一直在用頭撞牆,不知道發生了什么。
我有一個針對 .NET 4.7.2 的 asp.net 核心 web 項目,我一直在嘗試配置 hangfire。
public IServiceProvider ConfigureServices(IServiceCollection services)
{
var connectionString = Configuration.GetConnectionString("DocumentDbConnection");
services.AddMvc(o =>
{
o.Filters.Add(typeof(HttpGlobalExceptionFilter));
})
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services
.AddHangfire(x => x.UseSqlServerStorage(connectionString));
// configure StructureMap Dependency Injection
var container = new Container();
container.Configure(config =>
{
config.Scan(scan =>
{
scan.TheCallingAssembly();
scan.AssembliesAndExecutablesFromApplicationBaseDirectory();
scan.WithDefaultConventions();
scan.AddAllTypesOf(typeof(IIntegrationEventHandler<>));
});
// other configurations
config.Populate(services);
});
// tried this too
//GlobalConfiguration.Configuration.UseStructureMapActivator(container);
return container.GetInstance<IServiceProvider>();
}
和我的配置方法:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IContainer container)
{
//using Hangfire.StructureMap
GlobalConfiguration.Configuration.UseStructureMapActivator(container);
// exception is thrown on UsingHangfireServer()
app
.UseHangfireServer()
.UseMvc();
}
我不斷從 StructureMap 收到關於雙向依賴的異常:
Bi-directional dependency relationship detected!
Check the StructureMap stacktrace below:
1.) Instance of Hangfire.Client.IBackgroundJobFactory ('Hangfire.Client.BackgroundJobFactory, Hangfire.Core, Version=1.7.0.0, Culture=neutral, PublicKeyToken=null')
2.) new BackgroundJobFactory(*Default of IJobFilterProvider*, *Default of IBackgroundJobFactory*)
3.) Hangfire.Client.BackgroundJobFactory ('Hangfire.Client.BackgroundJobFactory, Hangfire.Core, Version=1.7.0.0, Culture=neutral, PublicKeyToken=null')
4.) Instance of Hangfire.Client.IBackgroundJobFactory ('Hangfire.Client.BackgroundJobFactory, Hangfire.Core, Version=1.7.0.0, Culture=neutral, PublicKeyToken=null')
5.) Container.GetInstance(Hangfire.Client.IBackgroundJobFactory)
6.) Container.TryGetInstance(Hangfire.Client.IBackgroundJobFactory)
我使用包 Hangfire.AspNetCore、Hangfire.SqlServer、Hangfire.StructureMap,所有 v 1.7.0
我也嘗試過只使用 Hangfire 包,但無法在我的啟動類中配置它,因為沒有任何擴展方法可用。
另請注意,這種確切的設置在針對 .net 核心的項目上工作得很好。
任何建議將不勝感激!
原來這是結構圖配置中這一行的問題:
scan.AssembliesAndExecutablesFromApplicationBaseDirectory();
我應該使用這樣的東西:
scan.AssemblyContainingType<IThumbnailGenerator>(); // directing structuremap to scan the assembly containing this type
這個問題是在 1.7+ hangfire 版本中。 我將版本降級到 1.6.27 並解決了
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.