Hello,
I have the following code that works well and I’m getting no exception :
public partial class Startup
{
public void ConfigureServices(IServiceCollection services)
{
...
services.AddSingleton<WorkStarter>();
services.AddSingleton<WhateverDependency>();
services.AddHangfireServer(options =>
{
options.WorkerCount = _hangfireOptions.WorkerCount;
options.Queues = new[] { "critical", "default" };
});
...
services.AddHangfire(configuration => configuration.UseRedisStorage(_hangfireOptions.RedisConnectionString, redisStorageOptions));
}
}
public class WorkStarter
{
private readonly IServiceProvider _serviceProvider;
public void WorkStarter(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
public Task StartWork(Guid whateverId)
{
BackgroundJob.Enqueue(() => StartWithHangfireAsync(whateverId));
}
[AutomaticRetry(Attempts = 0)]
[LatencyTimeout(timeoutInSeconds: 30)]
[Queue("critical")]
public Task StartWorkWithHangfireAsync(Guid whateverId)
{
var whateverDependency = _serviceProvider.GetRequiredService<WhateverDependency>();
whateverDependency.DoWhatever();
...
}
}
However, when I change WhateverDependency to implements IAsyncDisposable, I have the following exception that is thrown after the job executed:
System.InvalidOperationException: 'WhateverDependency' type only implements IAsyncDisposable. Use DisposeAsync to dispose the container.
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.Dispose()
at Hangfire.AspNetCore.AspNetCoreJobActivatorScope.DisposeScope()
at Hangfire.JobActivatorScope.Dispose()
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.<>c__DisplayClass9_1.<PerformJobWithFilters>b__2()
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)
Does Hangfire supports dependencies that implements IAsyncDisposable?
Thanks!