Optimising the ASP.NET Core release bundle - c#

I have published a .net core 2.0 api bundle with
dotnet publish -c release
The release folder includes a bunch of DLL's that I didn't think would be required for running the application, ie, code generation packages etc.
Is there a way to optimise the release package so that these dll's aren't included?
Microsoft.CodeAnalysis.CSharp.Workspaces.dll
Microsoft.CodeAnalysis.Workspaces.dll
Microsoft.EntityFrameworkCore.Relational.Design.dll
Microsoft.EntityFrameworkCore.SqlServer.Design.dll
Microsoft.VisualStudio.Web.CodeGeneration.Contracts.dll
Microsoft.VisualStudio.Web.CodeGeneration.Core.dll
Microsoft.VisualStudio.Web.CodeGeneration.EntityFrameworkCore.dll
Microsoft.VisualStudio.Web.CodeGeneration.Templating.dll
Microsoft.VisualStudio.Web.CodeGeneration.Utils.dll
Microsoft.VisualStudio.Web.CodeGeneration.dll
Microsoft.VisualStudio.Web.CodeGenerators.Mvc.dll
NuGet.Frameworks.dll
System.Composition.AttributedModel.dll
System.Composition.Convention.dll
System.Composition.Hosting.dll
System.Composition.Runtime.dll
System.Composition.TypedParts.dll
dotnet-aspnet-codegenerator-design.dll
My csproj file:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Folder Include="Models\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.1" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.1" />
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.1" />
<DotNetCliToolReference Include="Microsoft.DotNet.Watcher.Tools" Version="2.0.0" />
</ItemGroup>
</Project>

Yes. Install the Microsoft.Packaging.Tools.Trimming NuGet package. Then add this to your .csproj file:
<PropertyGroup>
<TrimUnusedDependencies>true</TrimUnusedDependencies>
</PropertyGroup>
Or use the /p:TrimUnusedDependencies=true flag from the command line.
dotnet build /p:TrimUnusedDependencies=true
dotnet publish /p:TrimUnusedDependencies=true
msbuild /p:TrimUnusedDependencies=true
msbuild /t:Publish /p:TrimUnusedDependencies=true
See Dependency Trimming documentation.

Related

Entity Framework, Could not load file or assembly 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=3.1.0.0

I'm trying to build an API .net core and hosted in Azure App Service. The setup consists of a couple of projects but 2 have Entity Framework. 1. DataAccess proj there I have the EF context, migrations, etc. and 2 my web app context is in the DI
My problem:
Application '/LM/W3SVC/..../ROOT' with physical root 'D:\home\site\wwwroot\' hit unexpected managed exception, exception code = '0xe0434352'. First 30KB characters of captured stdout and stderr logs:
Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=3.1.0.0, Culture=neutral, PublicKeyToken=.....'. The system cannot find the file specified.
File name: 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=3.1.0.0, Culture=neutral, PublicKeyToken=.....'
at Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder(String[] args)
at Name.Name.Program.CreateHostBuilder(String[] args)
I looked at all the things on the internet and I cannot figure it out.
I was adding Microsoft.Extensions.DependencyInjection.Abstractions 3.1.0 removing some dll, added AutoGenerateBindingRedirects, GenerateBindingRedirectsOutputType in one PropertyGroup seperate and all in vain. The funny part is that locally it works but when I deploy it to azure it doesn't.
1 DataAccess csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<PropertyGroup>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Name.Model\Name.Model.csproj" />
</ItemGroup>
</Project>
2 web app csproj
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
</PropertyGroup>
<PropertyGroup>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Azure.Storage.Files.Shares" Version="12.9.0" />
<PackageReference Include="LazyCache.AspNetCore" Version="2.4.0" />
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.20.0" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.20.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.23" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="6.3.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.3.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.3.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Name.BusinessLogic\Name.BusinessLogic.csproj" />
<ProjectReference Include="..\Name.DataAccessLayer\Name.DataAccessLayer.csproj" />
</ItemGroup>
</Project>

Microsoft.AspNetCore.Razor.Design.CodeGeneration.targets(161, 5): Assembly location for Razor SDK Tasks was not specified

Microsoft.AspNetCore.Razor.Design.CodeGeneration.targets(161, 5): Assembly location for Razor SDK Tasks was not specified.
The most likely cause is an older incompatible version of Microsoft.NET.Sdk.Razor,
or Microsoft.NET.Sdk.Web used by this project.
Please target a newer version of the .NET Core SDK.
I have the below package reference
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback>
<PreserveCompilationContext>true</PreserveCompilationContext>
<AssemblyName>LegalRegTech.Web.Public</AssemblyName>
<OutputType>Exe</OutputType>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<TypeScriptToolsVersion>2.8</TypeScriptToolsVersion>
<TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
<LangVersion>7.2</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="2.2.1" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.2.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.1" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup>
</Project>
By adding Microsoft.NET.Sdk.Razor still not able to resolve the issue. Tried the solution from this Assembly location for Razor SDK Tasks was not specified but still not able to solve the issue
It worked on the build server, after added this Nuget package:
Microsoft.NET.Sdk.Razor
Since I added the Net.SDK.Razor package, it builds successfully.
I had also added Microsoft.AspNetCore.Razor.Design, but only Microsoft.NET.Sdk.Razor is actually needed.
resource

Error NU1202 Microsoft.EntityFrameworkCore.SqlServer 5.0.0 package is not compatible with net50

Can anyone help me with this error?
Error NU1202 The Microsoft.EntityFrameworkCore.SqlServer 5.0.0 package is not compatible with net50 (.NETFramework, Version = v5.0). The Microsoft.EntityFrameworkCore.SqlServer 5.0.0 package supports: netstandard2.1 (.NETStandard, Version = v2.1)
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<RootNamespace>Microsoft.eShopWeb.Infrastructure</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Ardalis.EFCore.Extensions" Version="1.1.0" />
<PackageReference Include="Ardalis.Specification" Version="4.1.0" />
<PackageReference Include="Ardalis.Specification.EntityFrameworkCore" Version="4.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.0" PrivateAssets="All" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.8.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ApplicationCore\ApplicationCore.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Data\Migrations\" />
</ItemGroup>
To resolve this, I have downloaded the required SDK package with the link, .net framework developer pack download-link, and installed them. Restart the server, then the build is successful.
Check your dotnet version with dotnet --info

warning NETSDK1080: A PackageReference to Microsoft.AspNetCore.App is not necessary when targeting .NET Core 3.0 or higher

How do I fix the nasty warning I'm getting when running .NET Core tests from a command line via dotnet test?
The dotnet --version returns back 3.1.101.
$ dotnet test
watch : Started
C:\Program Files\dotnet\sdk\3.1.101\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.DefaultItems.targets(151,5):
warning NETSDK1080: A PackageReference to Microsoft.AspNetCore.App is not necessary when targeting
.NET Core 3.0 or higher. If Microsoft.NET.Sdk.Web is used, the shared framework will be referenced automatically.
Otherwise, the PackageReference should be replaced with a FrameworkReference.
[C:\github\demo\Demo\SmartHome.API\SmartHome.API.csproj]
C:\Program Files\dotnet\sdk\3.1.101\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.DefaultItems.targets(151,5):
warning NETSDK1080: A PackageReference to Microsoft.AspNetCore.App is not necessary when targeting
.NET Core 3.0 or higher. If Microsoft.NET.Sdk.Web is used, the shared framework will be referenced automatically.
Otherwise, the PackageReference should be replaced with a FrameworkReference.
[C:\github\demo\Demo\SmartHome.API\SmartHome.API.csproj]
Test run for C:\github\demo\Demo\SmartHome.API.Test\bin\Debug\netcoreapp3.1\SmartHome.API.Test.dll(.NETCoreApp,Version=v3.1)
Microsoft (R) Test Execution Command Line Tool Version 16.3.0
Copyright (c) Microsoft Corporation. All rights reserved.
Starting test execution, please wait...
A total of 1 test files matched the specified pattern.
Here's what my SmartHome.API.Test.csproj looks like.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="MongoDB.Driver" Version="2.10.1" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="coverlet.collector" Version="1.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SmartHome.API\SmartHome.API.csproj" />
<ProjectReference Include="..\SmartHome.Models\SmartHome.API.Models.csproj" />
</ItemGroup>
</Project>
And this is the SmartHome.API.csproj which seems to be the source of the issue.
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentValidation" Version="8.6.1" />
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.2.8" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.1" />
<PackageReference Include="MongoDB.Driver" Version="2.10.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SmartHome.Models\SmartHome.API.Models.csproj" />
</ItemGroup>
</Project>
Changing a package dependency from Microsoft.AspNetCore.App (2.2.8) into a FrameworkReference in the SmartHome.API.csproj solved the problem for me at the cost of introducing a new one.
Initial fix
+ <ItemGroup>
+ <FrameworkReference Include="Microsoft.AspNetCore.App" />
+ </ItemGroup>
+
<ItemGroup>
<PackageReference Include="FluentValidation" Version="8.6.1" />
- <PackageReference Include="Microsoft.AspNetCore.App" Version="2.2.8" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.1" />
<PackageReference Include="MongoDB.Driver" Version="2.10.1" />
</ItemGroup>
New warning
I started seeing a new warning:
C:\Program Files\dotnet\sdk\3.1.101\Sdks\Microsoft.NET.Sdk\targets\
Microsoft.NET.Sdk.FrameworkReferenceResolution.targets(39,5):
warning NETSDK1086: A FrameworkReference for 'Microsoft.AspNetCore.App' was
included in the project. This is implicitly referenced by the .NET SDK and you
do not typically need to reference it from your project. For more information,
see https://aka.ms/sdkimplicitrefs
Final fix
...so I ended up removing the "Microsoft.AspNetCore.App" reference altogether. Now the build is warning-free!
i.e. the file looks like this:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentValidation" Version="8.6.1" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.1" />
<PackageReference Include="MongoDB.Driver" Version="2.10.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SmartHome.Models\SmartHome.API.Models.csproj" />
</ItemGroup>
</Project>
Thanks a lot for the provided fix.
I deleted the reference <PackageReference Include="Microsoft.AspNetCore.App" Version="2.2.8" /> and my build is now warning-free too!! :)
Br,
harald

.net core web api with .net standard library EF

I am having the weirdest of issues in a .net core web api project I have added a .net standard library which houses my models and EF code first. This all works when I deploy to IIS Express but when I attempt to publish it to IIS, the build fails.
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Folder Include="wwwroot\logs\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.3" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.3" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<PackageReference Include="NLog.Web.AspNetCore" Version="4.5.4" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet">
<Version>1.0.0-*</Version>
</DotNetCliToolReference>
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\mobileapp\forms\xxxCallManager\xxxDalCoreStandard\xxxDalCoreStandardxxxlDalCoreStandard.csproj" />
</ItemGroup>
<ItemGroup>
<Reference Include="System.Text.Encoding.CodePages">
<HintPath>..\..\..\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\ReferenceAssemblies\Microsoft\Framework\MonoAndroid\v1.0\Facades\System.Text.Encoding.CodePages.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
As you see from about their is no mention of system.refelection so i can only asume its one the nuget in my standard project that is messing it up below is my standard.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.1" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.2" />
</ItemGroup>
</Project>
The error I am getting is as follows now I tried adding the nuget of system.reflection but still no joy
Edit 1
The suggestion below helped a bit but now I am stuck with this error.
Severity Code Description Project File Line Suppression State
Error The command ""dotnet" exec --runtimeconfig
"C:\Work\xxxApp\xxxlApis\bin\Release\netcoreapp2.0\FuelApis.runtimeconfig.json"
--depsfile "C:\Work\FxxxApp\xxxlApis\bin\Release\netcoreapp2.0\xxxApis.deps.json"
"C:\Program
Files\dotnet\sdk\NuGetFallbackFolder\microsoft.aspnetcore.mvc.razor.viewcompilation\2.0.3\build\netstandard2.0\Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.dll"
#"obj\Release\netcoreapp2.0\microsoft.aspnetcore.mvc.razor.viewcompilation.rsp""
exited with code 1. xxxlApis 0
You are using a wrong version of EF tools in the first project, it looks like a 1.0 preview version, it should be:
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.*" />
also I would remove this part:
<ItemGroup>
<Reference Include="System.Text.Encoding.CodePages">
<HintPath>..\..\..\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\ReferenceAssemblies\Microsoft\Framework\MonoAndroid\v1.0\Facades\System.Text.Encoding.CodePages.dll</HintPath>
</Reference>
I don't think you need that especially the hint path part. I would try remove that altogether

Categories