ASP.NET Core – How to receive a file in a web API request

When the client posts a file in a multipart/form-data request, it’s loaded into an IFormFile object. This contains file information (such as the file name) and exposes the file content as a stream. This allows you to save the file or process it however you want to. You can access the IFormFile object through Request.Form.Files: … Read more

ASP.NET Core – Getting query string values

The ASP.NET Core framework automatically parses query strings (i.e. ?name=Dune&year=2021) into HttpContext.Request.Query and maps the query string values to action parameters (if you’ve added them). You can get the mapped query string values by adding action parameters, like this: Or you can use HttpContext.Request.Query directly (which is useful in many scenarios): This can either return … Read more

ASP.NET Core – How to unit test an action filter

To unit test an action filter, you have to pass in an action filter context object (which requires a lot of setup). Action filter methods are void, so you have to verify the behavior by inspecting the context object (or dependencies, like a logger, if you are injecting those). Here’s an example of doing the … Read more

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