随机发生的问题:"无法解决类型(...)的服务 "与Hangfire。

问题描述 投票:0回答:1

我在使用Hangfire时遇到了一个奇怪的问题,我在尝试激活 "AW.Services.Jobs.SendSmsJob "时,有时(完全随机的)会出现 "无法解析'AW.Services.Interfaces.ISmsService'类型的服务 "的错误。当我试图激活 "AW.Services.Jobs.SendSmsJob "时,我有时(完全随机地)收到关于无法解决'AW.Services.Interfaces.ISmsService'类型的服务的错误。当我在dashboard上点击这个失败的任务的requeue时,它要么再次失败,要么以成功结束。这真的是随机发生的,我不知道发生了什么。

当然,我已经在IoC中注册了我的接口,就像这样。services.AddTransient<ISmsService, SmsService>();.

我使用的软件包版本如下。

  • .NET Core 3.1
  • Hangfire v1.7.9
  • Hangfire.AspNetCore v1.7.9
  • Hangfire.Console v1.4.2
  • Hangfire.PostgreSql v1.6.4.1

我的hangfire在startup.cs中的配置如下。

services.AddHangfire(config =>
{
    config.UsePostgreSqlStorage(Configuration["AW_API_DB_CONNECTIONSTRING"]);
    config.UseConsole();
});
GlobalConfiguration.Configuration.UseSerializerSettings
(
    new JsonSerializerSettings
    {
        ReferenceLoopHandling = ReferenceLoopHandling.Ignore
    }
);

(...)

app.UseHangfireServer(new BackgroundJobServerOptions
{
       WorkerCount = backgroundProcessingWorkersCount
});

app.UseHangfireServer(new BackgroundJobServerOptions
{
    WorkerCount = backgroundProcessingWorkersCount,
    Queues = new[] { JobQueueTypes.Transactions }
});
app.UseHangfireDashboard("/dashboard", new DashboardOptions
{
    Authorization = new[] { new AgriWalletDashboardAuthFilter() },
    I

以下是我从hangfire的仪表板上复制的整个日志。

Unable to resolve service for type 'AW.Services.Interfaces.ISmsService' while attempting to activate 'AW.Services.Jobs.SendSmsJob'. System.InvalidOperationException.Dashboard:当试图激活'AW.Services.Jobs.SendSmsJob'时,无法解析'AW.Services.Interfaces.ISmsService'类型的服务。在 Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider) 在 Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider)。 DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[]parameters)在Hangfire.Server.CoreBackgroundJobPerformer.Perform(PerformContext context)在Hangfire.Server.BackgroundJobPerformer.<>c__DisplayClass9_0.b__0()在Hangfire. 在Hangfire.Server.BackgroundJobPerformer.InvokePerformFilter(IServerFilter filter, PerformingContext preContext, Func1 continuation)处,Hangfire.Server.BackgroundJobPerformer.InvokePerformFilter(IServerFilter filter, PerformingContext preContext, Func1 continuation)。 在Hangfire.Server.BackgroundJobPerformer.PerformJobWithFilters(PerformContext context, IEnumerable`1 filters)处,Hangfire.Server.BackgroundJobPerformer.Perform(PerformContext context) 在Hangfire.Server.Worker.PerformJob(BackgroundProcessContext context, IStorageConnection, String jobId)处。

.net-core dependency-injection inversion-of-control ioc-container hangfire
1个回答
1
投票

从netcore2迁移到netcore3可能会导致依赖注入的问题。请检查项目中的Program.cs和Startup.cs类。

https:/docs.microsoft.comen-usaspnetcoremigration22-to-30?view=aspnetcore-3.1&tabs=visual-studio。

© www.soinside.com 2019 - 2024. All rights reserved.