Edi Wang

.NET and Azure Developer

Posts in 2021

Automated Deployment Script with Azure CLI and PowerShell Core

My blog project, "Moonglade", is optimized for running on Microsoft Azure. It can combine up to 14 Azure services together for a full deployment, manually setup is difficult and takes time. I chose Azure CLI with PowerShell Core to write a deployment script that enables a quick on-board experience to setup Moonglade in 10 minutes with minimal Azure Services. Let's see how it is done. …
SQL Server Azure App Service

How to Print Borderless Photos on Cannon MG2580s

I have a Cannon MG2580s printer, it can print 4x6 inch (10x15cm) A6 size photos, but with ugly white borders even the printing software is set to borderless. This is because this model of printer does not support borderless printing on hardware level, it's not a software or settings issue.  I've come up with a workaround that can produce borderless photo printing. …
Printer

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

Upload File with Progress from Browser to Azure Blob Storage

Recently I am working on an internal OA system for a small company. One of their business requirement is for the users to upload attachments when creating a ticket. Azure Blob Storage is ideal for this scenario. However, the attachments are sized from a few KB to hundreds of MB, so it is necessary to show a progress bar when user upload the files. Let's see how can we easily do it with Azure. …
Azure Azure Blob Storage

Identify Users from Different Countries and Regions for Websites on Azure

For websites that need to display different content for users in different countries and regions, we usually identify the user's location based on the IP address. It means we have to write our own code, purchasing and maintaining an IP database, or using a third-party IP data service, it will take huge efforts. How to quickly do it without purchasing an IP database and write a lot of code? …
Azure Front Door

Hosting Static HTML Website with Azure Static Web Apps

When I choose free static website hosting plans, I would use GitHub pages in the past. But now, since the GA of Azure Static Web Apps (SWA), I now have another free of charge option. I've tried to host a few pure HTML/JS website as well as .NET Blazor WASM applications with Azure Static Web Apps. In this post, I'll share how to create a static website by SWA. …
Azure

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

How to Prevent Access to App Service Url Behind Azure Front Door

In my previous posts, I have introduced usage of Azure Front Door. However, when we finish setting up the front door and bind our domain to the front door, users can still access the App Service URL. In this post, I'll introduce how to prevent access to our App Service when using Azure Front Door. Problem For example, my blog is binding https://edi.wang to my front door, I have 2 App Services …
Azure App Service Front Door

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