question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Do not warn about IDisposables in IHostedService

See original GitHub issue

While during my unit tests, I was suprised that the IDisposable that I declare in my [SetUp] method and disposed in my [TearDown] were correctly understood by IDisposableAnalyzers.

I’ve seen that there might be an exception somewhere, because I saw those unit tests : https://github.com/DotNetAnalyzers/IDisposableAnalyzers/blob/759fc9bb091625c711675513729de83789e544d5/IDisposableAnalyzers.Test/IDISP002DisposeMemberTests/ValidCode.TestFixture.cs

Would it be possible to do the same for IHostedService ? I think it’s a common pattern for background tasks in ASP.net core.

https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.hosting.ihostedservice?view=dotnet-plat-ext-3.0 There are two methods : StartAsync and StopAsync which are called by the IHost implementation. If I declare my Disposable in the StartAsync, and dispose them properly in the StopAsync, I think that it’s a good usage of the IDisposable pattern.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
jeremyVignellescommented, Mar 19, 2020

Here is an example: (written here, so it might not build)

using System.Threading;
using Microsoft.Extensions.Hosting;

public class Service: IHostedService
{
   private IDisposable disposable; // Should not trigger IDISP002 nor IDISP006 : Usage is correct

   public Task StartAsync(CancellationToken token)
   {
      this.disposable = new Disposable(); // Should not trigger IDISP003 : not assigned from the constructor, and this method is made to be called only once. No need to dispose previous
   }

   public Task StopAsync(CancellationToken token)
   {
      this.disposable.Dispose();
   }
}
0reactions
jeremyVignellescommented, Apr 27, 2020

Still triggers IDISP006 with this code:

    public class Test : IHostedService
    {
        private readonly SemaphoreSlim semaphore = new SemaphoreSlim(1);

        public Task StartAsync(CancellationToken cancellationToken)
        {
            return Task.CompletedTask;
        }

        public Task StopAsync(CancellationToken cancellationToken)
        {
            this.semaphore.Dispose();
            return Task.CompletedTask;
        }
    }
Read more comments on GitHub >

github_iconTop Results From Across the Web

IHostedService - StopAsync vs Dispose
Dispose is called when your object is ready to be collected by the garbage collector (broadly ... Do not expect StopAsync to be...
Read more >
Unknown socket error · Issue #64305 · dotnet/runtime
We have an application based on template dotnet create webapi project template and just adds an IHostedService to do background processing.
Read more >
ASP.NET Core background processing with IHostedService
Run background processes with the IHostedService and how to inject your services with dependency injection.
Read more >
background-tasks-with-ihostedservice.md
Background tasks and scheduled jobs are something you might need to use in any application, whether or not it follows the microservices architecture...
Read more >
Lifecycle of Generic Host Background Services
I decided to investigate the interactions between the Generic Host, IHostedService instances, and BackgroundService in particular, and decided I ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found