c# - 如何使用面向.net框架的结构图在asp.net核心中配置hangfire并解决双向依赖
问题描述
我刚刚把我的头撞在墙上,这个不知道发生了什么。
我有一个针对 .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
推荐阅读
- maven - 由于打包 Maven 项目的规范文件中的“BuildRequires:maven-local”而出错
- ios - 当其中的标签文本被修改时,有没有办法改变 tableView 单元格的高度?
- java - 从命名空间读取 XML 时编组链接异常
- javascript - 如果密码正确vuetify如何转到另一个页面
- docker - 使用带有自定义配置和样式文件的 Kubernetes for TileServer-GL
- pandas - 熊猫中的Python指数曲线拟合:每行定义函数参数
- c - getchar() 不等待输入并直接跳转到下一行
- visual-studio - 如何从 Visual Studio 启动 SurfaceDuo 模拟器
- typescript - 我们如何设置一个@Get() 方法来接收和传递值?
- java - 无论我使用什么元素查询类型,Selenium 程序都会返回 NoSuchElementException?