ASP.NET Core – Log messages to the database in the background

I was reading about logging in ASP.NET when I came across this statement about logging to the database: When logging to SQL Server, don’t do so directly. Instead, add log messages to an in-memory queue and have a background worker dequeue and insert data to SQL Server. Paraphrased from In this article, I’ll show … Read more

How to stop the ASP.NET service when a background service crashes

When a background service throws an exception in ExecuteAsync() and stops running, nothing happens. No errors are logged, and the ASP.NET service continues to run. What if you want to stop the ASP.NET service when the background service crashes? To do that, you’ll need to handle exceptions in the background service and call IHostApplicationLifetime.StopApplication(), like … Read more

C# – How to supply IOptions

The options pattern is an indirect way to dependency inject settings into a registered service. If you’re using code that implements the options pattern, then you’re required to supply an IOptions<T> object. For example, let’s say you’re using the MovieService class and it has the following constructor: This requires you to supply the IOptions<MovieSettings> parameter. … Read more

ASP0000 Calling BuildServiceProvider from application code results in an additional copy of singleton services being created

When you try to call services.BuildServiceProvider(), you’ll get the following warning: Warning ASP0000 Calling ‘BuildServiceProvider’ from application code results in an additional copy of singleton services being created. Consider alternatives such as dependency injecting services as parameters to ‘Configure’. There are two scenarios where you may be calling BuildServiceProvider() because you want to resolve services … Read more

ASP.NET Core – Dependency inject a background service into the controllers

Let’s say you have a background service called DatabaseLoggerService. It runs as a hosted background service and logs messages to the database. It has the following definition: You want your controllers to use this for logging. They don’t need to know about the concrete DatabaseLoggerService class, and they don’t need to know they are actually … Read more

How to use NLog in ASP.NET

When you want to use NLog in ASP.NET, the first step is to install and configure NLog. Then you can either use NLog directly or fully integrate NLog and ASP.NET. Use NLog directly if you prefer to have static ILogger properties, instead of using dependency injection. The downside of this approach is that you’ll have … Read more

ASP.NET Core – How to unit test your middleware class

There are three requirements for unit testing a middleware class: Mock out RequestDelegate by defining a lambda that returns a Task (Task.FromException, Task.FromResult, or Task.FromCanceled). Mock out HttpContext by using DefaultHttpContext. The middleware function needs to be awaited, so your unit test needs to be defined with async Task. Here’s an example: This is a … Read more

ASP.NET Core – How to add your own middleware function

Middleware functions have access to requests before they are sent to the controllers. Likewise, they have access to responses from the controllers before they are returned to the client. This is useful for doing things like logging the request and response, generating stats about requests, handling exceptions, and many more scenarios. In this article, first … Read more

ASP.NET Core – How to make the controllers use Newtonsoft

By default, ASP.NET Core uses System.Text.Json for JSON serialization. If you want to use Newtonsoft instead, you can add the Microsoft.AspNetCore.Mvc.NewtonsoftJson nuget package, then call AddNewtonsoftJson() in Startup.ConfigureServices() like this: In this article, I’ll show how to configure the Newtonsoft serializer options. Install the right nuget package Microsoft packaged up Microsoft.AspNetCore.Mvc.NewtonsoftJson based on the framework … Read more

ASP.NET Core – Self-hosted service stub with a command line interface

When you’re integrating with a third-party API, you may want to send requests to a service stub instead of sending them to the real API. The purpose of a service stub is to simulate the third-party API by returning hardcoded responses. This is similar to how mocks are used in unit testing – it helps … Read more