Update to .NET 7.

This commit is contained in:
Pete Chown
2022-11-29 23:51:53 +00:00
parent a2d9b52c99
commit 89d4f3303f
3 changed files with 15 additions and 5 deletions

View File

@@ -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. 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 dotnet publish -c Release
time ./time-startup 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.

View File

@@ -1,9 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk.Web"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<PublishAot>true</PublishAot>
</PropertyGroup> </PropertyGroup>
</Project> </Project>

View File

@@ -1,5 +1,5 @@
#!/bin/sh #!/bin/sh
trap "trap exit TERM; trap EXIT; pkill --pgroup 0" INT TERM EXIT 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 while ! curl -s http://localhost:8080; do true; done