Final Replace: Dec 31, 2022
I’m going to point out you a cool new function in .NET Core 3.0.
Let’s say you need to create a easy, lean executable you’ll be able to construct and drop on to a server.
For an instance we’ll create a console app that opens a line of textual content, reads it and shows the output.
First, let’s create a brand new .NET Core app:
This may scaffold a brand new console utility.
Now run it:
It ought to look one thing like this:
I’m on a Mac right here, but it surely doesn’t matter so long as your growth field has the .NET Core CLI Put in.
This shows “Hiya World” on the console.
Now, lets create a file known as file.txt:
it is a file!
With some strains
no matter
Doesn’t matter what you set in right here, so long as it has some textual content in it.
Subsequent we’ll create a one thing that can learn these strains and show them. Take away the “Hiya World!” code and substitute it with this:
string[] strains = System.IO.File.ReadAllLines(@"take a look at.txt");
foreach (string line in strains)
{
Console.WriteLine("t" + line);
}
Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();
That is just about your primary cookie cutter code for:
- opening up a file
- studying it right into a string array
- loop by the array line by line
- print every line
- exit
Fairly easy stuff. Once I run it on my machine it appears to be like like this:
And that’s nice. However I’m on a Mac right here, what if I would like it to run on a Home windows Machine? Linux? No downside, that is .NET Core proper? We’ll simply publish it to a number of targets.
However what if .NET Core isn’t put in on the machine?
What if I simply need a easy executable I can run to learn this file and not using a pile of information or .Web core put in?
Publishing in .Web Core
Let’s again up just a little. .NET Core has had publishing profiles for a very long time. The thought behind “goal” publishing is among the largest promoting factors of the platform. Construct your app, then publish it for a selected goal, Home windows, OSX, or Linux.
You may publish it just a few alternative ways
- Framework Dependent Deployment – This implies depends on a shared model of .NET Core that’s put in on the Pc/Server.
- Self Contained Deployment – This doesn’t depend on .Web Core being put in on the server. All parts are included with the bundle (tons of information often).
- Framework Dependent Executables – That is similar to a framework dependent deployment, but it surely creates executables which can be platform particular, however require the .NET Core libraries.
Okay, so what’s this cool new function I’m going to point out?
Effectively, once you do a self contained deployment it’s cool since you don’t want the runtime put in, but it surely finally ends up wanting one thing like this:
That is the applying we simply constructed revealed as a Self Contained Deployment for Home windows. Yikes.
Let’s say you wished to share this file reader utility, and asking somebody to repeat all these information right into a folder to run one thing to learn a textual content file. It’s foolish.
New Characteristic: Self Contained Executables
So to construct that self contained executable I ran the next command:
dotnet publish -c launch -r win10-x64
This could look fairly acquainted to you for those who’ve achieved it earlier than. However .NET Core 3.0 has a cool new function:
dotnet publish -r win-x64 -c Launch /p:PublishSingleFile=true
Utilizing this flag, it would construct one thing just a little completely different:
That’s MUCH higher. It’s now a single exe and .pdb. If we take a look at the information, it’s not tremendous small:
However it contains the .NET Core runtime with it as properly. And right here’s one other cool function.
dotnet publish -r win-x64 -c Launch /p:PublishSingleFile=true /p:PublishTrimmed=true
So in our instance it doesn’t change the dimensions, however you probably have a big complicated utility with a variety of libraries, for those who simply publish it to a single file, it may well get HUGE. By including the PublishTrimmed flag it would solely extract the libraries it’s essential to run the applying.
So after we copy the information to a Home windows 10 machine, we now have a pleasant small bundle:
And we run it and it really works! With out .NET Core!
and if I modify my goal:
dotnet publish -r linux-x64 -c Launch /p:PublishSingleFile=true /p:PublishTrimmed=true
I can run it on a Linux server with out .NET Core simply as simply:
Simply keep in mind on a Linux machine you gained’t want the .NET Core runtime, however you have to the Conditions for .NET Core on Linux put in.
Conclusion
So it is a cool function of .NET Core 3.0. If you wish to construct trimmed down self contained executables for any of the platforms, you are able to do it simply with a few flags.
That is nice for these silly easy issues: console apps, knowledge readers, microservices, or no matter you need to construct and simply drop on a machine with no trouble. I believed it was a cool function to point out.
If you wish to grasp .NET Core, try the ASP.Web Core Path on Pluralsight. The programs go fairly in depth and are an effective way to ramp up.
Yell at me on Twitter with questions or feedback!
Are you able to beat my SkillIQ Rating in C#??
I feel you’ll be able to! Take the C# take a look at right here to see the place you stand!
GIPHY App Key not set. Please check settings