user2286759
user2286759

Reputation: 189

Can an ASP.NET Core 2.0 project be created without a Microsoft.AspNetCore.All package?

When Visual Studio creates an ASP.NET Core 2.0 project using an Empty template, it automatically references a Microsoft.AspNetCore.All package which depends on everything (every ASP.NET related package I can think of). It seems to me that it defeats a key advantage of ASP.NET Core (being modular).

Can I make Visual Studio not include this package with every project?

Upvotes: 3

Views: 1301

Answers (3)

Mark
Mark

Reputation: 599

To get a new ASP.NET Core 2.0 app to target .Net Framework (e.g. to use SpecFlow.NetCore for BDD) open NuGet Manager to remove the Microsoft.AspNetCore.All then add Microsoft.AspNetCore and Microsoft.AspNetCore.Mvc packages. Building should highlight any futher packages you need to add. My end result was:

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore" Version="2.0.3" />
    <PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.0.3" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.4" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.0.4" />
  </ItemGroup>

You can now target .Net Framework 4.6.1, e.g.

<TargetFrameworks>netcoreapp2.0;net461</TargetFrameworks>

Without receiving a Microsoft.AspNetCore.All 2.0.0 is not compatible with Net461 error.

Of course, you loose all the advantages of the meta-package.

Upvotes: 0

Chris Amelinckx
Chris Amelinckx

Reputation: 4482

Building on pcdev's answer, which contains good references to what you can currently read on this topic.

For simplicity, do this to enable Package Trimming.

In the cs.proj file add the following entries:

  • PropertyGroup: TrimUnusedDependencies, true
  • PackageReference: Microsoft.Packaging.Tools.Trimming

  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <RuntimeIdentifiers>win10-x64</RuntimeIdentifiers>
    <TrimUnusedDependencies>true</TrimUnusedDependencies>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Packaging.Tools.Trimming" Version="1.1.0-preview1-25818-01" />
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.3" />
  </ItemGroup>
</Project>

With this solution you can still reference the Microsoft.AspNetCore.All package but achieve a smaller footprint.

In my case I am doing self contained deployment (which doesn't use the Runtime package store, the machine-wide installation of the .net core assemblies)

I build my app with cli command:

"C:\Program Files\dotnet\dotnet.exe" publish --configuration Release -r win10-x64 --self-contained 

Microsoft (R) Build Engine version 15.3.409.57025 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Trimmed 191 out of 405 files for a savings of 31.62 MB
  Final app size is 59.5 MB
  [projectName] -> [path]\bin\Release\netcoreapp2.0\win10-x64\[projectName].dll
  Trimmed 191 out of 405 files for a savings of 31.62 MB
  Final app size is 59.5 MB
  [projectName]-> [path]\bin\Release\netcoreapp2.0\win10-x64\publish\

Upvotes: 1

pcdev
pcdev

Reputation: 3052

The Microsoft.AspNetCore.All package is, as DavidG points out, just a meta package. Yes, it references all packages covering ASP.NET Core 2.x and Entity Framework Core 2.x., and yes that means it's huge. However these packages are not deployed with your app, unless you consider the runtime part of your app.

One of the reasons for introducing the .All meta package was to reduce the clutter in the project file, and it's done a great job of that. Now you will still get a large number of dependencies in your deps.json file unless you trim as described below, but the dll's themselves are not included.

Here are a couple of good resources:

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/metapackage

https://github.com/dotnet/standard/blob/release/2.0.0/Microsoft.Packaging.Tools.Trimming/docs/trimming.md

https://andrewlock.net/the-microsoft-aspnetcore-all-metapackage-is-huge-and-thats-awesome-thanks-to-the-net-core-runtime-store-2/ - a little older as it references Preview 1, but it still applies.

Hope that helps.

Upvotes: 2

Related Questions