.NET Core


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 …


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; …


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": " …


Limit ASP.NET Core request frequency by IP address

By default, the user can request an ASP.NET Core web server unlimitedly. The user may request our web server very frequently and submit lots of spam data. Also, too frequent requests may be a terrible attack which may cost our service down and lots of money. So how can we group the requests by their IP address, limit the frequency of the user requests, and return an error message? There's …


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 …