首页 > 解决方案 > Hangfire:初始化对象的问题

问题描述

因此,我在 kubernetes pod 上运行了一个 API img,并将其配置为使用 Hangfire 将一些日志发送到位于同一环境中的 mssql DB。

现在每次尝试此操作时,我都会收到以下错误。

Hangfire.AutomaticRetryAttribute[0]
      Failed to process the job '14537': an exception occurred. Retry attempt 4 of 10 will be performed in 00:03:32.
      System.InvalidOperationException: Unable to resolve service for type 'Aftermarket.Server.Common.PqatExport.PQATRepository' while attempting to activate 'Aftermarket.Server.Common.PqatExport.PQATExportHandler'.
         at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider)
         at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)
         at Hangfire.Server.CoreBackgroundJobPerformer.Perform(PerformContext context)
         at Hangfire.Server.BackgroundJobPerformer.<>c__DisplayClass9_0.<PerformJobWithFilters>b__0()
         at Hangfire.Server.BackgroundJobPerformer.InvokePerformFilter(IServerFilter filter, PerformingContext preContext, Func`1 continuation)
         at Hangfire.Server.BackgroundJobPerformer.PerformJobWithFilters(PerformContext context, IEnumerable`1 filters)
         at Hangfire.Server.BackgroundJobPerformer.Perform(PerformContext context)
         at Hangfire.Server.Worker.PerformJob(BackgroundProcessContext context, IStorageConnection connection, String jobId)

现在这里的另一个问题是,它试图在 PQATRepository 和 PQATExportHandler 的帮助下运行传输,这些甚至不是该 API 应该用于传输的类属于其他 API(位于同一环境或同一 pod 中,所以说)。

这个 api 应该使用 HwsPQATRepository 和 HwsPQATExportHandler。然而,所有 PQATExport 类都位于两个 API 都可以访问的共享项目中:

在此处输入图像描述

这是我的 startup.cs 的相关部分

                services.AddHangfire(configuration => configuration
               .SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
               .UseSimpleAssemblyNameTypeSerializer()
               .UseRecommendedSerializerSettings()
               .UseSqlServerStorage(hangFireConnectionString,
                   new SqlServerStorageOptions
                   {
                       CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
                       SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
                       QueuePollInterval = TimeSpan.Zero,
                       UseRecommendedIsolationLevel = true,
                       UsePageLocksOnDequeue = true,
                       DisableGlobalLocks = true,
                   }));

                // Add the processing server as IHostedService
                services.AddHangfireServer();

                // Add own stuff
                //services.AddSingleton<ReportGenerator>();
                services.AddScoped<HwsPQATRepository>();
                services.AddScoped<HwsPQATExportHandler>();

如果有人知道为什么 Hangfire 不断崩溃,那将有很大帮助。亲切的问候

标签: kuberneteshangfireinvalidoperationexception

解决方案


推荐阅读