Questions tagged [ihostedservice]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
2 votes
1 answer
39 views

How to add data seed method with IHostedService

This is my Program.cs var builder = WebApplication.CreateBuilder(args); builder.Services.AddControllers(); builder.Services.AddTransient<DatabaseSeeder>(); builder.Services.AddScoped<...
semisweetdude's user avatar
0 votes
0 answers
23 views

Does passing a reference into a BackgroundTaskQueue work item cause the calling class to be held in memory?

I have the standard Queued Background Tasks setup defined in this article. When using this service to queue items, via QueueBackgroundWorkItemAsync(Task workItem), what is the effect of passing in ...
Tristan Trainer's user avatar
0 votes
1 answer
41 views

Post-Build Addition of IHostedService

Is it possible to add an IHostedService to an IHost after calling HostApplicationBuilder.Build() and IHost.StartAsync()? The application needs to be able to load plug-ins during use and have services ...
PatrickV's user avatar
  • 2,097
0 votes
0 answers
39 views

Delay Windows Service "Running" State Transition in .NET 6 BackgroundService

I am working on creating a Windows service in .NET 6 using BackgroundService. However, I'm encountering an issue where the Windows service transitions to the "running" state as soon as ...
Jojo Peter's user avatar
0 votes
0 answers
66 views

When to use .NET Generic Host's Run, Start and WaitForShutdown

I use the .NET Generic Host (to run a BackgroundService). All the examples I've seen (example) use host.Run[Async], so I use that and it works for me. However there are a number of related lifetime ...
lonix's user avatar
  • 15.3k
0 votes
2 answers
115 views

Return non-zero exit code for crashed .NET Core BackgroundService

Due to bugs in BackgroundService I'm using Stephen Cleary's excellent workaround. It works, but I can't return a non-zero exit code upon failure. A minimal working example (for use with the $ dotnet ...
lonix's user avatar
  • 15.3k
0 votes
1 answer
20 views

Add Multiple Hosted Services with different concrete types

I have the following constructor definition for my class: public MyWorkerService(IConfiguration configuration, ILogger<MyWorkerService> logger, IWorkerService workerSerice) I then have the ...
Liam's user avatar
  • 1,175
1 vote
0 answers
17 views

Trouble with Running a Continuous Task in the Background

I want to add notifications at certain intervals using BackgroundService, but the waiting interval I set is 10 seconds in this example, the program running ends when it is finished. Service like this: ...
Bedirhan Gündöner's user avatar
0 votes
0 answers
53 views

Why do I see dependency injection so often in the context of hosted services?

I'm new to C# (and programming in general) and I've recently learned about how background tasks can be implemented using hosted services (e.g. schedule a task to fire at regular intervals). I've been ...
Elon Obama's user avatar
0 votes
1 answer
133 views

Is the difference between AddQuartz and AddQuartzHostedService that the latter adds a hosted service?

I'm just learning Quartz.NET (and C# in general in fact) and I'm struggling to get my head around the differences between the AddQuartz and AddQuartzHostedService methods. From what I've seen here, ...
Elon Obama's user avatar
0 votes
0 answers
125 views

long running operations using .NET core worker service - create, cancel , pause and resume operation

I've written worker service using .NET hosted service feature to move long running task from request thread. I designed API as per (https://github.com/microsoft/api-guidelines/blob/vNext/azure/...
Praveen Kumar N's user avatar
0 votes
0 answers
100 views

Is It OK to Instantiate Multiple Instances of the Generic Host (IHost)

We have a framework upon which my team builds microservice apps and related jobs. We want to migrate it from using IWebHost for web apps and IHost for jobs to simply using IHost for both. We haven't ...
bubbleking's user avatar
  • 3,379
0 votes
3 answers
2k views

How to map environment variables to a config object in a IHostedService?

I'm creating a new console app for the first time in a while and I'm learning how to use IHostedService. If I want to have values from appsettings.json available to my application, the correct way now ...
Bob Tway's user avatar
  • 9,351
0 votes
0 answers
35 views

Using NSB in IHostedService causes error Value cannot be null. (Parameter ‘session’)

I added NServiceBus v7.3 to ASP.NET Web API .NET Core 3.1. Using IHostedService I want to deal with cron jobs. When the IHostedService starts working, I add timer to call a method DoWork each 5 ...
Dabbas's user avatar
  • 3,152
0 votes
1 answer
342 views

How To Get The Results Of A Worker Background Service In C# .NET?

I have used the Worker Service template generated by Visual Studio 2022 as an example, but basically I was wondering what would be a method for finding out the result of a worker run once it is ...
danrockcoll's user avatar
2 votes
2 answers
155 views

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 ...
Devin Harris's user avatar
0 votes
0 answers
70 views

How to expose System.CommandLine arguments to HostedServices

There is a nice example, how to use System.Commanline with Host: https://github.com/dotnet/command-line-api/tree/main/samples/HostingPlayground I want to start more HostedServices to make some ...
Aaaaaaaa's user avatar
  • 2,045
0 votes
1 answer
90 views

Ho to get an asp.net hosted service to run automatically on IIS 10?

I'm having trouble with getting my hosted service to run automatically (without a user initiating a request) on IIS 10. This is an asp.net app, .NET 7 with a React front-end. It's running Signal R and ...
lsonnen's user avatar
1 vote
1 answer
185 views

Clear session data perodically from db

Let's say I have an application and it has 10000k monthly users and I need to clean session data from database every 2 days, should I use the built-in background service in .net core or is it better ...
Moka Buludlu's user avatar
0 votes
1 answer
272 views

How to prevent .net 6 generic hosted console apps logging queue from being shutdown on application stop?

I'm currently working on a small demo project for a dotnet console application using the generic hosting for logging and configuration. In the context of writing this demo / proof of concept app I ran ...
oli's user avatar
  • 24
0 votes
1 answer
385 views

Using Dependency Injection from Azure Key Vault to grab a connection string for AddDbContextFactory

I'm trying to set up the service registrations for my ihostedservice app and would like to pull in a connection string that is located in the key vault. What I have is something like this: using IHost ...
Kevin Le's user avatar
0 votes
1 answer
344 views

Long-running task without IHostedService running the entire life of the application?

I have a website page that needs the option of performing an operation that could take several minutes. To avoid performance issues and time outs, I want to run this operation outside of the HTTP ...
Jonathan Wood's user avatar
0 votes
0 answers
452 views

Blazor Server run only one background task

I have a Blazor Server application (net 7) where a long running background task can be triggered by any user, but once the task has been started everyone must wait until that task is completed. The ...
Sam's user avatar
  • 819
0 votes
1 answer
65 views

How can I run an IHostedService separate from the WebApplication (WebApi)?

Suppose I have a number of IHostedService implementations with a ton of dependencies that no WebApi controllers do not have direct nor indirect dependencies, too. Ideally, I like it if could set up an ...
softbear's user avatar
  • 495
1 vote
0 answers
78 views

Hostedservice with events

I have a question, i'm making a disk monitoring tool, just the simple system.io.filesystemwatcher currently and i'm stuck on the 'always running part'. So I tought, I create a watchservice that ...
Technology Researcher's user avatar
0 votes
0 answers
178 views

How to run background task? keep getting System.AggregateException

This is my program.cs using TestEmailTaskScheduler.Controllers; using TestEmailTaskScheduler.Models; using System.Configuration; var builder = WebApplication.CreateBuilder(args); // Add services to ...
CodingNeeded's user avatar
0 votes
1 answer
481 views

IHostedService app not starting automatically on azure app service

I have an IHostedService app in dotnet core 3.1, which is deployed on azure app service plan, but it needs to browse the app service to activate IHostedService. I have automatic deploy pipeline , when ...
Furqan Misarwala's user avatar
2 votes
1 answer
1k views

.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. ...
Coal Chris's user avatar
0 votes
1 answer
369 views

Error logging in New Relic for .Net Core Worker (IHostedService) Hosted on Azure App Service

I’m trying to integrate Logging for .Net Worker Application (IHostedService) which is hosted on Azure App Service ( Linux App Service Plan) I followed the instructions defined in this link https://...
Unknown Coder's user avatar
2 votes
1 answer
1k views

BackgroundService never started/stopped if no await done

On ASP.NET Core I'm observing a strange behavior, that was actually reported in BackgroundService not shutting down, stoppingToken never set with .net core generic host but without the root cause ever ...
lezebulon's user avatar
  • 7,645
1 vote
0 answers
143 views

Blazor Modular UI application hosted in service container, routing problem

I'm trying to create a Blazor application where ultimately the host and layout are declared in one Razor Class Library or application and applied across other RCL modules. I've tried a few variants ...
Mik3c's user avatar
  • 11
0 votes
0 answers
76 views

Ensure finalizing iHostedServices before recycling on a IIS with dotnet core

I have a dotnet 6 application with an hosted service that runs some scheduled tasks all x-minutes. So far so good, everything working almost perfectly. What I get from time to time is, that the app ...
rst's user avatar
  • 2,580
0 votes
1 answer
573 views

C# HostedService through IHost - get Instance from running ServicePool

The question is quite simple, yet challenging. Normal service creation: IHost host = Host.CreateDefaultBuilder(args) .ConfigureServices(services => ...
SpReeD's user avatar
  • 7
1 vote
0 answers
385 views

Quart.Net :Enable/Disable the job at RunTime

i have done similar Thing in my Project https://andrewlock.net/using-quartz-net-with-asp-net-core-and-worker-services/. i am scheduling all the job at the StartAsync by fetching the job data from Db . ...
GOWTHAM's user avatar
  • 13
0 votes
0 answers
683 views

What is best approach to polling a MongoDB database looking for changes

I have two microservices (ServiceA and ServiceB). ServiceA inserts records into a MongoDB database. ServiceB check in MongoDB if there are new records and does some work with theses new records. ...
Julien Martin's user avatar
2 votes
1 answer
3k views

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 ...
Unknown Coder's user avatar
6 votes
2 answers
6k views

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 ...
tdean's user avatar
  • 538
0 votes
0 answers
865 views

How to run background hosted service after the other finished work?

I'm creating app that should be long-running background service. It should take data periodically from one place and send API request. But here are 2 services. 2nd should send API requests after the ...
demo's user avatar
  • 6,097
0 votes
1 answer
384 views

using Database in IHostedService

Hi guys I try to use my Database in an IHostedService. Asp .net, SQLite I get the context with the IServiceScopeFactory. After that I Start a Timer and then I want to access the database every time ...
jonnyVoAustria's user avatar
1 vote
1 answer
1k views

Graceful shutdown of IHostedService / BackgroundService

What is the best way to block shutdown of a service while jobs finish? I have a few background services that listen for messages on a message bus and handle processing.Some implement IHostService ...
bep's user avatar
  • 962
1 vote
3 answers
603 views

Is it possible to add all IHostedService classes with a loop (ASP.NET Core 6)?

Is it possible to add all IHostedService implemented classes in a loop without adding them individually in ASP.NET Core 6? Let's say we have this two implementations: public class ...
BaRRuKa's user avatar
  • 39
0 votes
1 answer
1k views

Multiple Hosted Service With Simple Injector

i'm trying to follow this example here the only thing is i need to inject multiple hosted services instead of one like this services.AddSimpleInjector(container, options => { // Registers the ...
Redouane's user avatar
0 votes
0 answers
215 views

How to check the Queue is empty or how many items are in it of Queued background service in ASP.NET Core?

I want to implement the QueueBackground service in asp.net core. I need to check the queue is empty or not? How to count the items which are in Queue?
joseph rozario's user avatar
0 votes
1 answer
420 views

.Net Core BackgroundService for RabbitMQ.Client.Core.DependencyInjection - not possible to use other services or MediatR command

I'm creating an application using RabbitMQ.Client.Core.DependencyInjection as a consumer of messages in the BackgroundService. Receiving and deserializing the message works correctly, but when I'm ...
Tommix's user avatar
  • 81
1 vote
0 answers
590 views

How to pass data between two Worker Services

Let's say I have two long running custom services and two WorkerServices registered with host: .ConfigureServices((hostContext, services) => { services.AddSingleton<IServiceA, ServiceA>();...
Chris's user avatar
  • 438
0 votes
1 answer
206 views

IHostedService for database changes hung forever

I am trying to implement database change notificaitons using SignalR in a web api. below is the code for NotificationService.cs using System; using System.Threading; using System.Threading.Tasks; ...
VIRIYALA NARESH's user avatar
5 votes
0 answers
1k views

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, ...
Robin Zimmerman's user avatar
0 votes
0 answers
178 views

Convert IHostedService to Microservice

I'm working with .NET Core 5 in a monolithic application, it has many IHostedServices that affect performance and I wanted to convert them all to microservices. I can't find any examples of ...
Paul's user avatar
  • 11
1 vote
1 answer
2k views

.NET Core iHostedService shutting down IIS

I am running a .NET Core web application on IIS which runs a web request every few minutes, this is working absolutely fine, the app checks the DB and if the time has already elapsed it does the web ...
APearTree's user avatar