Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Dependency injection in HostedService failing

$
0
0

Hi,

I am busy on a asp net core application where I need to perform a background task on my database once a day. So reading https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-3.1&tabs=visual-studio I made a background service that's working fine.  But in order to do something with it I need to access my database with the database context.  I wanted to use Dependency injection like I do with all my other classes. But then I get a runtime error : HTTP Error 500.30 ANCM In-Process start failure.

The relevant code:

 public class BackgroundService : IHostedService, IDisposable
    {
        private readonly Data.ApplicationDbContext _DB;
        private int executionCount = 0;
        private readonly ILogger<BackgroundService> _logger;

        private Timer _timer;

        //public BackgroundService(Data.ApplicationDbContext DB, ILogger<BackgroundService> logger) //this fails!!
        public BackgroundService( ILogger<BackgroundService> logger)  //This works
        {
            _logger = logger;
           // _DB = DB;
        }
....

Can somebody tell me what I do wrong? 

thanks i advance.

Rob


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>