Anduin Xue

let today = new Beginning();


Raise up Visual Studio Code correctly in your C# client-side program

When I search this topic from Google, I always get results with 'How to write C# program in VS Code'. But what I was working on is an editor, which allows the user to open VS Code to edit a folder. My UI is like this: So what should I do to open the folder in VSCode? Code is below: Process.Start(new ProcessStartInfo { UseShellExecute = true, // IMPORTANT: …


Validate an object in any C# projects

Sometimes, we need to check if the input is valid. Usually, we may do this: if (string.IsNullOrWhiteSpace(InputArgument.TargetMachine) || string.IsNullOrWhiteSpace(InputArgument.PatchId)) { throw new ArgumentException($"Invalid input argument! Patch ID: '{InputArgument.PatchId}', Target Machine: '{InputArgument.TargetMachine}'!");  …

ASP.NET Core C# .NET Validation

C# Play with GZip.

A extension class for string to add GZip features. Copy the following code to your project first. using System; using System.IO; using System.IO.Compression; using System.Text; public static class GZipExtensions { private static byte[] Zip(string str) { var bytes = Encoding.UTF8.GetBytes(str); using (var msi = new MemoryStream(  …

C# .NET Compress GZip Base64

Retry with exponetial back-off on C#

Just built a simple retry engine in C#. /// /// Retry engine. /// public class RetryEngine { private static Random rnd = new Random(); private readonly ILogger logger; /// /// Creates new retry engine. /// /// Logger public RetryEngine(  …

C# .NET Retry

Build a common cache service for your C# app.

Recently, I found that almost every time I creates a new .NET app, I gonna need cache service. While Microsoft officially provides the IMemoryCache, I found that it is pretty complicated for you to use it. For it requires a lot of code. So I wrapped it to a more common one. Before starting, make sure the project references Microsoft.Extensions.Caching.Memory and Microsoft.Extensions.Logging.  …

ASP.NET Core C# .NET Core Performance Caching .NET Cache MemoryCache

Programmatically connect to the remote server via SSH and execute remote command.

Recently I was trying to build a server management tool. That I need to connect to my server (Ubuntu) programmatically. I usually access my server via SSH, as a normal human. So I wanna do it with the same approach. First, create a new .NET Core project with: dotnet new console After creating the new project, install the latest SSH.NET library. Reference: NuGet Gallery | SSH.NET 2020.0.1 And  …

C# .NET Core bash Linux SSH

Use JetBrains code quality analyzer to prevent checking-in bad C# code

When we are using Azure DevOps to review PR, it's simple and easy to use. Usually, we only run tests and build tasks, like this: In my previous blog, I mentioned how to run test and generate code coverage for CI pipelines: Display code coverage information for .NET Core project using Azure DevOps. - Anduin Xue ( -------------- JetBrains ReSharper and Rider are impressive C# grammar  …

C# Azure DevOps JetBrains Resharper Code Quality Pipelines

C# run tasks in a threads pool with fixed size

Imagine I have 100 tasks to run. But my machine has only 16 core. So I expect that every core will run one task in parallel. And once a core is idle, assign a new task to it. That will be very easy for C# with Task. But before starting, we gonna create a thread-safe queue for saving our tasks. public class SafeQueue<T> { private readonly Queue<T> queue = new Queue&  …

C# Async Task Multi-Threading

ASP.NET Core Integration Test using MSTest

In the official document, there is only an example of the ASP.NET Core integration test using xunit. But what if you don't like xunit? Can we replace that with MSTest? Yes. We can. Brief steps Uninstall xunit. Install MSTest instead. First, clear the project structure. Install MSTest for your test project. Start your  …

ASP.NET Core C# .NET Core Test MSTest Functional Test Integration Test