如何使用针对 .net 框架的结构图在 asp.net 核心中配置 hangfire 并解决双向依赖关系

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 包,但无法在启动时配置它 class 因为 none 的扩展方法可用。

另请注意,此确切设置适用于以 .net 核心为目标的项目。

如有任何建议,我们将不胜感激!

原来是结构图配置中的这一行有问题:

scan.AssembliesAndExecutablesFromApplicationBaseDirectory();

我应该改用这样的东西:

scan.AssemblyContainingType<IThumbnailGenerator>(); // directing structuremap to scan the assembly containing this type

此问题出现在 1.7+ hangfire 版本中。 我把版本降到1.6.27就解决了