Anduin

let today = new Beginning();

.NET Topics about .NET Core and C#



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 .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 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 (aiursoft.com) -------------- 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

Display code coverage information for .NET Core project using Azure DevOps.

Target. Display code coverage information on your own .NET Core project. First, create a build pipeline in your Azure DevOps. If you are using the classic designer, you need to add new .NET Core command line step. Make sure you publish the test result. As for the Arguments input, insert: --configuration $(buildConfiguration) --collect "Code coverage" Or if you are using YAML: Add …

.NET Core Azure DevOps .NET Test MSTest Integration Test Code coverage GitHub

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. https://docs.microsoft.com/en-us/aspnet/core/test/integration-tests 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

Fire and forget in ASP.NET Core with dependency alive

C# fire and forget might not be suitable in ASP.NET Core To fire and forget in C#, it is really simple: Task.Run(() => FireAway()); But the same approach might not be suitable in ASP.NET Core Controller. Consider the following example: public class MyController : Controller { private readonly MyHeavyDependency _hd; public MyController(MyHeavyDependency hd) { _hd …

ASP.NET Core C# Async Fire and forget

How to serialize JSON object in C# without Newtonsoft Json

In many cases, we may not be able to access Newtonsoft.Json package. Like sometime there might be different packages using different versions of it, which may cause version conflict via build. That usually happens in some projects really large. So how can we play with JSON as while without it? In pure C#? First, copy the following class to your project: public static class MyJsonConverter { …

C# JSON .NET Newtonsoft.Json

Creating a proxy to another URL with ASP.NET Core

This post talks about writing a simple HTTP proxy logic in C# or ASP.NET Core. And allowing your project to proxy the request to any other URL. It is not about deploying a proxy server for your ASP.NET Core project. Before starting, you need to be clear about the target you are going to proxy for. It shall be an URL. Add the following code anywhere of your project. public static …

ASP.NET Core Reverse Proxy Web Proxy

Get unique random numbers in C#

The source code this blog mentioned is here: https://github.com/Anduin2017/SuperRandom The traditional methods for obtaining `n` non-repeating random numbers are: The random number is generated by the linear congruence method, and each random number is generated and compared in the database. If it already exists, the number is discarded. Randomly generate a linear sequence, and then …

C# .NET Core Algorithm Random Numbers Unique random Non-repeat Random