<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ramesh Kanjinghat</title>
    <description>The latest articles on DEV Community by Ramesh Kanjinghat (@rameshkanjinghat).</description>
    <link>https://dev.to/rameshkanjinghat</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F298796%2Fbb32dd8d-d7b1-4cc7-9e66-3baa9f7bb7b1.jpeg</url>
      <title>DEV Community: Ramesh Kanjinghat</title>
      <link>https://dev.to/rameshkanjinghat</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rameshkanjinghat"/>
    <language>en</language>
    <item>
      <title>Dotnet Warp to further optimize .Net self-contained single executable file.</title>
      <dc:creator>Ramesh Kanjinghat</dc:creator>
      <pubDate>Thu, 02 Jul 2020 18:13:27 +0000</pubDate>
      <link>https://dev.to/rameshkanjinghat/dotnet-warp-to-further-optimize-net-self-contained-single-executable-file-41g7</link>
      <guid>https://dev.to/rameshkanjinghat/dotnet-warp-to-further-optimize-net-self-contained-single-executable-file-41g7</guid>
      <description>&lt;p&gt;.NET core 3.0 and later versions enable us to deploy executables as self-contained. This means we can copy the publish folder to any machine and can run it even that machine doesn't have .NET core runtime installed. &lt;/p&gt;

&lt;p&gt;Please check &lt;a href="https://docs.microsoft.com/en-us/dotnet/core/deploying/#:~:text=Publishing%20your%20app%20as%20self,NET%20Core%20runtime%20installed.&amp;amp;text=runtime%2Ddependent%20executable%20for%20the%20current%20platform"&gt;Microsoft article&lt;/a&gt; for more details.&lt;/p&gt;

&lt;p&gt;The self-contained deployment significantly increases the overall size of the publish folder because it carries the whole .NET core runtime along with it. Second thing you will notice with self-contained deployment is the sheer number of files you will have in your publish folder.&lt;/p&gt;

&lt;p&gt;Fortunately, .NET tools team provides 2 File Publish Options that let you produce much trimmed and single file version of your application.&lt;/p&gt;

&lt;p&gt;The options are  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Produce single file&lt;/li&gt;
&lt;li&gt;Trim unused files&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To keep this a article to the scope, I won't be delving much into .NET Core deployment models. You can check my blog, &lt;a href="https://blogs.dhrutara.com/blogs/dotnet-core-deployment-models/"&gt;Dotnet Core 3.0 deployment models&lt;/a&gt;, if you want to learn more about .NET core deployment models.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/dgiagio/warp"&gt;Warp&lt;/a&gt; lets you produce a trimmed version of your self-contained single executable file. Warp is written in Rust and is supported on Linux, Windows and macOS.&lt;/p&gt;

&lt;h2&gt;
  
  
  Dotnet-warp
&lt;/h2&gt;

&lt;p&gt;To make it further easier for .NET developers, Hubert Rybak created a wrapper around wrap called &lt;a href="https://github.com/Hubert-Rybak/dotnet-warp/graphs/contributors"&gt;dotnet-warp&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I have tested &lt;strong&gt;dotnet-warp&lt;/strong&gt; on few of my internal projects and it definitely produces trimmer executable than the built-in one.&lt;/p&gt;

&lt;p&gt;In the below image you can see that the executable produced by dotnet-warp is 5MB lighter than built-in produced executable. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In this case it might not be much of a gain, but I have noticed that as the dependencies grow the difference tend to increase. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--e7MtF3Wq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/am190cj2mkk7qkwtl1ch.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--e7MtF3Wq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/am190cj2mkk7qkwtl1ch.jpg" alt="dotnet-warp vs .Net core built-in" width="880" height="376"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;dotnet-warp is a dotnet tool so, we can install and use dotnet-warp as any other dotnet tools.&lt;/p&gt;

&lt;h4&gt;
  
  
  To install dotnet-warp run below command
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dotnet tool install --global dotnet-warp --version 1.1.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above command installs the tool globally. Globally doesn’t mean at machine level instead it is at user level. Once installed globally the tool can be executed by that specific user without giving the whole exe path. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;To install it locally remove the flag –global&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Lets a take a simple application to
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Let’s call it Dhrutara.DotnetWarp.&lt;/li&gt;
&lt;li&gt;I am using Visual Studio 2019&lt;/li&gt;
&lt;li&gt;Runtime is dotnet core 3.1&lt;/li&gt;
&lt;li&gt;Added NewtonSoft.Json as dependency but I am not going to use it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;*&lt;em&gt;Newtonsoft.Json will be removed by warp-it because it is not used. *&lt;/em&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Use dotnet-warp
&lt;/h4&gt;

&lt;p&gt;The only required argument for dotnet-warp is the project file. This could be either relative or absolute. Other arguments are optional.&lt;/p&gt;

&lt;p&gt;So, let's begin with the only required argument.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Open either windows PowerShell or command prompt.&lt;/li&gt;
&lt;li&gt;Navigate to the project folder. &lt;/li&gt;
&lt;li&gt;Run below command.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dotnet-warp ".\Dhrutara.DotnetWarp.csproj"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZMcgrAuN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7m1gf3p68xobdr2vvi0j.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZMcgrAuN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7m1gf3p68xobdr2vvi0j.jpg" alt="Screen shot of the project folder. dotnet-warp with only the required argument and --output argument" width="880" height="408"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this case the executable is created in the same folder where the project file resides. That is because dotnet-warp creates the executable in the same location where the command is ran from.&lt;/p&gt;

&lt;p&gt;If you want to deploy it to a different location, then you can pass the target folder using “--output” option.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dotnet-warp ".\Dhrutara.DotnetWarp.csproj" --output ".\publish\Dhrutara.DotnetWarp.exe"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aOetBC2A--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/4q4ddz1hpbqphii5pixt.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aOetBC2A--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/4q4ddz1hpbqphii5pixt.jpg" alt="Screen shot of the project folder with dotnet-warp with required argument and --output argument" width="880" height="429"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;With --output option it is mandatory to mention the executable file full name, including extension. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;dotnet-warp internally uses the default dotnet core compiler and hence it also allows us to pass additional arguments to the compiler. The format to pass compiler options is&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-p:&amp;lt;argument name&amp;gt;=&amp;lt;argument value&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By default, dotnet-warp compiles in release mode. let us assume we want to compile Dhrutara.DotnetWarp in debug mode and also want to mention the version of the executable as 1.2.1. Below command can be used to achieve this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dotnet-warp ".\Dhrutara.DotnetWarp.csproj" --output ".\publish\Dhrutara.DotnetWarp.exe" -p:configuration="debug" -p:Version="1.2.1"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Since dotnet-warp is a dotnet tool we can use it in our azure CI/CD pipelines. Please check my blog &lt;a href="https://blogs.dhrutara.com/blogs/warp-it/,"&gt;warp-it&lt;/a&gt; to know how I used it in my projects.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;At the time of writing this article dotnet-warp produces lighter version of .NET core 3.0+ single executable files. .NET core team is working hard, and I hope very soon the built-in tool can do better job than dotnet-warp.&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>singleexe</category>
      <category>optimization</category>
      <category>warp</category>
    </item>
  </channel>
</rss>
