【问题标题】:Randomly occurring issue: 'Unable to resolve service for type (...)' with Hangfire随机出现的问题:使用 Hangfire 的“无法解析类型 (...) 的服务”
【发布时间】:2020-08-08 01:15:54
【问题描述】:

我在使用 Hangfire 时遇到了一个奇怪的问题。在尝试激活“AW.Services.Jobs.SendSmsJob”时,我有时会(完全随机)收到有关无法解析类型“AW.Services.Interfaces.ISmsService”的服务的错误。当我在此失败作业的仪表板上单击重新排队时,它会再次失败或成功完成。它真的是随机发生的,我不知道发生了什么。

我在 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的仪表板复制了整个日志:

在尝试激活“AW.Services.Jobs.SendSmsJob”时,无法解析“AW.Services.Interfaces.ISmsService”类型的服务。 System.InvalidOperationException:尝试激活“AW.Services.Jobs.SendSmsJob”时无法解析“AW.Services.Interfaces.ISmsService”类型的服务。在 Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider 提供者)在 Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider 提供者,类型 instanceType,Object[] 参数)在 Hangfire.Server.CoreBackgroundJobPerformer.Perform(PerformContext 语境) 在 Hangfire.Server.BackgroundJobPerformer.c__DisplayClass9_0.b__0() 在 Hangfire.Server.BackgroundJobPerformer.InvokePerformFilter(IServerFilter 过滤器,PerformingContext preContext,Func1 延续在 Hangfire.Server.BackgroundJobPerformer.InvokePerformFilter(IServerFilter 过滤器,PerformingContext preContext,Func1 延续)在 Hangfire.Server.BackgroundJobPerformer.PerformJobWithFilters(PerformContext 上下文,IEnumerable`1 过滤器)在 Hangfire.Server.BackgroundJobPerformer.Perform(PerformContext 上下文) 在 Hangfire.Server.Worker.PerformJob(BackgroundProcessContext 上下文, IStorageConnection 连接,字符串 jobId)

【问题讨论】:

    标签: .net-core dependency-injection inversion-of-control ioc-container hangfire


    【解决方案1】:

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

    https://docs.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.1&tabs=visual-studio

    【讨论】:

    • 确实,它导致了问题:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-16
    • 2022-01-10
    • 2020-03-13
    • 2019-02-10
    • 2019-04-27
    • 1970-01-01
    相关资源
    最近更新 更多