Edi Wang

.NET and Azure Developer

.NET C# Programming Language and .NET Platform

Adding Azure Cache for Redis in My Elf Project

ASP.NET Core Memory cache has its own limits. It is difficult to scale in a cloud environment with multiple instances. Azure Cache for Redis provides a fully managed distributed cache service for cloud applications. It is built on top of Redis, a popular cache and messaging product. Integrating Redis into cloud applications can improve performance and scalability. …
Azure Redis

How to Get Access Token for Azure REST APIs in .NET

I am recently working on a project that requires front-end to call Azure REST APIs. Microsoft document describes how to get Access Token in postman via Jon Gallant's blog post. However, placing secret keys in front-end is very dangerous. In my project, I must get the Access Token from server side in .NET. Let's see how to do it. …
Azure Azure AD

Log User Identity Information to Azure Application Insights

Azure Application Insights is a very powerful APM tool for monitoring web applications. However, not all features that we sometimes require come out of the box. I now have an ASP.NET Core Web API application that uses JWT authentication. I would like to log user's identity when there is a failed request. Let's see how to do it. …
Azure Application Insight

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

Blazor WASM 实现人民币大写转换器

.NET 5 正式发布已经有一段时间了,其中 Blazor 技术是该版本的亮点之一。作为微软技术的被坑者,年少的我曾经以为 SilverLight 能血虐 Flash,Zune 能团灭 iPod,WP 能吊打 iPhone,UWP 能统一全平台…… 可是后…… 最终步入大龄程序员的我发现,只有陪伴了我将近 20 年的 ASP.NET 还没有完蛋。于是我这两天花了点时间,尝试将我的一个 UWP 小工具用 Blazor 重写,分享给大家。 无法抢救的 UWP “人民币大写转换器” 是我年少无知时开发的小工具之一,它的主要功能有: - 将数字金额转化为大写中文 - 复制结果 - 使用中文语音朗读结果 - 显示参照表 可惜 UWP 不论是充满 Bug 的 SDK,Runtime,还是微软的龟速更新与混乱的规划,都已经无可救药了,是时候给应用找个新家了。 Blazor Blazor 是 .NET …
.NET Blazor WASM

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

Migrating an ASP.NET Core Web API Project to Azure Function

Some time ago, I successfully migrated an ASP.NET Core Web API project to the latest Azure Function V3, thereby taking advantage of the serverless features of the Azure platform to reduce operation and maintenance costs by 10 times. This article will introduce the key steps and practices in the migration process to help you migrate similar Web API projects. Project background This Web API …
.NET Web API Azure Azure Function

How to Use the Latest .NET Core Runtime on Azure App Service

Microsoft often releases updates to .NET Core, usually as security patches. This morning, the .NET Core 3.1.5 update was released. However, Azure App Service's own .NET Core runtime will not be updated immediately, and we would usually wait a few weeks before Microsoft rolls out the update to Azure. For applications with high security requirements, it is urgent to complete the update before …
.NET Azure App Service