Questions tagged [ihostedservice]
75 questions
5
votes
0 answers
When should I use IHostApplicationLifetime.OnStarted event vs IHostedService.StartAsync?
I have been investigating the us of the .NET generic host for hosting console applications. It seems like the recommended pattern for .NET going forwards allowing for easy use of DI, logging, configuration, etc. while maintaining a consistent API as…

Robin Zimmerman
- 593
- 1
- 6
- 17
5
votes
1 answer
Is there a way to manually start BackgroundService in Asp.Net core 3.1
I know prior to Asp.Net 3.0 (or 3.1), to manually start a BackgroundService, we could derive it from IHostedService instead and change the DI registration to:
services.AddSingleton();
and then manually trigger…

Jinish
- 1,983
- 1
- 17
- 22
4
votes
2 answers
How can I access the command line arguments in a console application using IHostedService?
I can not figure out how to access the command line args in my ConsoleHostedService implementation class. I see in the sources CreateDefaultBuilder(args) somehow adds it to the configuration... named Args...
Having the main program:
internal sealed…

g.pickardou
- 32,346
- 36
- 123
- 268
4
votes
1 answer
Failed to get hosted service instance (Manually start hosted service .net core 3.1)
I had api project and test project in .net core 2.2. Now I have migrated both project in .net core 3.1
In test project I have "Initialize" method where I am adding services manually
public void Initalize(){
var services = new…

Shyam Padodara
- 63
- 5
3
votes
2 answers
IHostedService .NET 6 not starting when deployed
I have an API which contains a HostedService that is built in VS2022 with .Net 6.
When I run locally the service is called as expected and everything works fine but when deplyed, the service doesn't seem to start.
I have tried many different…

tdean
- 508
- 1
- 7
- 15
3
votes
0 answers
How to inject cancellation token in a HostedService timer with Simple Injector
I'm trying to create a console application that run ahosted service with Simple Injector and I looked to the example at generichostintegration.
Now I would like to change IProcessor.DoSomeWork to be an async function with a cancellation token as…

Paolo
- 31
- 2
3
votes
1 answer
How to stop and resume worker service?
Within a WPF application I have configured a hosted service to perform specific activity in background (Followed this article).
This is how hosted service is configured in App.xaml.cs.
public App()
{
var environmentName =…

shreesha
- 1,811
- 2
- 21
- 30
2
votes
2 answers
Do I need to explicitly create a cancellation token source with IHostedService?
Is the internal implementation of the library supplying me with a cancellation token source, or do I need to create my own?
I'm reading the documentation and it seems like I would need to create my own source in the caller method, then pass the…

Devin Harris
- 23
- 3
2
votes
1 answer
.NET OpenTelemetry Not Adding Listener For ActivitySource
Why is OpenTelemetry .NET not adding a listener for my ActivitySource in a hosted service on generic host?
I have some configurations to pass into OpenTelemetry set-up, so I depend on a class MyClass. This means registering that class, and then…

Coal Chris
- 21
- 2
2
votes
1 answer
Cannot consume scoped service 'ApplicationDbContext' from singleton 'Microsoft.Extensions.Hosting.IHostedService'
I'm using .Net Core Background Service to Connect to Kafka and save messages to SQL Server. My Project Structure looks like this:
In the Infrastructure Dependency, I have the following code to register Entity Framework using IConfiguration…

Unknown Coder
- 1,510
- 2
- 28
- 56
2
votes
0 answers
StopAsync of IHostedService is not called on CTRL+C
I am building an ASP.NET Web API (.NET 5).
Other than having typical controller, I also want to have a background job running parallel all the time. For this purpose, I have created a hosted service:
public class MyHostedService : IHostedService
{
…

mnj
- 2,539
- 3
- 29
- 58
2
votes
2 answers
IHostedService Error in ASP.NET Core app - why is the object already disposed?
I have created a Background Service in my Server Solution
public class PurgeService : IHostedService, IDisposable
{
private readonly IServiceProvider _provider;
private Timer timer;
public PurgeService(IServiceProvider serviceProvider)
…

T0bi
- 261
- 1
- 4
- 13
2
votes
1 answer
.NET 5 Tray Icon Usage in C# Windows Service
I searched for current best practices to handle windows system / tray icons in the .NET environment, but did not find any up-to-date information.
Considering a usual .NET 5 project configuration:
…

ˈvɔlə
- 9,204
- 10
- 63
- 89
2
votes
0 answers
.NET Core Access HttpContext from HostedService
I am working on .Net CORE 3.1 web API. We use BackgroundHostedService to process time consuming operations. We need an access to HttpContext from that hosted service like this:
public PermissionHelper(IHttpContextAccessor accessor)
{
…

kihtov23
- 131
- 1
- 7
2
votes
1 answer
.Net core 3.1 - IHosted service (AddHostedService) is not resolving Dependency registrations
We have an API project in.net core and one library project in the .net core where we define the background process (Background services using IHosted Service).
In the API project, we register the dependencies of our background services…

Rrao
- 21
- 1
- 4