The .NET Core team really prides themselves on performance. However, it's not immediately obvious (as with all systems) if you just do Hello World as a develop. Just today I was doing a Ruby on Rails app in Development Mode with mruby - but that's not what you'd go to production with.
Let's look at a great question I got today on Twitter.
@shanselman @davidfowl Is it normal for dotnet run on the latest .net core 2.0 preview 2 bits to take 4 seconds to run? pic.twitter.com/wvD2aqtfi0
— Jerome Terry (@jeromeleoterry) June 28, 2017
Dotnet Run - Builds and Runs Source Code in Development
That's a great question. If you install .NET Core 2.0 Preview - this person is on a Mac, but you can use Linux or Windows as well - then do just this:
$ dotnet new console
$ dotnet run
It'll be about 3-4 seconds. dotnet is the SDK and dotnet run will build and run your source code. Here's a short bit from the docs:
The
dotnet run
command provides a convenient option to run your application from the source code with one command. It's useful for fast iterative development from the command line. The command depends on thedotnet build
command to build the code. Any requirements for the build, such as that the project must be restored first, apply todotnet run
as well.
While this is super convenient, it's not totally obvious that dotnet run isn't something you'd go to production with (especially Hello World Production, which is quite demanding! ;) ).
Dotnet Publish then Dotnet YOUR.DLL for Production
Instead, do a dotnet publish, note the compiled DLL created, then run "dotnet tst.dll."
For example:
C:\Users\scott\Desktop\tst> dotnet publish
Microsoft (R) Build Engine version 15.3 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.
tst -> C:\Users\scott\Desktop\tst\bin\Debug\netcoreapp2.0\tst.dll
tst -> C:\Users\scott\Desktop\tst\bin\Debug\netcoreapp2.0\publish\
C:\Users\scott\Desktop\tst> dotnet run .\bin\Debug\netcoreapp2.0\tst.dll
Hello World!
On my machine, dotnet run is 2.7s, but dotnet tst.dll is 0.04s.
Dotnet publish --self-contained
I could then publish a complete self-contained app - I'm using Windows, so I'll publish for Windows but you could even build on a Windows machine but target a Mac runtime, etc and that will make a \publish folder.
C:\Users\scott\Desktop\tst> dotnet publish --self-contained -r win10-x64
Microsoft (R) Build Engine version 15.3 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.
tst -> C:\Users\scott\Desktop\tst\bin\Debug\netcoreapp2.0\win10-x64\tst.dll
tst -> C:\Users\scott\Desktop\tst\bin\Debug\netcoreapp2.0\win10-x64\publish\
C:\Users\scott\Desktop\tst> .\bin\Debug\netcoreapp2.0\win10-x64\publish\tst.exe
Hello World!
Note in this case I have a "Self-Contained" app, so all of .NET Core is in that folder and below. Here I run tst.exe, not dotnet.exe because now I'm an end-user.
I hope this helps clear things up.
Sponsor: Check out JetBrains Rider: a new cross-platform .NET IDE. Edit, refactor, test, build and debug ASP.NET, .NET Framework, .NET Core, or Unity applications. Learn more and get access to early builds!
© 2017 Scott Hanselman. All rights reserved.