Edi Wang

.NET and Azure Developer

ASP.NET

Cloud Architecture Design of My Email Notification Infrastructure on Azure

My blog system "Moonglade" needs to send email notifications to blog owner when there are events like new comments, new pingbacks, and send email to readers when their comments are replied.  I've been building my own infrastructure in the last few years. I will share the journey of how this email infrastructure evolved and the things I learnt. …
ASP.NET Azure Azure Function Azure Storage Queue

ASP.NET Core Unit Test: How to Mock HttpClient.GetStringAsync()

In ASP.NET Core unit tests, if you want to mock HttpClient.GetStringAsync(), here's the trick. Problem Given the following code var html = await _httpClient.GetStringAsync(sourceUrl); When I tried to mock HttpClient.GetStringAsync() like this var httpClientMock = new Mock<HttpClient>(); httpClientMock .Setup(p => p.GetStringAsync(It.IsAny<string>())) .Returns(Task.FromResult("...")); Moq …
ASP.NET Unit Test

ASP.NET Core Unit Test: How to Mock HttpContext.Features.Get()

In ASP.NET Core unit tests, if you want to mock HttpContext.Features.Get<SomeType>(), here's the trick. Problem I have my Error page code that will get exception detail infomation, to do that, I use HttpContext.Features.Get<IExceptionHandlerPathFeature>(). public void OnGet() { var requestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier; var exceptionFeature = HttpContext.Features. …
ASP.NET Unit Test

ASP.NET Core Unit Test: How to Mock Url.Page()

Problem In ASP.NET Core, when you use extension methods on UrlHelperExtensions class, it would be difficult to write Mock in unit test. Because Moq doesn't support mocking extension methods. For example, the following code that I use in my blog is using Url.Page() method: var callbackUrl = Url.Page("/Index", null, null, Request.Scheme); But in my unit test, mocking like this will throw …
ASP.NET Unit Test

ASP.NET Core 5.0 Throughput Test in Kestrel, IIS, Nginx and Caddy

Starting from version 2.2, ASP.NET Core allow you to use the InProcess mode to improve performance under IIS. Rick Strahl has a detailed article on this. Three years have passed, and now ASP.NET Core has reached version 5.0, how the performance diffs between servers? Let's take a look together. Rick's Test Result In the original article, Rick Strahl tested the performance of ASP.NET Core 2.2 in …
ASP.NET IIS Linux

IP Rate Limit for ASP.NET Core

In websites or API applications, we often want to block high-frequency requests in a short period of time for an IP to prevent boring people or malicious attacks. In ASP.NET Core, limiting the request rate of IP is now super easy, let's see. AspNetCoreRateLimit There's already a nice library for limiting request rate, called AspNetCoreRateLimit. GitHub: https://github.com/stefanprodan/ …
.NET ASP.NET IP

Restart an ASP.NET Core Application Programmatically

We developers won't have access to servers all the time, and without management capabilities, it will be difficut to restart a web application. However, in ASP.NET Core, there is a way for us to programmatically restart our application without requring management capabilities from server admins. IApplicationLifetime There is a IApplicationLifetime interface in ASP.NET Core which can handle events …
.NET ASP.NET

Auto Refresh Settings Changes in ASP.NET Core Runtime

In ASP.NET Core, if you modify the settings in appsettings.json, you will have to restart the site to take effect. Is there a way to refresh and apply it automatically after you modify the settings? Background Let's take a look at a website created by ASP.NET Core default templates. It includes two settings files: appsettings.json appsettings.Development.json The former one is used in production …
.NET ASP.NET

Dependency Injection with Multiple Implementations in ASP.NET Core

The built-in Dependency Injection (DI) in ASP.NET Core is very useful, but how do you deal with an interface has multiple implementations? Can the runtime choose one of these implementations based on configuration? Is there a way to get rid of reflection? Let me show you how to dynamically select a specific implementation of an interface at run time according to the configuration file without …
.NET ASP.NET Design Pattern Dependency Injection

Path Caveat with ASP.NET Core 2.2 IIS Hosting

ASP.NET Core 2.2 has been in place for some time, with a new feature that can use the new AspNetCoreModuleV2 and deploy with InProcess mode on IIS to dramatically improve performance. These days Azure App Service finally completed the deployment of this new version of the module, I configured my blog to the new module, and it exploded in production. Let's see why and how to solve it. If you don't …
.NET ASP.NET IIS