Open Closed

Unable to integrate Hangfire #7378


User avatar
0
themisoft created
  • ABP Framework version: v8.1.3
  • UI Type: Blazor Server
  • Database System: PostgreSQL
  • Tiered (Auth/Blazor/ApiHost): Yes
  • Exception message and full stack trace:
 `Started background worker: Volo.Abp.OpenIddict.Tokens.TokenCleanupBackgroundWorker
2024-06-20 17:06:41.303 +02:00 [FTL] HangfireTest.AuthServer terminated unexpectedly!
Volo.Abp.AbpInitializationException: An error occurred during the initialize Volo.Abp.Modularity.OnApplicationInitializationModuleLifecycleContributor phase of the module Volo.Abp.Hangfire.AbpHangfireModule, Volo.Abp.HangFire, Version=8.1.3.0, Culture=neutral, PublicKeyToken=null: An exception was thrown while activating λ:Volo.Abp.Hangfire.AbpHangfireBackgroundJobServer -> λ:Hangfire.JobStorage.. See the inner exception for details.
 ---> Autofac.Core.DependencyResolutionException: An exception was thrown while activating λ:Volo.Abp.Hangfire.AbpHangfireBackgroundJobServer -> λ:Hangfire.JobStorage.
 ---> System.InvalidOperationException: Current JobStorage instance has not been initialized yet. You must set it before using Hangfire Client or Server API. For .NET Core applications please call the IServiceCollection.AddHangfire extension method from Hangfire.NetCore or Hangfire.AspNetCore package depending on your application type when configuring the services and ensure service-based APIs are used instead of static ones, like `IBackgroundJobClient` instead of `BackgroundJob` and IRecurringJobManager instead of RecurringJob.
  • Steps to reproduce the issue:

I'm fairly new to background jobs, so I might be missing something. I managed to get the default ABP background jobs running, so I decided to install Hangfire for future scalability as our system grows more complex. However, after many attempts, I couldn't get it to work in our project. To troubleshoot, I decided to try it in a fresh project. Despite following the ABP documentation and searching online for similar issues, I couldn't find a solution to start my project with Hangfire.

These are the packages I installed in the HttpApi.Host project:

  • Hangfire.PostgreSql
  • Hangfire.Core

This is all the code I added to the HttpApi.Host project:

[DependsOn(
    typeof(AbpBackgroundJobsHangfireModule),
    typeof(AbpHangfireModule)
    )]
    
public override void ConfigureServices(ServiceConfigurationContext context)
{
    var configuration = context.Services.GetConfiguration();
    var hostingEnvironment = context.Services.GetHostingEnvironment();
    ...
    ConfigureHangfire(context, configuration);
    ...
}

    private void ConfigureHangfire(ServiceConfigurationContext context, IConfiguration configuration)
    {
        context.Services.AddHangfire(config =>
        {
            config.UsePostgreSqlStorage(options =>
            {
                options.UseNpgsqlConnection(configuration.GetConnectionString("Default"));
            });
        });

        context.Services.AddHangfireServer();
    }
    
    public override void OnApplicationInitialization(ApplicationInitializationContext context)
    {
        ...
        app.UseAbpHangfireDashboard();
        app.UseConfiguredEndpoints();
    }

I tried many workarounds, such as adding more Hangfire packages or attempting to initialize JobStorage before the Auth server tries to use it. However, I still don't fully understand the issue.


5 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Current JobStorage instance has not been initialized yet. You must set it before using Hangfire Client or Server API. For .NET Core applications please call the IServiceCollection.AddHangfire extension method from Hangfire.NetCore or Hangfire.AspNetCore package depending on your application type when configuring the services and ensure service-based APIs are used instead of static ones, like IBackgroundJobClient instead of BackgroundJob and IRecurringJobManager instead of RecurringJob.

    1. Can you try to remove the context.Services.AddHangfireServer();
    2. If 1 is not working, Can you share your fresh project by https://wetransfer.com/

    liming.ma@volosoft.com

    Thanks

  • User Avatar
    0
    themisoft created

    Hello,

    Removing context.Services.AddHangfireServer(); did not work sadly. I will send you the entire project.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Remove this line from your domain module

  • User Avatar
    0
    themisoft created

    The program runs now!

    Thank you so much. Does this mean that the ABP CLI installs the package in the wrong project?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    You have multiple host projects and only want to use hangfire in API.HOST project. so you shouldn't use the hangfire module in the shared module(domain)

Made with ❤️ on ABP v9.1.0-preview. Updated on November 01, 2024, 05:35