Anduin

let today = new Beginning();

All Posts in 2020


FFmpeg 用法概览

用法概览 ffmpeg有两种用法,一种是复制流,一种是编码流。 复制流 如果采用复制流,其参数简单,工作效率会极高,而且处理器占用率极低,往往能够每秒复制几千帧,是视频播放速度的几百倍。这种方法非常推荐。 但是,复制流无法修改视频的码率、分辨率,也无法改变视频格式。 如果需要直播,建议先将视频编码为可以直播的视频,再使用复制流的方法推流。 其中, 一般RTMP直播服务器接受的视频格式有:h.264编码的所有视频,一般为mp4格式或flv格式。一般RTMP直播服务器接受的音频格式有:aac编码的所有视频,一般为aac格式或mp4格式。 警告:其中Lavc57.89编码器编码的所有mp3格式音频无法推流。 编码流 如果采用编码流,其参数复杂,工作效率极低,处理器占用率极高,一般能够达到和视频播放速度相当或显著低于视频播放速度。这种方法仅适合于非直播场景。 但是,编码流,可以修改流的码率、分辨率 …

bash FFmpeg

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

Enable PowerShell remoting for Windows Server machines

Like you can use SSH to execute remote command on a remote Linux machine and you can also execute remote PowerShell on a target Windows Server. To enable it with a self-signed certificate, execute the following command: (Execute it on your server) > Get-ChildItem WSMan:\Localhost\listener | Remove-Item -Recurse # Remove old listeners > Cert = New-SelfSignedCertificate -CertstoreLocation  …

PowerShell Windows Server Remote Management WMI

Best-practice for authentication after creating a new Linux server

This article helps you change the following items if your cloud provider didn't do that for you. Create your own account instead of root. Disable password login and force to use SSH key to log in. Disable root account. Enable sudo without password. Before starting the process, make sure you have a valid SSH key-pair locally. Fist, connect to the server. (With root password). you@local  …

bash Linux Ubuntu SSH

Setup auto-renew wildcard HTTPS certificate on Azure virtual machine

Sometimes we might need to host a website in Azure. And there are so many options, like storage account, app service. But none of those supports wildcard HTTPS and auto-renew. An ideal configuration shall be static which means that it will never crash even when no one maintains it. Auto-renew is a must-have feature for HTTPS. Luckily, I got a solution based on win-acme: https://github.com/win-  …

Azure Azure DNS Windows Server HTTPS Win-ACME Certificate letsencrypt ACME

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[]  …

C# Algorithm Matrix Math

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

Fix Azure Media Player can't play video in iOS mobile devices

Recently, when we are trying to distribute video from Azure Media Service, we noticed that our video can't be played in iOS mobile devices. But the same web page plays normally in Android devices. There are multiple reasons for iOS devices to load and play Azure Media Player. As for the player itself is not open source, it might happen some strange effects when I change these settings. But the  …

Azure Media Player Azure Azure Media Service

微信的架构是如何实现分布式消息分发?

我们每天的生活都离不开微信。微信基本上已经成了在中国生活的一部分。 但是,微信有些方面的功能总是看起来令人觉得有些奇怪。例如: 微信几乎是第一款需要手机扫码才能登录PC端的 微信群和微信聊天记录在切换设备后会完全无法同步 你无法得知自己是否被一个微信好友拉黑了 你不能同时在多个移动设备上同时登录微信 从产品的角度来看,这些设定看起来彷佛是在给用户制造障碍,也成了大家普遍对微信的槽点。那么,微信的团队为什么要这么设计,而为什么又不改良它的体验呢?我想或许问题并不只是产品和需求分析这么简单,而是还需要进一步探寻架构上的本质。 在我生活在澳大利亚期间,我发现一件很有趣的事情:在澳大利亚,我使用微信给同样在澳大利亚的好友发送微信消息,他也可以几乎瞬间收到。假想微信在国内建立单点的服务器,那么这种性能一定不足以支撑澳大利亚内的通信。数据从澳洲抵达中国再从中国推送到澳洲至少需要500ms。而表现来看 …

Web WeChat Distributed Messging IM

Download any Azure Media Service video or live stream with FFmpeg.

This blog shows a tutorial about how to download any Azure media service video or live stream. Before starting, you need to have FFmpeg installed. No matter you are using Windows, Linux, or Mac OS. Download latest FFmpeg here: https://ffmpeg.org/download.html After installing it, prepare a command: ffmpeg -protocol_whitelist file,http,https,tcp,tls,crypto -i "{0}" -c copy video.mp4 And you  …

Azure Media Player bash Azure Azure Media Service Download FFmpeg

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  …

ASP.NET Core C# SQL Server SQL Entity Framework Core Azure Redis Caching

软件定制行业为何应当发展软件订阅制?

凡是涉及到事业单位的项目,涉及到知识产权外包的,通俗来说,就是自己写完就卖出去的代码,普遍软件的质量都相当差劲,漏洞百出,功能欠缺。开发商(乙方)在验收完成后,等付款方(甲方)完成验收,安装完成后,往往拿钱走人就去拓展下一个项目。这种低质量的软件严重影响企业的扩展和用户体验,甚至能够影响整个行业的健康度和。对于我们普通人的认知,就是非互联网企业的线上服务体验往往都很遭。 表面来看可能是因为构建软件的程序员水平较弱。但是如果它形成了一个现象,那么问题一定是在规则上。在于软件本身质量而引发的责任在多次承包后足够弱化,导致真正构建软件的人根本不在乎代码的质量。这是由于现在普遍的商业采购模式导致的。而要想解决这个问题,发展软件订阅制是个非常不错的方案。 对于甲方来说,采用订阅制来采购软件,就是按时间和用量付费,而不购买整个软件源码的知识产权。换句话说,就是买软件的使用权和一种服务,这个服务就是  …

DevOps China Software Development SaaS Subscription

在前端哈希密码是否是个不错的方案?

脑筋急转弯:为了防止运维人员能够直接去数据库里查看密码,Web应用普遍采用将密码哈希一趟后再存入数据库。需要校验时,由前端提供密码值,服务器将密码哈希后再比对,从而判断密码是否正确。这样运维人员即使接触的到数据库,也最多只能重置密码,无法登录一个特定的账号。那么,既然最终需要比对的是密码的哈希,为何不直接在前端哈希完,再直接把哈希后的结果发给服务器,来鉴定密码是否正确呢?答案其实挺简单的。当初我们加一次哈希是为了阻止运维登录。而如果哈希在前端完成,那么对于服务器而言,它其实只是做了一次明文的比较。数据库里的哈希值就相当于密码了。运维完全可以直接拿着哈希去调登录的API来获取一个账号的权限。 这个脑筋急转弯挺有趣的,乍一看仿佛还挺有道理的。但是换句话说,虽然看起来前端加哈希蠢蠢的,其实历史上有很多公司和项目都做过类似的事情。其中比较典型的就是腾讯公司的QQ了。 在十几年前的时候,  …

Security Password Hash

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


Tips for Azure CLI script programming on bash

Ask the user to select the Azure cloud under which he runs the Azure CLI: login_azure() { no_cloud=true while $no_cloud; do echo 'Select your cloud:' az cloud list --output table read -p 'Enter the name:' cloudName az cloud set --name $cloudName && no_cloud=false done az account show || az login echo 'To logout, please press `  …

bash Azure DevOps Azure CLI

Bootstrap dark theme minimum style

Now Apple is forcing all apps in its app store to support dark mode. Also, most native Android apps already support the dark theme. Viewing a screen which white background when you enabled dark mode on your device will greatly harm the viewer's eyes. Like lots of websites are made by bootstrap. So how can we get a solution with minimum changes and allows the website to support automatically  …

Web CSS Bootstrap Media Query Style Dark theme

Microsoft account integrated sign in via C#

This code example indicates how to build an app that supports Microsoft account OAuth authentication. Before coding, we gonna create an app in your Azure portal first. https://portal.azure.com The name is your app's display name. Select your app can access accounts in any organization and personal Microsoft account. As for the redirect URI, it must be your server redirect back address. For  …

ASP.NET Core Azure Microsoft OAuth Login Authentication

Install Azure CLI on Windows 10 and use it in bash

The Azure command-line interface (CLI) is Microsoft's cross-platform command-line experience for managing Azure resources. The Azure CLI is designed to be easy to learn and get started with, but powerful enough to be a great tool for building custom automation to use Azure resources. The Azure CLI is available to install in Windows, macOS and Linux environments. It can also be run in a Docker  …

bash Azure Azure CLI PowerShell

Ubuntu bash experience for Windows Terminal without Linux subsystem

Bash is the major shell client I use. On Windows 10 I really enjoy git-bash. But how to set bash to the default terminal in the new Windows terminal? Brief steps: Install a Linux style shell like git-bash. Install the Windows Terminal Make git-bash the default terminal in Windows Terminal Make the theme looks like Ubuntu style. Allow Ctrl + V to paste Allow starting WT in the directory  …

bash Ubuntu Windows 10 Windows Terminal

Publish app from Azure DevOps to non-global Azure like Azure CN

如何使用Azure DevOps将编译完的应用发布到非Azure Global环境,例如Azure CN。 Publishing an app from Azure DevOps to Azure global is super easy. Simply a few clicks and you can find your azure subscription and publish it in a minute. Like this: But in some cases, our customers might be using other Azure environments, like Azure Arc, Azure for government and Azure CN. And we can't find our subscriptions easily via  …

Azure App Service Azure Azure DevOps DevOps Azure CN China

Linux Cheatsheet for Windows Users

A notebook for Windows folks like me who struggle to remember Linux commands Before starting Control options Operation Command Example Execute command one by one command1; command2 Execute command1 background command1 & command2 Execute command2 if 1 success command1 && command2 Execute command2 if 1 fail command1 || command2 Reverse the command result !command1 Pipeline options  …

bash Linux Ubuntu

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. Create a new console .NET Core app Open your terminal and type the following command to create a new console app. $ mkdir  …

C# .NET Core Aiursoft Kahla Bot Kahla.SDK Kahla App Console App

Creating a Model for an existing database in Entity Framework Core (DB First)

Install EF first: dotnet tool install --global dotnet-ef 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  …

C# Entity Framework SQL Server Database Entity Framework Core LINQ

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# Entity Framework Database Data Sync

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

C# .NET Core Tencent Cloud Live Streaming China

Get an ideal flight simulator free based on X-Plane 11 (Free plugins)

Video: https://www.youtube.com/watch?v=nRMQn8rdZ-4 Or(China edition):   https://www.bilibili.com/video/BV1Tg4y1z7UP Before reading this, you need to have X-Plane 11 installed. Buy X-Plane 11 here: https://www.x-plane.com/ Every time I install X-Plane 11, I gonna install many plugins to get better fly experience. And I will try to list all the plugins I use free which helps me to get perfect fly  …

X-Plane Flight simulation Flight Plugins Plane Configuration Free

Tips to get better performance for Entity Framework Core

Only get the data you need And the key reason for your slow query is it outputs too much data. Consider adding: Take(200), Skip() to take only the data you need or the current page requires. Use a pager to generate the report. This might helps a lot. Consider the following code (BAD Example) var myArticles = await _dbContext .Articles .Where(t => t.OwnerId == me.Id) .  …

Entity Framework SQL Performance Database

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

C# .NET Core Reflection

Use IIS or Azure App Service as a reverse proxy

IIS is a popular web server with awesome GUI management and high performance. And sometimes we just need IIS works as a reverse proxy. Last week, I was building Aiursoft.IO. Aiursoft.IO is a project which reverse-proxies https://probe.aiursoft.com to provide a shorter download URL. And we already have a Windows Server virtual machine and Azure App Service Plan. So we don't plan to buy an  …

IIS web.config Reverse Proxy Web Azure App Service Azure

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

ASP.NET Core C# class library View component

One command to pull all git repositories

Every time I start my computer, I gonna open bash and pull all my repositories. My folder structure looks like this: So how can I pull all my repos with only one command? First, create a new file: pull.sh under the folder WorkSpace Change the content to: find . -maxdepth 2 -mindepth 2 -type d ( ! -name . ) -exec bash -c "cd '{}' && pwd && git pull" ; And execute the  …

bash git