How To Create A .NET Core 3.0 Trimmed Self-Contained Single Executable App Using VS Code

In this tutorial, we are going to learn how to create a self-contained app using Asp.Net Core 3.0 using Visual Studio code.

Introduction 

 
In this way we can share our application without installing .NET SDK on the target machine. So, first-of-all, we will create an Asp.Net Core 3.0 app, then we will publish it as self-contained app and then we will see how to reduce the size of executable using PublishTrimmed flag which is a new feature of .NET Core 3.0 Preview 6.
 

Prerequisite of this tutorial

 
Before going to the next, you must have installed these below prerequisites.

How to create a .NET Core 3.0 trimmed self-contained single executable app using VS Code?

 
Let’s see how to create a .NET Core 3.0 trimmed self-contained single executable app using VS Code in step by step.
 
Step 1 - Create an Asp.Net Core 3.0 app using VS Code.
 
So, first-of-all, in this step we will create an Asp.Net Core app using Visual Studio code. So, open Visual Studio code, then open command terminal and then choose directory where you want to create your project and then run this below command.
 
dotnet new mvc
 
After running the above command, you will see a new “Asp.Net Core web App (Model-View-Controller)” project is created.
 
How To Create A .NET Core 3.0 Trimmed Self-Contained Single Executable App Using VS Code
 
Now, run this project by running this below command.
 
dotnet run
 
Now, you will see the output as you do see in the below screenshot.
 
How To Create A .NET Core 3.0 Trimmed Self-Contained Single Executable App Using VS Code
 
Step 2 - How to publish self-contained app?
 
Now, in this step, we will see how to publish the self-contained app. So, go to terminal and run this below command.
 
dotnet publish -r win10-x64 -c Release --self-contained
 
How To Create A .NET Core 3.0 Trimmed Self-Contained Single Executable App Using VS Code
 
So, the above command will build the app in release mode and will publish the self contained app. It will create a new folder with the name of publish which will have lots of files with the .exe file. So, now we can run this app on any other machine. We just need to copy the complete folder and paste in any other machine. Then finally, run the .exe file.
 
How To Create A .NET Core 3.0 Trimmed Self-Contained Single Executable App Using VS Code
 
So, we have a publish folder with lots of files and the size is also huge as you can see in the below screenshot. So, the question is, how can we reduce the files and the size? Let’s see in the next step.
 
Step 3 - How to Reduce the files in self-contained app?
 
In this step, we will see how to reduce the files in self-contained app. So, according to .NET Core 3.0 Preview 5, we can publish a single-file executable using PublishSingleFile flag. Let’s see how it works.
 
Now, run this below command.
 
dotnet publish -r win10-x64 -c Release /p:PublishSingleFile=true
 
Now, you will see the output as you do see in the below screenshot.
 
How To Create A .NET Core 3.0 Trimmed Self-Contained Single Executable App Using VS Code
 
So, as you see the above we have reduced our files. There is only one .exe file with web.config file. But, the problem of size is still the same as you see in the below screenshot.
 
How To Create A .NET Core 3.0 Trimmed Self-Contained Single Executable App Using VS Code
 
Let’s see how to reduce the size.
 
Step 4 - How to reduce the size of self-contained app?
 
In this step, we will see how to reduce the size of self-contained app. So, according to .NET Core 3.0 Preview 6, we can reduce the size of self-contained app using PublishTrimmed flag. Let’s see how it works.
 
Now, run this below command and publish self-contained app.
 
dotnet publish -r win10-x64 -c Release /p:PublishSingleFile=true /p:PublishTrimmed=true
 
Now, you will see, the above command will give you the output as you see in the below screenshot.
 
How To Create A .NET Core 3.0 Trimmed Self-Contained Single Executable App Using VS Code
 
So, it has reduced the size of the self-contained app.
 
Thank you for reading. Please keep visiting and sharing it within your community.