Sign in

Senior Application Developer. MBA. I write blogs about .NET, Angular, JavaScript/TypeScript, Docker, AWS, DDD, and many others.

How to Design and Implement Robust Background Jobs in Our Applications

Most background jobs are running behind the scene and are transparent to end-users, and admins usually don’t need to worry about the nitty-gritty of how the system automates tasks or when the system executes them. In some cases, product owners may know that there are background jobs supporting the application because they are involved in the requirement gathering phase of the application development lifecycle. However, as time passes or initial product owners transit away, corporate knowledge of background jobs starts to fade, unless the current product owners are tech-savvy users or they have detailed documentation at hand. …


How to implement and test IP restrictions in an ASP.NET Core Web API project

We sometimes want to limit our application to only certain IP addresses or IP network ranges. For example, during beta testing, we prefer to only allow admins and preselected testers to access the new website. Besides blocking the whole application, we sometimes want to expose most APIs in an application but restrict a few API endpoints to a confined network so that sensitive data won’t be scrapped.

IP restrictions can be done at several levels to achieve security goals. As developers, we can properly guard our applications and API endpoints in the code so that they are available to trusted…


How to return a file from an API endpoint in ASP.NET Core and create an HTML download link.

In this article, I will use a demo Web API application in ASP.NET Core to show you how to transmit files through an API endpoint. In the final HTML page, end users can left-click a hyperlink to download the file or right-click the link to choose “Save Link As” in the context menu and save the file.

The full solution can be found in my GitHub repository, which includes a web project for uploading/downloading files and an integration test project for testing controller methods.

Now let’s dive in.

Return a File in ASP.NET Core Web API

In ASP.NET Core, a Web API action method usually returns an ActionResult


Surprise! The arrays are not replaced as a whole.

ASP.NET Core uses configuration providers to read configuration key-value pairs from a variety of sources. You can read more about configuration in ASP.NET Core in its official documentation, which has covered tons of use cases in detail.

Recently, when I set a JSON array in the appsettings.json files in an ASP.NET Core project, the result of the final configuration data surprised me. The array did not behave the same as other JSON objects that could be overridden in an environment-specific appsettings.json file. Instead, the final array could have more elements than expected. After some googling, I found that there are…


How to create an immutable collection of cache objects and how to unit test IMemoryCache

Caching is a common technique to reduce the number of trips for fetching data that seldom changes. Thus we can take advantage of caching to boost our application’s performance. ASP.NET Core provides several caching libraries, including in-memory caching, distributed caching, and many others. Among these caching techniques, IMemoryCache, the simplest cache, is included in an ASP.NET Core web project by default and works natively with dependency injection.

There are many introductory articles talking about using the IMemoryCache to store data in the memory of the webserver. However, few of them have mentioned how to ensure the consistency of cached values…


How to upload multiple files through one HTTP request to an API endpoint

I wrote an article entitled Upload Files with Angular and .NET Web API recently. To continue on this subject, in this article I will show you the details of creating an Angular component to upload multiple files within one HTTP request to an API endpoint. The demo app works as the following screen recording shows.


How to write integration tests for API endpoints using MSTest

We often want to write integration tests to evaluate our app’s whole request-response pipeline. For example, we want our API endpoints to return the same responses given the same requests. These integration tests are helpful in catching cases of missing properties in the JSON responses, mismatching upper/lower cases of property names, obsolete error messages, and so on.

The ASP.NET official documentation has an article entitled Integration tests in ASP.NET Core, which covers lots of aspects of integration tests in ASP.NET Core using the xUnit test framework. However, the magical IClassFixture<T> in xUnit creates confusion and difficulties for developers who are…


How to write concise code using Tuples as DTOs

In this article, I will show you how tuples can be used to save some coding in certain scenarios. The idea is simple, but I don’t see similar code much, so I think it’s worth writing a short article to introduce it to more people.

Data Transfer Object

A data transfer object (DTO) is an object that carries data between processes. In a lot of articles, DTOs are described as simple containers for data to be transferred across system boundaries. The key signature of a DTO is that it does not contain any business logic or behavior.

For example, when we query a…


How to create an Angular component for uploading files to a .NET API endpoint

In this article, we will go through details of creating an Angular component for uploading files to a .NET Web API endpoint. The following screen recording shows the demo app.


How to quickly set up test data in unit tests

The builder icon is made by Good Ware from www.flaticon.com; The MOQ icon is from its Git repository.

We regularly need to create some test data (or objects) for unit tests. A lot of unit tests only utilize a part of the input object and disregard the rest of the properties of the data object. For example, if a Person object contains a name, gender, birth date (and so on) and we want to write a unit test to check the calculation of a person’s age, we know that a person’s age only depends on the current date and the person’s birth date, and the person’s other attributes do not matter in this specific unit test. …

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store