Open Closed

Adding Hangfire support to a solution #4032


User avatar
0
akaziuka created

We have a tiered app/solution generated with ABP Suite. We're looking to add Hangfire support to this solution. Following the documentation that describes the steps (https://docs.abp.io/en/abp/latest/Background-Jobs-Hangfire), we were able to install the package with ABP CLI, as suggested. My understanding is that we don't need to do anything extra. When we run the solution it fails at runtime with the following error:

Volo.Abp.AbpInitializationException
  HResult=0x80131500
  Message=An error occurred during the initialize Volo.Abp.Modularity.OnApplicationInitializationModuleLifecycleContributor phase of the module Volo.Abp.Hangfire.AbpHangfireModule, Volo.Abp.HangFire, Version=6.0.1.0, Culture=neutral, PublicKeyToken=null: An exception was thrown while activating λ:Volo.Abp.Hangfire.AbpHangfireBackgroundJobServer -> λ:Hangfire.JobStorage.. See the inner exception for details.
  Source=Volo.Abp.Core
  StackTrace:
   at Volo.Abp.Modularity.ModuleManager.<InitializeModulesAsync>d__4.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Volo.Abp.AbpApplicationBase.<InitializeModulesAsync>d__19.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Volo.Abp.AbpApplicationWithInternalServiceProvider.<InitializeAsync>d__7.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at _3M.DbMigrator.DbMigratorHostedService.<StartAsync>d__3.MoveNext() in D:\_Sources\MyAbilities\3Motion\_3M\aspnet-core\src\_3M.DbMigrator\DbMigratorHostedService.cs:line 32
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult()
   at Microsoft.Extensions.Hosting.Internal.Host.<StartAsync>d__12.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult()
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.<RunAsync>d__4.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.<RunAsync>d__4.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at _3M.DbMigrator.Program.<Main>d__0.MoveNext() in D:\_Sources\MyAbilities\3Motion\_3M\aspnet-core\src\_3M.DbMigrator\Program.cs:line 28

  This exception was originally thrown at this call stack:
    [External Code]

Inner Exception 1:
DependencyResolutionException: An exception was thrown while activating λ:Volo.Abp.Hangfire.AbpHangfireBackgroundJobServer -> λ:Hangfire.JobStorage.

Inner Exception 2:
InvalidOperationException: JobStorage.Current property value has not been initialized. You must set it before using Hangfire Client or Server API.

Steps to reproduce:

  • Generate a new solution, and make it run
  • Add Hangfire support via ABP CLI (as per documentation)

Questions:

  • What might be the issue here
  • How do we access Hangfire Dashboard (it's not clear from the docs)

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

    hi

    Can you try https://github.com/abpframework/abp/issues/11069 ?

  • User Avatar
    0
    akaziuka created

    hi

    Can you try https://github.com/abpframework/abp/issues/11069 ?

    Hello, I have re-implemented Hangfire manually, as the documentation suggested, and I can even see the dashboard running. However, the DbMigrator exits with the following error:

    Unhandled exception. 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=6.0.1.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: JobStorage.Current property value has not been initialized. You must set it before using Hangfire Client or Server API.
       at Hangfire.JobStorage.get_Current()
       at Hangfire.HangfireServiceCollectionExtensions.<>c.<AddHangfire>b__1_0(IServiceProvider _)
       at Hangfire.HangfireServiceCollectionExtensions.<>c__DisplayClass13_0`1.&lt;TryAddSingletonChecked&gt;b__0(IServiceProvider serviceProvider)
       at Autofac.Extensions.DependencyInjection.AutofacRegistration.&lt;&gt;c__DisplayClass3_0.&lt;Register&gt;b__0(IComponentContext context, IEnumerable`1 parameters)
       ...
    

    I'm not sure what JobStorage does it require. I'm using PostgresSQL as a database server, and I can see Hangfire has created a separate schema in the database to host its data. Any clues on what can it be?

    Also, please suggest what's the proper place in the solution to implement Hangfire logic/jobs?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    I have re-implemented Hangfire manually

    Can you share your code?

    Maybe you should move some code to DbMigrator module.

  • User Avatar
    0
    akaziuka created

    I have re-implemented Hangfire manually

    Can you share your code?

    Maybe you should move some code to DbMigrator module.

    I can share the code. How do I send it?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    liming.ma@volosoft.com

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    _3M.DbMigrator.csproj

        <PackageReference Include="Hangfire.PostgreSql" Version="1.9.9" />
    
    public class _3MDbMigratorModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            var configuration = context.Services.GetConfiguration();
            context.Services.AddHangfire(config =>
            {
                GlobalConfiguration.Configuration.UsePostgreSqlStorage(configuration.GetConnectionString("Default"));
                //config.UsePostgreSqlStorage(configuration.GetConnectionString("Default"));
            });
    
            Configure<AbpBackgroundJobOptions>(options =>
            {
                options.IsJobExecutionEnabled = false;
            });
        }
    }
    
    
Made with ❤️ on ABP v8.2.0-preview Updated on March 25, 2024, 15:11