r/aspnetcore • • Dec 01 '22

400% faster, Rapid data insertion in Entity Framework Core 7

53 Upvotes

Because in the previous version, Entity Framework Core (EF Core) could not efficiently insert, modify and delete data in batches, so I developed Zack.EFCore.Batch, an open-source project, which was quite popular and obtained more than 400 stars.

Since .NET 7, EF Core has built-in support for the efficient batch updating and deletion of data in Entity Framework Core 7. See this document for details https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-7.0/whatsnew?WT.mc_id=DT-MVP-5004444#executeupdate-and-executedelete-bulk-updates So my open source project will no longer provide support for bulk updating and deletion of data in. NET 7. However, since Entity Framework Core still does not provide efficient bulk data insertion, I upgraded this open-source project to . NET 7, thus continuing to provide EF Core with the ability to efficiently bulk insert data.

Why did I develop this feature?

The AddRange() method can be used to batch insert data in Entity Framework Core. However, the data added by AddRange() is still inserted into the database by using INSERT statements one by one, which is inefficient. We know that SqlBulkCopy can quickly insert a large amount of data to SQLServer database, because SqlBulkCopy can pack multiple data into a packet and send it to SQLServer, so the insertion efficiency is very high. MySQL, PostgreSQL, and others have similar support.

Of course, using SqlBulkCopy to insert data directly requires the programmer to fill the data to the DataTable, perform column mapping, and handle ValueConverter and other issues, which is troublesome to use. Therefore, I encapsulated these capabilities to make it easier for EF Core developers to insert data in a model-oriented manner.

This library currently supports MS SQLServer, MySQL, and PostgreSQL databases.

Comparison of performance

I did a test of inserting 100,000 pieces of data with SQLServer database, and the insertion took about 21 seconds with AddRange(), compared to only about 5 seconds with my open-source project.

How to Use?

Older versions of the library also support it. NET 5, and 6 version, such as specific usage see https://github.com/yangzhongke/Zack.EFCore.Batch, the following statement is for . NET 7.

First, install Nuget package:

SQLServer: Install-Package Zack.EFCore.Batch.MSSQL_NET7

MySQL: Install-Package Zack.EFCore.Batch.MySQL.Pomelo_NET7

Postgresql: Install-Package Zack.EFCore.Batch.Npgsql_NET7

You can then use the extension method BulkInsert for DbContext provided by my project to do bulk data insertion, as follows:

List<Book> books = new List<Book>();
for (int i = 0; i < 100; i++)
{
	books.Add(new Book { AuthorName = "abc" + i, Price = new Random().NextDouble(), PubTime = DateTime.Now, Title = Guid.NewGuid().ToString() });
}
using (TestDbContext ctx = new TestDbContext())
{
	ctx.BulkInsert(books);
}

GitHub repository: https://github.com/yangzhongke/Zack.EFCore.Batch

I hope this library helps.


r/aspnetcore • • Nov 29 '22

Who says .NET doesn't have GC tuning?! changing one line of code made less memory consumption

51 Upvotes

It's common to see .Net developers tease: "Why are Java developers always learning about JVM tuning? That's because Java sucks! We don't need that at .NET!" Or is it? Today I will use a case to analyze.

Yesterday, a student asked me a question: He built a default ASP. Net Core Web API project, which is the default project template for WeatherForecast, and changed the default code for generating 5 pieces of data to generating 150,000 pieces of data. The code is as follows:

public IEnumerable<WeatherForecast> Get()
{
	return Enumerable.Range(1, 150000).Select(index => new WeatherForecast
	{
		Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
		TemperatureC = Random.Shared.Next(-20, 55),
		Summary = Summaries[Random.Shared.Next(Summaries.Length)]
	})
	.ToArray();
}

And then he used a stress test tool to test the Web API with 1000 concurrent requests and found that memory soared to 7GB and didn't drop back after the stress test. For a Web API project written in Python with the same stress test, he applied the same number of requests for the Web API written in Python, and found that the memory also soared, but after the stress test, the memory usage quickly fell back to the normal level.

He wondered, "Does such a simple program have a memory leak? Is .NET performance that bad?"

I "solved" his problem in four ways, and I will analyze the methods and principles of these ways in turn. Before I do that, let me briefly explain the basics of garbage collection (GC) :

When an object is created, it occupies memory. We must release the memory occupied by the object after it is no longer needed to prevent the program from becoming more and more memory occupied. In C, the programmer is required to use malloc() for memory allocation and free() for memory release. However, in modern programming languages such as C#, Java and Python, programmers rarely need to care about memory management. Programmers just need to create new objects as needed. Garbage Collector (GC) will help us release the objects we don't need.

Regarding GC, there are also problems such as "generation 0, generation 1". You can check the .NET official documentation for more information:

https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/?WT.mc_id=DT-MVP-5004444

Let's start with these "solutions."

Solution 1: remove ToArray()

How: The return value of Get() method is of IEnumerable<WeatherForecast>, and the Select() method returns the same type, so there was no need to convert it to an array using ToArray() , so we dropped the ToArray(). The code is as follows:

public IEnumerable<WeatherForecast> Get()
{
	return Enumerable.Range(1, 150000).Select(index => new WeatherForecast
	{
		Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
		TemperatureC = Random.Shared.Next(-20, 55),
		Summary = Summaries[Random.Shared.Next(Summaries.Length)]
	});
}

Run the same stress test again, and something amazing happens: the peak memory usage is less than 100MB.

Why:

IEnumerable and LINQ work in a "pipeline" way by default, In other words, a consumer who uses IEnumerable (in this case, a Json serializer) calls MoveNext() once for a single piece of data and then performs a Select() to create a new WeatherForecast object. In contrast, with ToArray(), 150,000 WeatherForecast objects are generated at once, and put into an array before the large array is returned.

Without ToArray(), objects are generated and consumed one by one. Therefore, objects are generated concurrently in a "slow flow", so there is no ToArray() operation of gradually accumulating 150,000 objects, so the concurrent memory consumption is smaller. At the same time, WeatherForecast objects are produced and consumed in "pipeline" mode, so when a WeatherForecast object is consumed, it is "ready" to be collected by GC. With ToArray(), array objects hold references to 150,000 WeatherForecast objects, so only if the array is marked "recyclable" can those 150,000 WeatherForecast objects be marked "recyclable". As a result, the chance to retrieve WeatherForecast objects is greatly delayed.

I don't know why Microsoft has given unnecessary ToArray() in the WeatherForecast Web API example project code. I will go to Microsoft to give feedback, and no one can stop me!

In conclusion: In order to "pipeline" Linq, use an IEnumerable instead of an array or List, and be careful of ToArray() or ToList() every time you use an IEnumerable.

The solution is the most perfect one, and the following solutions are just to help you understand GC more deeply.

Solution 2: change ‘class’ to ‘struct’

How: Keep the original ToArray(), but change the WeatherForecast from ‘class’ to ‘struct’ as follows:

public struct WeatherForecast
{
	public DateOnly Date { get; set; }
	public int TemperatureC { get; set; }
	public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
	public string? Summary { get; set; }
}

When the same stress test was run again, the peak memory footprint with the struct was only about half that with the class. Again, the memory footprint did not drop after the stress test.

Why: class objects contain more information than structs, and structs have a more compact memory structure, so structs containing the same members take up less memory than class objects. Therefore the peak memory footprint is reduced after changing the class to the struct.

You may ask "Are struct objects allocated on the stack, and are they released after used? Why didn't the memory footprint drop after the stress test? Isn't the struct's memory automatically released?". It should be noted that "struct objects are automatically released without GC" only occurs when struct objects are not referenced by reference type objects. Once a struct object is referenced by a reference type object, struct objects also need to be collected by GC. Because of the ToArray() operation in our code, 150,000 struct objects are referred to by an array, so they must be collected by GC.

Solution 3: Invoke GC manually

How: Since the memory consumption is high after the stress test because the GC is not executed in time, we can manually invoke GC after the stress test to invoke garbage collection forcefully.

Let's create a new Controller and then call GC.Collect() from the Action to force the GC. The code is as follows:

public class ValuesController : ControllerBase
{
	[HttpGet(Name = "RunGC")]
	public string RunGC()
	{
		GC.Collect();
		return "ok";
	}
}

We then performed the stress test, and after the stress test was complete, it was clear that the memory footprint did not drop. We then requested RunGC() a few more times, and we can see that the memory footprint fell back to about 100 MB.

Why: GC.Collect() forces garbage collection, so that WeatherForecast objects will be releasd. Why does GC.Collect() called multiple times before memory usage goes back to the original state? That's because memory collection is a CPU-consuming operation. To avoid affecting program performance, garbage collection does not recycle all unused objects at once.

It is noticeable that it is not good to call GC.Collect () manually, because GC will choose the appropriate time to perform memory collection, which may cause performance problems. If you need to manually collect GC.Collect() to reduce your program's memory footprint to your expectations, either your program needs to be optimized or your expectations of the program's memory footprint are wrong. What do I mean, "The expectation of a program's memory footprint is wrong"? please check out the following solution.

Solution 4: Change type of GC

How: Add the following configuration into the ASP.NET Core project file (*.csproj file):


<PropertyGroup>

<ServerGarbageCollection>false</ServerGarbageCollection>

</PropertyGroup>

The same stress test was run again, and the memory footprint quickly fell back to the original 100MB+.

Why: As we know, the programs we develop often fall into two categories: desktop applications (e.g., WinForms, WPF) and server-side applications (e.g., ASP.NET Core).

Desktop programs generally don't hog the memory and CPU resources of the entire operating system because there are many other programs running on the operating system, so desktop programs are conservative in their memory and CPU usage. For a desktop program, if it takes up too much memory, we think it's bad.

Desktop programs generally don't monopoly the memory and CPU resources of the entire operating system because there are many other programs running on the operating system, so desktop programs are conservative in their memory and CPU usage. For a desktop program, if it takes up too much memory, we think it's bad.

In contrast, server-side programs usually have the memory and CPU resources of the entire server (because a normal system will deploy the database server, web server, Redis server on to different computers), so the full use of memory and CPU can improve the performance of web applications. Therefore the Oracle database will try to take up most of the server's memory by default, which can be used to improve performance. If a web application underuses the memory, it may not have the best performance.

In contrast, there are two modes of.NET GC: Workstation and Server. The Workstation mode is for desktop applications with a more conservative memory footprint, while the Server mode is for server-side applications with a more aggressive memory footprint. We know that garbage collection is resource-intensive, and frequent GC can degrade performance for server-side applications, so in server mode, .NET tries to minimize the frequency and scope of GC as long as there is enough memory available. Desktop programs have a high tolerance for the performance impact of GC and a low tolerance for excessive memory footprint. Therefore, GC runs at higher frequencies in Workstation mode, thus keeping the program memory footprint low. In Server mode, if there is enough memory available, GC runs as little as possible and does not collect a large number of objects for a long time. Of course, there are many other differences between the two models, as detailed in Microsoft's documentation:

https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/workstation-server-gc?WT.mc_id=DT-MVP-5004444

ASP.NET Core programs are enabled with Server mode GC by default, so memory did not fall back after the stress test. After disabling Server mode via <ServerGarbageCollection>false</ServerGarbageCollection>, GC becomes Workstation mode and the program will recycle memory more aggressively. Of course, when you change a server-side program to Workstation mode, the performance of the program will suffer, so it is not recommended unless there is a good reason to do so, as idle memory is a waste to the server.

In addition to the GC type, a variety of complex GC tuning parameters, such as heap memory size and percentage, can be used in .NET just like in Java's JVM. Please read the Microsoft documentation for details

https://learn.microsoft.com/en-us/dotnet/core/runtime-config/garbage-collector?WT.mc_id=DT-MVP-5004444

Summary: Try to use LINQ's "pipelined" operation and avoid ToArray() or ToList() for data sources with large amounts of data; Avoid manual GC; Setting the right expectations for a program's memory footprint is not always better for server-side programs; Make good use of GC mode to meet the performance and memory usage of different programs; The performance of the program can be more personalized through GC tunning.


r/aspnetcore • • Feb 10 '23

Without Blazor WebAssembly, develop a web site that compiles and runs C# code on browser

27 Upvotes

In this article, I will share with you on how to " build a tool to compile and run C# code in the browser". The core technology is to write WebAssembly and Roslyn technology in C# without relying on the Blazor framework.

1. Why such a tool is needed?

For programming novices, the installation and configuration of the development environment is a headache, if we can let beginners do not do any installation and configuration, but directly open the browser and write, run code on the browser, then this will greatly reduce the threshold of learning programming novices.

There are already some websites that can enabled learners to write and run C# code online, these websites have the following two approaches:

Approach 1: The code is delivered from the front end to the back-end server, where it is compiled, run, and displayed to the front end. The disadvantage of this approach is that it cannot complete complex input/output, interface interaction, etc.

Approach 2: Writing WebAssembly using Mono technology. The downside is that the C# syntax is not followed up in a timely manner and some new C# syntax is not supported.

Therefore, developing a tool that compiles and runs C# code on the browser side and supports the latest C# syntax is important. To develop such a tool, WebAssembly is an unavoidable technology.

2. What is WebAssembly?

While traditional front-end development uses JavaScript, WebAssembly lets you write programs that run in the browser using other programming languages. Because WebAssembly is standard in modern browsers, no additional plug-in is required to run the WebAssembly program in the browser. Major programming languages such as Java, Go, Python, and others now all support compilation to WebAssembly.

3. Disadvantage of Blazor WebAssembly

The Blazor WebAssembly technology in.NET compiles C# code into WebAssembly to run on the browser side. But traditional Blazor WebAssembly is a very intrusive framework, meaning that the entire system must be developed using C# technology, rather than just one component using C# code and the rest using traditional JavaScript. Of course, using Microsoft.AspNetCore.Components.CustomElements, we can only leave a placeholder on the UI for Blazor WebAssembly, but this way is still very heavy, we can't implement a lightweight component like "just write a function in C#", that is, an non-intrusive, lightweight WebAssembly component in C# with low dependencies.

4. Without Blazor WebAssembly, write WebAssembly in C#

Starting with.NET 6, we can write a lightweight WebAssembly in C# that uses only the basic runtime environment provided by Blazor, rather than introducing the entire Blazor WebAssembly.

Now, I will demonstrate the use of this technique with a simple example of calculating the sum of two numbers in C#. Of course, this is just a simple demonstration, something that would never be done in C# in a real project. The following projects use.NET 7 for demonstration purposes. Other versions may be used slightly differently.

a) Create a .NET “class library” project and then install the following two components via Nuget: Microsoft.AspNetCore.Components.WebAssembly, Microsoft.AspNetCore.Components.WebAssembly.DevServer. Then change the attribute ‘Sdk’ of the *.csproj file to "Microsoft.NET.Sdk.BlazorWebAssembly".

The modified file looks like Code 1.

Code 1 *csproj file

<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="7.0.2" />
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="7.0.2" />
  </ItemGroup>
</Project>

b) Create a file Program.cs in the library project, as shown in Code 2.

Code 2 Program.cs

using Microsoft.JSInterop;

namespace Demo1
{
    public class Program
    {
        private static async Task Main(string[] args)
        {
        }

        [JSInvokable]
        public static int Add(int i,int j)
        {
            return i + j;
        }
    }
}

The body of Main method is currently empty, but cannot be omitted. The [JSInvokable] on the Add method means that the method can be called by JavaScript, which means that it’s a method exported by Web Assembly.

c) Build the generated project. Generated Web Assembly and related files are in the “_framework” folder in the “wwwroot” folder under the output folder.

d) Create a front-end project using any front-end technology you like. Instead of using any front-end framework, I'll write the front-end project in plain HTML+Javascript.

First, copy the _framework folder generated in the previous step into the root folder of the front-end project.

Then we write the index.html file shown in code 3.

Code 3 index.html

<html lang="en">
	<head>
		<meta charset="UTF-8" />
	</head>
	<body></body>	
	<script src="_framework/blazor.webassembly.js" autostart="false"></script>	
	<script>	
		window.onload = async function () {
			await Blazor.start();
			const r = await DotNet.invokeMethodAsync(
				'Demo1',//Assembly name
				'Add',//the method decorated with [JSInvokable]
				666,//some arguments
				333
			);
			alert(r);
		};	
	</script>
</html>

Let me explain the above code,<script src="\\_framework/blazor.webassembly.js" autostart="false"></script> is for importing related files. Blazor.start() is for start Blazor runtime; DotNet.invokeMethodAsync is for invoking a method of the WebAssembly, and the first argument is the name of the called assembly, the second argument is the name of the called method, and the next arguments are the values passed to the called method as arguments.

As can be seen, we are using WebAssembly as a component here, with no other special requirements for the page. So this component can be used in any front-end framework, and can also be compatible with other front-end libraries.

Finally, let’s run the front-end project. Because Blazor generates *.blat, *.dll etc files that may not be accepted by the Web server by default, please make sure to configure Mimetyps mappings ffor the following format on your web server: .pdb, .blat, .dat, .dll, .json, .wasm, .woff, .woff2.

The Web server I used for testing here is IIS, so you can create the Web.config file as shown in the root folder of the website, as shown in code 4. For developers using other Web servers, please refer to the manual of the Web server used to configure MimeType mappings.

Code 4 Web.config

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
	<staticContent>
	<remove fileExtension=".pdb" />
	<remove fileExtension=".blat" />
	<remove fileExtension=".dat" />
	<remove fileExtension=".dll" />
	<remove fileExtension=".json" />
	<remove fileExtension=".wasm" />
	<remove fileExtension=".woff" />
	<remove fileExtension=".woff2" />			
	<mimeMap fileExtension=".pdb" mimeType="application/octet-stream" />
	<mimeMap fileExtension=".blat" mimeType="application/octet-stream" />
	<mimeMap fileExtension=".dll" mimeType="application/octet-stream" />
	<mimeMap fileExtension=".dat" mimeType="application/octet-stream" />
	<mimeMap fileExtension=".json" mimeType="application/json" />
	<mimeMap fileExtension=".wasm" mimeType="application/wasm" />
	<mimeMap fileExtension=".woff" mimeType="application/font-woff" />
	<mimeMap fileExtension=".woff2" mimeType="application/font-woff" />			
	</staticContent>
</system.webServer>
</configuration>

e) Access the index.html in the Web server on rowser, if you see the pop-up window as shown in Figure 1, it means that Javascript has successfully called the Add method written in C#.

5. Scenarios of writing WebAssembly in C#

WebAssembly written in C# by default accounts for a large amount of traffic, around 30MB. We can use BlazorLazyLoad, enable Brotli algorithm and other ways to reduce traffic below 5MB. Please search online for details.

In my opinion, writing WebAssembly in C# includes, but is not limited to, the following scenarios.

Scenario 1: Reuse some. NET component or C# code. The existing .NET components or C# code can also be rewritten in Javascript, but this adds extra work. These components can be reused directly through WebAssembly. For example, I have used a PE file parsing Nuget package for back-end development, and I can continue to use the package directly in WebAssembly to process PE files on the front end.

Scenario 2: using some WebAssembly components. Because programs written in languages such as C/C++ can be ported to the WebAssembly version, many classic C/C++ developed software can continue to be used on the front end as well. For example, FFMpeg for audio and video processing already has a WebAssembly version, so you can call it in C# for audio and video processing. For example, the famous computer vision library OpenCV has also been ported to WebAssembly, so we can also use C# in the front end for image recognition, image processing and other image operations. WebAssembly is ideal for developing tools such as "online image processing, online audio and video, online games".

Scenario 3: Develop some complex front-end components. We know that Javascript often falls short when it comes to developing complex projects, and even Typescript isn't fundamentally better than Javascript. In contrast, C# and others are more suitable, so some very complex front-end components may be more appropriate to be writen in C# for WebAssembly.

In the scenarios above, you can develop only some of the components in C#, and the rest can continue to be developed in Javascript, so we can take advantage of each language.

6. Invoke methods of Javascript in C#

When writing WebAssembly, we might need to call methods of Javascript in C#. We can invoke asynchronous and synchronous methods in Javascript with IJSRuntime and IJSInProcessRuntime respectively.

Create a library project, named Demo2, and first configure the project as described in Section 4 above.

The code for index.html and Program.cs is shown in codes 5 and 6.

Code 5 index.html

<html lang="en">
<head>
    <meta charset="UTF-8" />
</head>
<body>
	<div id="divDialog" style="display:none">
		<div id="divMsg"></div>
		<input type="text" id="txtValue" />
		<button id="btnClose">close</button>
	</div>
	<ul id="ulMsgs">		
	</ul>
</body>
<script src="_framework/blazor.webassembly.js" autostart="false"></script>
<script>
	function showMessage(msg) {
        const divDialog = document.getElementById("divDialog");
		divDialog.style.display = "block";
		const divMsg = document.getElementById("divMsg");
		divMsg.innerHTML = msg;
		const txtValue = document.getElementById("txtValue");
        const btnClose = document.getElementById("btnClose");
		return new Promise(resolve => {			
			btnClose.onclick = function () {
				divDialog.style.display = "none";
                resolve(txtValue.value);
			};
		});
	}
	function appendMessage(msg) {
		const li = document.createElement("li");
		li.innerHTML = msg;
		const ulMsgs = document.getElementById("ulMsgs");
		ulMsgs.appendChild(li);
	}
	window.onload = async function () {
		await Blazor.start();
        await DotNet.invokeMethodAsync('Demo2','Count');
	};
</script>
</html>

Code 6 Program.cs

using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.JSInterop;

namespace Demo2;

public class Program
{
    private static IJSRuntime js;

    private static async Task Main(string[] args)
    {
        var builder = WebAssemblyHostBuilder.CreateDefault(args);
        var host = builder.Build();
        js = host.Services.GetRequiredService<IJSRuntime>();
        await host.RunAsync();
    }

    [JSInvokable]
    public static async Task Count()
    {
        string strCount = await js.InvokeAsync<string>("showMessage","Input Count");
        for(int i=0;i<int.Parse(strCount);i++)
        {
            ((IJSInProcessRuntime)js).InvokeVoid("appendMessage",i);
        }
        ((IJSInProcessRuntime)js).InvokeVoid("alert", "Done");
    }
}

The appendMessage() method defined in Index.html is a synchronous method for attaching a given message to “ul”; showMessage() is an asynchronous method that displays an HTML-simulated input dialog that closes when the user clicks the [Close] button and returns what the user entered. This operation involves the concept of promises in Javascript, if you're not familiar with this,please check out online resources.

In Program.cs, we get the IJSRuntime service used to invoke Javascript code in the Main method. IJSRuntime interface provides InvokeAsync and InvokeVoidAsync methods, which are used to asynchronously invoke JavaScript methods with and without a return value. If you want to call methods in Javascript synchronously, you need to cast IJSRuntime to IJSInProcessRuntime, and then call its InvokeVoid and Invoke methods. The Count() method labeled with [JSInvokable] is asynchronous, and asynchronous methods in C# are called in Javascript the same way.

7. Compile C# code at runtime: Roslyn

Roslyn is used to compile C# code at runtime, and Roslyn supports use in WebAssembly, so we use this component to compile C# code here. The usage of Roslyn is widely available on the Internet and I won't go into detail here.

The only caveat is that Roslyn builds concurrently by default, aas it speeds up the compilation. However, due to the limitations of the browser sandbox environment, WebAssembly does not support concurrent build, so if you use the default Roslyn compilation setting, “System.PlatformNotSupportedException: Cannot wait on monitors on this runtime” will be thrown when the compilation operation is performed. Therefore, please set concurrentBuild=false for CSharpCompilationOptions, as shown in Code 7.

Code 7 disable concurrentBuild

var compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, concurrentBuild: false);
var scriptCompilation = CSharpCompilation.CreateScriptCompilation(
"main.dll", syntaxTree, options: compilationOptions).AddReferences(references);

8. Replace the default implementation of the Console class

Due to the limitations of the browser sandbo, not all. NET classes are callable, or their functionality is limited. For example, when calling IO classes in WebAssembly, you can't read or write files on the user's disk at will, only because of the security restrictions of the browser sandbox environment. In WebAssembly, for example, you can call the HttpClient class to make Http requests, but again, the cross-domain access of the browser is limited. WebAssembly is powerful, but no matter how powerful it is, it doesn't break the limits of sandbox.

In this tool for compiling and running C# code online, I want the user to be able to use Console.WriteLine() and Console.ReadLine() to interact with the user on output and input. However, in Web Assembly, Console.Writeline () writes message in the console of developer tools, equivalent to executing console.log() in JavaScript; Console.ReadLine() is not available in Web Assembly. So I wrote a Console class with the same name and provided implementations of the WriteLine() and ReadLine() methods using the JavaScript alert() and prompt() functions, respectively. When compiling user-written code with Roslyn, I use the assembly of my custom Console class instead of System.Console.dll, so that the Console class in the user-written code calls my custom class.

9. Demo and Github repository

I have deployed the project online, and it can be accessed on https://block.youzack.com/editor.html, as shown in Figure 2.

Write C# code in the code editor, then click the [Run] button. If the code has errors, the interface will also display detailed compilation error information.

The Github repository of the project is: https://github.com/yangzhongke/WebCSC

10. Conclusion

Since.NET 6, it has been possible to move away from the traditional and intrusive Blazor WebAssembly framework and write lightweight, non-intrusive WebAssembly applications in C# that can be developed in conjunction with Javascript. Let the project in the development efficiency, engineering management and other aspects to achieve better results.

This article introduced you to developing a non-intrusive WebAssembly component in C# and shared an open-source project for writing, compiling, and running C# development in the browser.


r/aspnetcore • • Jun 25 '19

Roadmap to become an ASP.NET Core developer in 2019

Thumbnail
github.com
27 Upvotes

r/aspnetcore • • Mar 13 '21

Web API in 5 Hours (2021)

Thumbnail
youtu.be
26 Upvotes

r/aspnetcore • • Apr 21 '20

Clean Architecture with ASP.NET Core 3.0

Thumbnail
youtu.be
25 Upvotes

r/aspnetcore • • Mar 23 '23

ASP.NET Core Web API vs Minimal API, when to choose to use?

20 Upvotes

Good day everyone.

I want to create an api for our projects, I saw the minimal api, and I'm amazed from it, but the Web API is still around, now if I want to create an API for company which is used by multiple applications, which of these is the best to use, are there scenarios that I have to use Web API, instead of Minimal API?

Thanks, and regards,


r/aspnetcore • • Feb 22 '21

What are some good examples of production quality ASP.NET Core REST API?

18 Upvotes

I watched several courses on how to create Web API in ASP.NET Core 5. So I got over the basics and I then I started creating my own REST API for simple side-project, but I am encountering problems that I am not really sure how to solve. The courses I watched are covering rather simple scenarios like Todo-App or app for tracking students in classes. So when I approached more real world cases I am not sure how to proceed with implementation as I am missing some experience in REST best practices and design.

So I'd like to go deeper now and study some production grade REST API projects written in ASP.NET Core that are open source and can be found on GitHub. Can you recommend some that are good example of best practices?

Also, what are good examples of public REST API that I can study? Eg. maybe try to create client for them and thus discover how other people approached when designing the API.


r/aspnetcore • • Apr 09 '23

FullStackHero .NET 7 Web API is now released!🔥

16 Upvotes

fullstackhero's .NET Web API Boilerplate is a starting point for your next .NET 7 Clean Architecture Project that incorporates the most essential packages and features your projects will ever need including out-of-the-box Multi-Tenancy support. This project can save well over 200+ hours of development time for your team.

Watch the getting started video here: https://www.youtube.com/watch?v=a1mWRLQf9hY&ab_channel=MukeshMurugan

I have covered the following topics:

  1. Features of the Boilerplate - including multitenancy & dockerization.

  2. Clean Architecture

  3. Installing the template using FSH CLI tool.

  4. Creating your new solution using the CLI tool.

  5. Development Environment

  6. Setting up the Connection string & Starting up the API Server

  7. API Testing with ThunderClient

  8. Multitenacy explained

  9. Project Structure - Basics

  10. Docker & Docker Compose

  11. Terraform & AWS Deployment to ECS.

  12. Blazor WASM Boilerplate Support.

Do not forget to star the repository! Let me know in the comments section if you have any feedback and suggestions related to the .NET 7 Web API Boilerplate!


r/aspnetcore • • Dec 21 '20

Is there an ASP.NET Core equivalent to Django Admin (built-in admin panel)?

16 Upvotes

One of the things I like about Django is that it comes with a built-in admin panel for CRUD operations. Laravel also has such a feature, but I've never heard of such a thing for ASP.NET Core.

Is there a package you can install that gives you an out-of-the-box admin panel (examples here and here) for ASP.NET Core? This saves time having to create an admin area with input fields for all the different models.


r/aspnetcore • • Sep 17 '20

Why is identity and auth so ridiculously complicated in asp.net core?

16 Upvotes

I'm seeing things like the dotnet cli tool not fully creating ef data migrations depending on what's passed to --auth. One thing I noticed is that dotnet new --auth Individual won't completely scaffold the pages themselves, so I went looking into what it would take to do the scaffolding.

And what I find is that in order to actually scaffold anything I have to add 6+ dependencies to my project. Why in the world am I adding dependencies to my project just to generate source code?

And then it makes me wonder if the reason --auth MultiOrg doesn't generate any ef migrations is because I don't have the proper magical tools installed globally.

I mean, go look at this documentation and click on the CLI tabs: https://docs.microsoft.com/en-us/aspnet/core/security/authentication/scaffold-identity?view=aspnetcore-3.1&tabs=netcore-cli

How is that reasonable?

Furthermore, how do I do something as simple as replace EF with dapper for the DB access? Where's the documentation for this? I've gone and looked many times in the past and I've never been able to find complete documentation about the identity system except for prescriptive descriptions about how to get it to work with EF.

The few asp.net core applications I've written I've actually just completely rolled my own auth system so I didn't have to deal with the built in Identity. You read that right. I find it simpler to roll my own auth than to deal with all of that, only this time I figured I'd give it the good ole college try.

Can someone explain to me why Identity is this complicated? I'm actually considering walking away from the .net ecosystem entirely as a result of this. It's completely baffling to me because the old Membership stuff wasn't nearly as complicated, and I've done some pretty gnarly stuff with the provider model.


r/aspnetcore • • May 12 '20

Is this RoadMap outdated rn? Can someone post a 2020 version?

Post image
16 Upvotes

r/aspnetcore • • Jun 07 '21

Zack.EFCore.Batch, which can do batch deletion and update in EF Core, released three new features

15 Upvotes

Zack.EFCore.Batch is an open source library that supports efficient deletion and update of data in Entity Framework Core. As we know, Entity Framework Core does not support efficient deletion and update of data. All updates and operations are done one by one.For example, if you use the following statement to do " Delete all books that cost more than $10 " :

ctx.RemoveRange(ctx.Books.Where(b => b.Price > 33))

Then, Entity Framework Core will execute the following SQL statement:

Select * from books where price>33

Then, each records will be deleted using “delete from books where id=@id” on by one.

The batch update in EF core is the same. Therefore, it is relatively inefficient to delete and update a large amount of data in EF Core.

In order to achieve the "SQL data deletion, update", I developed an open source project Zack.EFCore.Batch, this open source project can help developers achieve the following batch deletion writing:

await ctx.DeleteRangeAsync<Book>(b => b.Price > n || b.AuthorName == "zack yang");

The C# code above will execute the following SQL statement to achieve the effect of "delete data in a single SQL statement" :

Delete FROM [T_Books] WHERE ([Price] > xx) OR ([AuthorName] =xxx)

This open source project uses EF Core to translate SQL statements, so any database supported by EF Core can be translated into the corresponding dialect SQL, such as the following batch update LINQ code:

await ctx.BatchUpdate<Book>()
    .Set(b => b.Price, b => b.Price + 3)
    .Set(b => b.Title, b => s)
    .Set(b => b.AuthorName,b=>b.Title.Substring(3,2)+b.AuthorName.ToUpper())
    .Set(b => b.PubTime, b => DateTime.Now)
    .Where(b => b.Id > n || b.AuthorName.StartsWith("Zack"))
.ExecuteAsync();

An UPDATE statement is translated under the SQL Server database as follows:

Update [T_Books] SET [Price] = [Price] + 3.0E0, [Title] = xx, [AuthorName] = COALESCE(SUBSTRING([Title], 3 + 1, 2), N'') + COALESCE(UPPER([AuthorName]), N''), [PubTime] = GETDATE()
WHERE ([Id] > xx) OR ([AuthorName] IS NOT NULL AND ([AuthorName] LIKE N'Zack%'))

This project has been upgraded to version 1.4.3, which supports SQLServer, MySQL, PostgreSQL, Oracle and SQLite database. Theoretically, any database supported by EFCore, can be supported by Zack.EFCore.Batch. If you have other databases to support, please contact me.

In addition to the existing features, the new version of Zack.EFCore.Batch released the following features.

Feature one: Data filtering based on entity relationship

Relationships between entities are supported in filter conditions. Such as:

ctx. DeleteRangeAsync<Article>(a=>a.Comments.Any(c=>c.Message.Contains(“History”))
||a.Author.BirthDay.Year<2000);

Feature two: Support bulk insertion

We can do efficient bulk insert in the following way

List<Book> books = new List<Book>();
for (int i = 0; i < 100; i++)
{
	books.Add(new Book { AuthorName = "abc" + i, Price = new Random().NextDouble(), PubTime = DateTime.Now, Title = Guid.NewGuid().ToString() });
}
using (TestDbContext ctx = new TestDbContext())
{
	ctx.BulkInsert(books);
}

The underlying BulkInsert() uses the BulkCopy mechanism of the individual databases for data inserts, so the inserts are very efficient. There are two disadvantages: automatic insertion of the associated data is not supported. For the associated object, please call Bulkinsert () for insertion; Because PostgreSQL'[s.Net](https://s.Net) Core Provider does not support Bulkcopy, so currently Zack.EFCore.Batch does not support PostgreSQL, I will try to find a solution later.

Feature three: Support for Take(), Skip() to limit the scope of deleting and updating

Both batch deletion and batch update support partial deletion and partial update through Take() and Skip(). The example code is as follows:

await ctx.Comments.Where(c => c.Article.Id == id).Skip(3)
.DeleteRangeAsync<Comment>(ctx);
await ctx.Comments.Where(c => c.Article.Id == id).Skip(3).Take(10)
.DeleteRangeAsync<Comment>(ctx);
await ctx.Comments.Where(c => c.Article.Id == id).Take(10)
.DeleteRangeAsync<Comment>(ctx);

await ctx.BatchUpdate<Comment>().Set(c => c.Message, c => c.Message + "abc")
	.Where(c => c.Article.Id == id)
	.Skip(3)
	.ExecuteAsync();

await ctx.BatchUpdate<Comment>().Set(c => c.Message, c => c.Message + "abc")
	.Where(c => c.Article.Id == id)
	.Skip(3)
	.Take(10)
	.ExecuteAsync();
await ctx.BatchUpdate<Comment>().Set(c => c.Message, c => c.Message + "abc")
   .Where(c => c.Article.Id == id)
   .Take(10)
   .ExecuteAsync();

For details, please visit the open-source project:

https://github.com/yangzhongke/Zack.EFCore.Batch

NuGet:https://www.nuget.org/packages/Zack.EFCore.Batch/


r/aspnetcore • • Apr 30 '21

Aspnetcore keycloak guide

15 Upvotes

I recently had the joy of figuring out how to do authorization with Keycloak in an aspnetcore project, running in a Linux container.

I finally got around to creating a guide. Hope it helps others with the same struggles 😉

https://github.com/tuxiem/AspNetCore-keycloak


r/aspnetcore • • Jan 22 '21

Authenticating MVC using Google (mostly command line, super quick, and..easy)

Thumbnail
youtu.be
15 Upvotes

r/aspnetcore • • Feb 01 '22

Implement JWT Refresh Token Authentication in ASP.NET Core 5 [Detail Guide]

Thumbnail
codepedia.info
13 Upvotes

r/aspnetcore • • Aug 25 '19

ASP.NET Core integration for free HTTPS certificates from LetsEncrypt

Thumbnail
github.com
13 Upvotes

r/aspnetcore • • Oct 02 '24

Orchard Core 2.0 has been released

12 Upvotes

Hi everyone! I would like to notify the community that Orchard Core 2.0 has been released!

Orchard Core is an open-source, modular, multi-tenant, and community-driven application framework built with ASP.NET Core, and a content management system (CMS) built on top of that framework. Its vision is to create shared components for building ASP.NET applications, and specific applications that leverage these components to meet the needs of end-users, content administrators, and developers.

The community recently released the second major version of Orchard Core with several new features and additions! This release is packed with performance improvements, bug fixes, and a variety of enhancements that make it our best version yet. Whether you're building a new project or maintaining an existing one, Orchard Core 2.0 brings stability and power to your fingertips.

In this release, there's a new "Azure AI Search" module, designed to empower you in the administration of Azure AI Search indices. When enabled with the "Search" module, it facilitates frontend full-text search capabilities through Azure AI Search.

The Microsoft Azure Media and Amazon S3 Media modules have new features for replacing the default PhysicalFileSystemCache of ImageSharp that stores resized images in the local App_Data folder. Instead, you can now use Azure Blob Storage with the Azure Media ImageSharp Image Cache feature (that utilizes AzureBlobStorageImageCache), and AWS S3 with the Amazon Media ImageSharp Image Cache feature (that utilizes AWSS3StorageCache). Depending on your use case, this can provide various advantages. And we have a lot more to cover! Here are all the details of the release: https://docs.orchardcore.net/en/latest/releases/2.0.0/.

Orchard Core is available as a NuGet package which you can easily add to your ASP.NET Core solution. Here’s how you can get started with Orchard Core: https://docs.orchardcore.net/en/latest/docs/getting-started.


r/aspnetcore • • Jan 22 '23

Visual Studio Endpoints Explorer

12 Upvotes

Check it out.
Microsoft Visual Studio now has Endpoints Explorer feature to show you all the endpoints available in your ASP.NET Core project.

This is a preview feature that you can enable by simply searching on the top search bar for Endpoints Explorer - super simple and easy to use.

This feature is available in Visual Studio 2022 Preview 3.0

#visualstudio #dotnet #csharp #endpoints #apis


r/aspnetcore • • Jul 30 '22

Force HTTPS in ASP.NET Core Applications

Thumbnail
auth0.com
12 Upvotes

r/aspnetcore • • Nov 03 '21

Role-Based Authorization for ASP.NET Web APIs

Thumbnail
auth0.com
12 Upvotes

r/aspnetcore • • Oct 18 '21

ASP.NET Core Identity - Authentication & Authorization in 2 Hours

Thumbnail
youtu.be
12 Upvotes

r/aspnetcore • • Jul 08 '21

.NET Ketchup

Thumbnail
dotnetketchup.com
12 Upvotes

r/aspnetcore • • Apr 09 '21

ASP.NET Core updates in .NET 6 Preview 3

Thumbnail
devblogs.microsoft.com
12 Upvotes