简体   繁体   中英

How to configure hangfire in asp.net core using structuremap targeting .net framework and resolve bi-directional dependency

I've just been banging my head against the wall with this one not sure what's going on.

I've got an asp.net core web project targeting .NET 4.7.2 that I've been trying to configure 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>();
        }

and my configure method:

        public void Configure(IApplicationBuilder app, IHostingEnvironment env, IContainer container)
        {
            //using Hangfire.StructureMap
GlobalConfiguration.Configuration.UseStructureMapActivator(container);

// exception is thrown on UsingHangfireServer()
            app
                .UseHangfireServer()
                .UseMvc();
        }

I keep getting an exception from StructureMap about a bi-directional dependency:

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)

I've using packages Hangfire.AspNetCore, Hangfire.SqlServer, Hangfire.StructureMap, all v 1.7.0

I also tried just using the Hangfire package but am unable to configure it in my startup class because none of the extension methods were available.

Also note that this exact set up works just fine on a project targeting .net core.

Any advice would be greatly appreciated!

Turns out it was an issue with this line in the structuremap configuration:

scan.AssembliesAndExecutablesFromApplicationBaseDirectory();

I should have been using something like this instead:

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

this problem is in 1.7+ hangfire version. I downgraded the version to 1.6.27 and it solved

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM