C#


C# Rotate the square matrix

This is one of my questions from an interview. The interviewer asked me to rotate a square matrix in C#. While it is not so hard for me and I have done it in 10 minutes, I'd share my solution to all developers who may need this. namespace Test { class Program { static void Main(string[] args) { var a = new int[][] { new int[] …


EF second layer cache to enhance your SQL database performance based on Redis

Entity-Framework Core is a lightweight, extensible, open-source, and cross-platform version of the popular Entity Framework data access technology. It really helps the developer to build applications which access database easily. But in most cases, we may cache some results which do not change frequently, to reduce access to our database. For example, the home page of a blog may not change …


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 …


HTTP post file from .NET Core new HTTP client

Now with .NET Core 3, Microsoft released a new .NET Core HTTP client. Reference: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-3.1 With the new client, to call a WEB API is much easier. For example, to make an HTTP GET request: var request = new HttpRequestMessage(HttpMethod.Get, url) { Content = new …


How to write a bot for Kahla

Kahla.SDK Kahla.SDK is a library for writting bots and extends for Kahla. Tutorial - How to create a bot with Kahla.SDK This will introduce how to write a bot for Kahla. Before starting, make sure you have .NET Core SDK installed. Download .NET Core SDK here. 1. Create a new console .NET Core app Open your terminal and type the following command to create a new console app. $ mkdir …


Creating a Model for an existing database in Entity Framework Core

Try to execute the following command under your project folder: $ dotnet ef dbcontext scaffold "Server=....." Microsoft.EntityFrameworkCore.SqlServer -o Models And fill the string Server=..... with your database connection string. Make sure that you can connect to this database successfully. And EF will try to build models from the database. Note: Once you have created the model, you …


Sync data to database with Entity-Framework Core

We already know how to add data to database. That's simple: _dbContext.MyDbSet.Add(myObject); But there may already exists some data in the database. We need to delete the obsolete data, and try to add the missing data. For example, I have some numbers: 1, 1, 2, 2, 3, 3 While in the database there is: 1, 1, 1, 5 To seed the database to the way we expected, we shall delete the first 1 and …


C#获取腾讯云直播的推流地址和观看地址

最近公司有个项目想对接腾讯云直播。腾讯云直播的推流链接是在客户端生成的,所以这段代码就别指望调用API获取了。 遗憾的是,腾讯云的网站上,只给了Java和PHP的代码示例,并没有给出C#的代码示例: 而我实在看不懂他Java的代码实例中那个txTime参数是从哪冒出来的。自己试了试各种方案,最后终于猜出来,原来是自1970年到今天的总秒数。 剩下的就是求一下MD5,拼接字符串,转一下十六进制。都是体力活,拿C#写也不算费劲。 折腾一阵后,终于搞定。 using System; using System.Security.Cryptography; using System.Text; namespace TencentCloud { public class Program { // Any value is fine. …


Scan all accessible class in C#

Getting all accessible assemblies in C# is easy. Calling GetReferencedAssemblies and you can get all you referenced assemblies. But project reference is recursive. To get all the class, you need to build a reference tree and read from the entire tree. Consider the following code: private IEnumerable<Assembly> ScanAssemblies(Assembly entry) { yield return entry; …


Share view component between different ASP.NET Core web project

When some logic in C# can be shared between projects, we gonna create a new class library project. But sometimes we just want to share some view components. For example, for all pages in Aiursoft, it all contains a Logout component. Writing this more than one time doesn't make sense. So how can we share it? First, we gonna create a new .NET Core class library project alongside it. Name it: …


Inject an instance of a class with all default values

Last week while I was building a wiki generator and realized that I have to generate the possible responses of an API. Which means I need to get an object with all meaningful values in it. For example: { "value": { "id": "69c8981d-3498-48ff-8126-fcee0e8c8929", "conversationId": 0, "ats": [ { "targetUserId": "an example string." } ], "senderId": " …


Soft deletion in Entity Framework Core

Soft deletion feature is important in some cases that you need a second step to delete the data in your database. When you first delete it from your C# LinQ query, the data will only be marked as Deleted but not delete in your database. Multi-tenancy is one of the ASP.NET boilerplate's important features. But how can we implement the soft-deletion feature with only pure Entity Framework Core? …


Support multi-tenant in pure Entity Framework Core

Multi-tenant requires many features because the data can be separated by different tenants. So when you are accessing the database, you don't need to worry that your operation will affect other tenants. Multi-tenancy is the ASP.NET boilerplate's most important feature and works perfectly with Domain-driven design. But how can we implement the multitenant feature with only pure Entity Framework …


Auto update database for ASP.NET Core with Entity Framework

If your ASP.NET Core project is connecting to your database via entity framework, you can't be unfamiliar with the script: dotnet ef database update. Without executing the script your database is not the latest and this may cause some issues. Traditionally we update the database every time we start our app or publish our app. But we may forget this process and it costs us work. How can we …


How to run async method in C# synchronous method

In some situations, we may need to run an async method and get its result. But we can't access the await keyword. In the constructor method. When you are implementing an interface sync method. When you are implementing an abstract class sync method. I got a solution to unwrap the async method and call it in the synchronous method. First, put the following code anywhere you like: using …


Consolidate all Entity-Framework database migrations to one migration

Why do we need to do that? In case you ruined your code or you have too many migrations and caused your editor slow, you may want to reset all migrations. However, deleting all migrations directly in your code will cause database update fail. So I will talk about my solution about how to consolidate all migrations without deleting any data. The following steps only suitable for code-first EF …


Upgrade existing class library to .NET Core 3.0

When we are upgrading our existing class library to the latest .NET Core, like this: <TargetFramework>netcoreapp3.0</TargetFramework> On doing so, you may get the following warning: C:\Program Files\dotnet\sdk\3.0.000\Sdks\Microsoft.NET.Sdk\targetsMicrosoft.NET.Sdk.DefaultItems.targets(149,5): warning NETSDK1080: A PackageReference to Microsoft.AspNetCore.App is not necessary …