From 89d4f3303fdbda993f33cb4bd82c47d8a20b1a22 Mon Sep 17 00:00:00 2001 From: Pete Chown Date: Tue, 29 Nov 2022 23:51:53 +0000 Subject: [PATCH] Update to .NET 7. --- README.md | 15 ++++++++++++--- StandaloneKestrel.csproj | 3 ++- time-startup | 2 +- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 5656027..6981bd6 100644 --- a/README.md +++ b/README.md @@ -5,13 +5,22 @@ This project demonstrates how to run Kestrel without ASP.NET. It creates a web It also demonstrates how to add the websocket middleware. If you connect to ws://localhost:8080 (with wscat, for example) it will again respond with "hello world". This time it will be sent as a textual websocket message. -I wrote this code because I was curious to see if it could be done, but it may be interesting for someone. It uses significantly less memory than ASP.NET. Startup time is a bit lower too, about 180ms on my system. On a Linux system, you can measure it like this: +Memory consumption is slightly better than ASP.NET, and startup time is a bit lower too, about 180ms on my system. On a Linux system, you can measure the startup time like this: ``` dotnet publish -c Release time ./time-startup ``` ---- +.NET 7 +====== -This project was originally developed for .NET Core 2.1, but has now been updated for .NET 6. The update allowed one of the classes to be removed, simplifying the project significantly. Unfortunately, the Kestrel package is no longer published on its own, so it now has to depend on `Microsoft.NET.Sdk.Web` rather than `Microsoft.NET.Sdk`. +I've been maintaining this package on and off since .NET Core 2.1. For most of this time, it has just been an interesting hack, but now it has a potential real-world use. .NET 7 supports [AOT compilation to a single executable](https://learn.microsoft.com/en-us/dotnet/core/deploying/native-aot/), but this is only for simple console applications, not for ASP.NET. Standalone Kestrel, though, works in this mode. You can build it like this: + +``` +dotnet publish -r linux-x64 -c Release +``` + +(This has only been tested on Linux. If you are not on Linux, you could try building an executable, but you will need to substitute a different runtime identifier in the above command.) + +You will see a few warnings during compilation, but as far as I can tell, the resulting executable works well. The executable is about 46M with debug information, 15M stripped. After the process has been invoked (using both HTTP GET and websockets) the resident memory is about 22M, far better than even the simplest ASP.NET application, and similar to Go. diff --git a/StandaloneKestrel.csproj b/StandaloneKestrel.csproj index 6d42a97..7fc3378 100644 --- a/StandaloneKestrel.csproj +++ b/StandaloneKestrel.csproj @@ -1,9 +1,10 @@ - net6.0 + net7.0 enable enable + true \ No newline at end of file diff --git a/time-startup b/time-startup index eb1bac4..2b077f8 100755 --- a/time-startup +++ b/time-startup @@ -1,5 +1,5 @@ #!/bin/sh trap "trap exit TERM; trap EXIT; pkill --pgroup 0" INT TERM EXIT -./bin/Release/net6.0/StandaloneKestrel & +./bin/Release/net7.0/linux-x64/StandaloneKestrel & while ! curl -s http://localhost:8080; do true; done