# Support for IAsyncDisposable dependencies

**URL:** <https://discuss.hangfire.io/t/support-for-iasyncdisposable-dependencies/9095>\
**Category:** question\
**Created:** [April 15, 2022, 5:29pm UTC](https://discuss.hangfire.io/t/support-for-iasyncdisposable-dependencies/9095 "2022-04-15T17:29:35Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![gfournier](https://avatars.discourse-cdn.com/v4/letter/g/35a633/32.png) [@gfournier](https://discuss.hangfire.io/u/gfournier)\
**Post date:** [April 15, 2022, 5:29pm UTC](https://discuss.hangfire.io/t/support-for-iasyncdisposable-dependencies/9095/1 "2022-04-15T17:29:35Z")

</div>

Hello,

I have the following code that works well and I’m getting no exception :

```auto
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:

```auto
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!
