HangfireIO / HangfireIO/Hangfire

Randomly occurring issue: 'Unable to resolve service for type (...)'

Open
#1,653 8 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
10.1k
Forks
1.8k
Avg merge
1h 19m
Merged PRs (30d)
1

Description

I encountered a strange issue. I'm getting **sometimes** (totally randomly) error about `Unable to resolve service for type 'AW.Services.Interfaces.ISmsService' while attempting to activate 'AW.Services.Jobs.SendSmsJob'.` When I click requeue on dashboard on this failed job it either fails again or finish with success. It happens really randomly and I don't have any idea what is happening.

I have registered my interface in IoC of course like this: `services.AddTransient();`.

I'm using the following packages versions:

- .NET Core 3.1
- Hangfire 1.7.9
- Hangfire.AspNetCore 1.7.9
- Hangfire.Console 1.4.2
- Hangfire.PostgreSql 1.6.4.1

My hangfire's configuration in startup.cs is the following:
```
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() },
IgnoreAntiforgeryToken = true
});
```

Below I've copied the entire log from hangfire's dashboard:

> Unable to resolve service for type 'AW.Services.Interfaces.ISmsService' while attempting to activate 'AW.Services.Jobs.SendSmsJob'.
>
> System.InvalidOperationException: Unable to resolve service for type 'AW.Services.Interfaces.ISmsService' while attempting to activate 'AW.Services.Jobs.SendSmsJob'.
> 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.b__0()
> at Hangfire.Server.BackgroundJobPerformer.InvokePerformFilter(IServerFilter filter, PerformingContext preContext, Func`1 continuation)
> 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)

Contributor guide

Open the contributing guide

Research direction

Start with startup.cs, the ISmsService registration, SendSmsJob, and the two UseHangfireServer configurations. Review the reported InvalidOperationException and try to reproduce the intermittent failure when requeueing jobs; done means identifying the cause and providing a verified fix or clear explanation.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.