Hosting services in .NET Core console application

Building .NET Core console applications with IHost and HostBuild to take advantage of IHostedService, graceful shutdown, dependency injection, logging, hostbuilder, configuration and more.

When building services for data processing you do not always need a user interface. An IHost is very capable of hosting such an application in a console application as headless service. The IHost does give you a number of advantages like graceful shut down, dependency injection, logging, and configuration. When running long processing tasks in Docker containers, graceful shut down helps you to keep the state of your application consistent. This post explains how making use of the generic IHost in .NET Core for headless services.

Continue reading “Hosting services in .NET Core console application”

Advertisement

HttpClient and HttpClientFactory in ASP.NET Core

Use the new HttpClientFactory to create HttpClient objects in ASP.NET Core. Learn how to create Named or Typed HttpClient instances.

With .NET Core 2.1 the HttpClientFactory is introduced. The HttpClientFactory is a factory class which helps with managing HttpClient instances. Managing your own HttpClient correctly was not so easy. The HttpClientFactory gives you a number of options for easy management of your HttpClient instances. In this post I’ll explain how to use the HttpClientFactory in your ASP.NET Core application.

Continue reading “HttpClient and HttpClientFactory in ASP.NET Core”

Injecting a Scoped service into IHostedService

Cannot consume scoped service ‘IScoped’ from singleton ‘Microsoft.Extensions.Hosting.IHostedService’.

In our projects we are using IHostedService to run background processes. What I have seen is that when injecting a scoped service into the IHostedService you get a InvalidOperationException on startup. The first time you get this exception, it is takes some time to figure out what is going on. This blog post will help you to resolve the problem faster.

Continue reading “Injecting a Scoped service into IHostedService”

Run scheduled background tasks in ASP.NET Core

In the previous blog post called background tasks with ASP.NET Core using the IHostedService Peter described how to use the IHostedInterface for background tasks. In this post, we continue on this subject and add some pointers on how to perform scheduled background tasks.

In many software projects, there are repetitive tasks; some do just repeat every x seconds after the last instance is finished but you might also have to run a task on a schedule like every 10 minutes. When building repeating or scheduled tasks there are many options on how to approach the scheduling and this approach can be influenced by a number of technical choices.

Building the scheduling yourself is an option when you do not want to add extra dependencies to your project, have full control or just want an extra technical challenge. An out of the box solution you can a look at Hangfire, Quartz.net, or an external service that does an http call every x seconds to trigger the task (something like Pingdom).

Continue reading “Run scheduled background tasks in ASP.NET Core”

WebHost CreateDefaultBuilder, what differs if you run in Development

Knowing the differences when you run your application in a Development environment

When building ASP.NET Core 2 web applications, most projects are initializing Kestrel with the CreateDefaultBuilder. If you use this extension method to configure Kestrel, it is good to know what is set and what differs when you are running a Development configuration or any other configuration. In practice there are two differences. In this blog I’ll explain the two differences.

Continue reading “WebHost CreateDefaultBuilder, what differs if you run in Development”

ASP.NET Core background processing with IHostedService

Run background processes with the IHostedService and how to inject your services with dependency injection

Many services need background processing. The ASP.NET Core 2.X IHostedService interface gives you an easy implementation skeleton to implement background processes. The Hosted Services are registered in the dependency injection at startup and started automatically. You do not have to do the pluming to get them started at startup. On shutdown you can implement a graceful shutdown. When running background processes there a few pitfalls to avoid. In this blog I’ll introduce the IHostedService and how to avoid common memory leaks when implementing the hosted service.
Continue reading “ASP.NET Core background processing with IHostedService”

Cannot access a disposed object in ASP.NET Core when injecting DbContext

Find the root cause of what really happened to you disposable object.

Working with ASP.NET core I have seen this error multiple times and can be hard to debug. The error can have multiple root causes. It can be a real pain to find the cause and it is sometimes fixed by only taking care of the symptoms. By finding the root cause, you can take the appropriate action in your code.

Continue reading “Cannot access a disposed object in ASP.NET Core when injecting DbContext”