Microsoft FTE.

All Posts


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

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

C# .NET Core

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

C# Entity Framework SQL Server Soft deletion

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 …

ASP.NET Core C# Entity Framework Multi-tenant

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 …

ASP.NET Core C# Entity Framework SQL Server

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 …

ASP.NET Core .NET Core IP HTTP

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 …

C# .NET Core Async

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 …

ASP.NET Core C# .NET Core Entity Framework SQL Server

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 …

C# class library .NET Core