How to reduce the size of published files of ASP.NET Core project libraries
As we know ASP.NET Core project needs to published to run in production and we have different types of publishing models, but with the Self-Contained Deployment model, we can reduce the size of libraries with .NET core 3.0 SDK. Many times our project do not need all the things that come with default publishing, so with "PublishTrimmed" options, we can reduce size which just removes unused assemblies in our project.
This can be done either with our Project property or with the setup of the publishing profile.
The option for the project property is like,
<PropertyGroup> <PublishTrimmed>true</PublishTrimmed> </PropertyGroup>
Note:- After changing this setting our project need clean build to get the effect of these type of settings.
Similar to this there are some other properties, which helps for some great works the list is like,
<PropertyGroup> <!--disable Quick JIT is enabled and creates fully optimized code --> <TieredCompilationQuickJit>false</TieredCompilationQuickJit> <!--disable TC completely, use this MSBuild property--> <TieredCompilation>false</TieredCompilation> <!--Ready To Run--> <PublishReadyToRun>true</PublishReadyToRun> </PropertyGroup>
Max of the above can be done with publishing wizard too in Visual Studio 2019, The wizard options are like,
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <WebPublishMethod>FileSystem</WebPublishMethod> <PublishProvider>FileSystem</PublishProvider> <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration> <LastUsedPlatform>Any CPU</LastUsedPlatform> <SiteUrlToLaunchAfterPublish /> <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish> <ExcludeApp_Data>False</ExcludeApp_Data> <TargetFramework>netcoreapp3.1</TargetFramework> <RuntimeIdentifier>win-x64</RuntimeIdentifier> <PublishSingleFile>True</PublishSingleFile> <PublishTrimmed>True</PublishTrimmed> <PublishReadyToRun>True</PublishReadyToRun> <ProjectGuid>edc08c5c-79ed-45cc-a148-bad46d756508</ProjectGuid> <SelfContained>true</SelfContained> <publishUrl>D:\BlazorDemo\TrimingApp\PblishFiles</publishUrl> <DeleteExistingFiles>True</DeleteExistingFiles> </PropertyGroup> </Project>
and the options looks like the image shown bellow,
Note:- These options are avilable for Self-Contained Deployment model only
Hope this helps someone who do want some tunning in the productions.