asp.net-core-hosted-services

Task.Delay() triggers cancellation of application lifetime

被刻印的时光 ゝ 提交于 2019-12-24 19:13:21
问题 I have faced a situation when Task.Delay() method would trigger an event of cancellation in IApplicationLifetime . Here is the code: static async Task Main(string[] args) { Console.WriteLine("Starting"); await BuildWebHost(args) .RunAsync(); Console.WriteLine("Press any key to exit.."); Console.ReadKey(); } private static IHost BuildWebHost(string[] args) { var hostBuilder = new HostBuilder() .ConfigureHostConfiguration(config => { config.AddEnvironmentVariables(); config.AddCommandLine(args)

How do I get the website URL or access the HttpContext from a IHostedService in ASP.NET Core?

大兔子大兔子 提交于 2019-12-13 03:18:43
问题 In our MultiTenant ASP.NET Core 2.2 app, we determine the tenant from the URI. How can get the website URL from an IHostedService? The HttpContext is always null. The IHttpContextAccessor.HttpContext IS ALWAYS NULL public MyHostedService(ILogger<TurnTimeTask> logger, IHttpContextAccessor httpContextAccessor) { _logger = logger; _httpContextAccessor = httpContextAccessor; } Even running the IHostedService in Scope also returns NULL for the httpContextAccessor.HttpContext i.e. Injecting it

Background task of writing to the database by timer

流过昼夜 提交于 2019-12-10 17:29:08
问题 How to write to the database on a timer in the background. For example, check mail and add new letters to the database. In the example, I simplified the code just before writing to the database. The class names from the example in Microsoft. The recording class itself: namespace EmailNews.Services { internal interface IScopedProcessingService { void DoWork(); } internal class ScopedProcessingService : IScopedProcessingService { private readonly ApplicationDbContext _context; public

.Net core Hosted Services guaranteed to complete

孤人 提交于 2019-12-08 08:47:32
问题 I'm looking at .Net-Core 2.1 new feature Hosted Services, I see that they are very similarly modelled to QueueBackgroundWorkItem The Queue Background work item seems to have a limitation that the task must execute within 90 seconds The AppDomain shutdown can only be delayed 90 seconds (It’s actually the minimum of HttpRuntimeSection.ShutdownTimeout and processModel shutdownTimeLimit). If you have so many items queued that they can’t be completed in 90 seconds, the ASP.NET runtime will unload

IHostedService for tcp servers in .NET Core

放肆的年华 提交于 2019-12-07 07:49:30
问题 I am trying to build a small tcp server/daemon with asp.net core as a web frontend to interact with the server. I have found IHostedService/BackgroundService which seems to provide a low effort alternative to bundle the server and the frontend together. The code looks basically like this at the moment (echo server for testing purposes): public class Netcat : BackgroundService { protected override async Task ExecuteAsync(CancellationToken stoppingToken) { TcpListener listener = new TcpListener

IHostedService for tcp servers in .NET Core

梦想与她 提交于 2019-12-05 16:44:18
I am trying to build a small tcp server/daemon with asp.net core as a web frontend to interact with the server. I have found IHostedService/BackgroundService which seems to provide a low effort alternative to bundle the server and the frontend together. The code looks basically like this at the moment (echo server for testing purposes): public class Netcat : BackgroundService { protected override async Task ExecuteAsync(CancellationToken stoppingToken) { TcpListener listener = new TcpListener(IPAddress.Any, 8899); listener.Start(); while(!stoppingToken.IsCancellationRequested) { TcpClient

Integration Test For Hosted Service .Net-Core`

不打扰是莪最后的温柔 提交于 2019-12-04 17:48:17
问题 I have a QueueTask Hosted service (.Net-Core's new background service) that I'd like to test. My queuedHosted service looks like so: public QueuedHostedService(IServiceProvider serviceProvider, IBackgroundTaskQueue taskQueue, ILoggerFactory loggerFactory) { TaskQueue = taskQueue; _logger = loggerFactory.CreateLogger<QueuedHostedService>(); _serviceProvider = serviceProvider; } protected async override Task ExecuteAsync(CancellationToken stoppingToken) { using (var scope = _serviceProvider

Proper way to register HostedService in ASP.NET Core. AddHostedService vs AddSingleton

早过忘川 提交于 2019-12-01 02:19:01
What is proper way to register custom hosted service in ASP.NET Core 2.1? For example I have custom hosted service derived from BackgroundService named MyHostedService . How should I register it? public IServiceProvider ConfigureServices(IServiceCollection services) { //... services.AddSingleton<IHostedService, MyHostedService>(); } or public IServiceProvider ConfigureServices(IServiceCollection services) { //... services.AddHostedService<MyHostedService>(); } ? Here we can see first case, but here there is second case. Are these methods equal? They are similar but not completely

Proper way to register HostedService in ASP.NET Core. AddHostedService vs AddSingleton

若如初见. 提交于 2019-11-28 02:32:59
问题 What is proper way to register custom hosted service in ASP.NET Core 2.1? For example I have custom hosted service derived from BackgroundService named MyHostedService . How should I register it? public IServiceProvider ConfigureServices(IServiceCollection services) { //... services.AddSingleton<IHostedService, MyHostedService>(); } or public IServiceProvider ConfigureServices(IServiceCollection services) { //... services.AddHostedService<MyHostedService>(); } ? Here we can see first case,