31

I am creating an ASP.NET Core 6 application in which I want to integrate additional areas provided by separate assemblies. I follow the documentation at https://learn.microsoft.com/en-us/aspnet/core/mvc/advanced/app-parts?view=aspnetcore-6.0. In a project that provides app parts, I add an extension method for IMvcBuilder which allows me to register the parts assembly conveniently, for instance:

services.AddMvc().AddFeatureXyzParts();

The extension does nothing more like this:

public static IMvcBuilder AddFeatureXyzParts(this IMvcBuilder builder)
{
    if (builder == null) throw new ArgumentNullException(nameof(builder));
    builder.AddApplicationPart(typeof(MvcBuilderExtensions).Assembly);
    return builder;
}

In the project file, I changed the Sdk attribute from Microsoft.NET.Sdk to Microsoft.NET.Sdk.Web to reference the ASP.NET Core 6 SDK implicitly (as it is set up for the main application project), but this breaks the build with the error stating that the assembly lacks the definition of a Main method.

Since it is not a standalone web application assembly, but a parts library shipping additional controller types and services, I´d like to set the Sdk property back to Microsoft.NET.Sdk. Then I have to add missing package references manually. For this, the Microsoft.AspNetCore.App package seems to be the wrong choice for ASP.NET Core 6, and Microsoft.AspNetCore.App.Refs must not be used.

What references do I need instead?

Update - Define the required entry point to satisfy the build

As a workaround that allows me to reference the required ASP.NET Core 6 SDK, I can use the Microsoft.NET.Sdk.Web SDK if I add an internal Program class that defines a Main method. With this in place, I can register my part assembly as wanted, and controllers (even if part of an area) integrate nicely.

internal static class Program
{
    public static void Main() => throw new NotImplementedException();
}

2 Answers 2

59

You can just add

<ItemGroup>
  <FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

to library projects .csproj file as explained in the docs:

<Project Sdk="Microsoft.NET.Sdk">
  <!--... rest of file-->

  <ItemGroup>
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
  </ItemGroup>

  <!--... rest of file-->
</Project>
6
  • 1
    This is 100000x times easier than trying to find every single package reference.
    – Chris Rice
    Commented Jan 21, 2023 at 4:42
  • 1
    never thought that it would be so easy. So can we use this in worker app too ?
    – vibs2006
    Commented Jun 5, 2023 at 14:49
  • 1
    @vibs2006 yes, for sure. Though it still will require corresponding runtime installed.
    – Guru Stron
    Commented Jun 5, 2023 at 14:54
  • 3
    with Microsoft.AspNetCore.App takes almost 4 minutes to do a restore in CI/CD (azure pipelines) Commented Jun 12, 2023 at 19:02
  • 1
    @JuanZamudio TBH have not experienced the same.
    – Guru Stron
    Commented Jun 12, 2023 at 21:48
1

For me, I was upgrading a class lib. using Microsoft.NET.Sdk

From:

<TargetFrameworks>netcoreapp2.1;net45;net46;net47;net48</TargetFrameworks>

To:

<TargetFrameworks>net8.0</TargetFrameworks>

I found that adding the following solved my all build errors

<ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
</ItemGroup>

Not sure if would work for the OPs exact situation, but posting as a series of "duplicate" questions led me here, and want to post in-case helps some other googler out.

The upgrade assistant VSIX skipped this, with some uninformative note. The old nugets have notices to say deprecated, but don't help with what to use instead.

This is all VS2022 Community Edition.

UPDATE 2024-05:

As per @Guru Stron comment,this package is deprecated. Something must have been odd with my build before, as I have now removed this Package and added

<FrameworkReference Include="Microsoft.AspNetCore.App" />

...as per Guru Stron's answer and everything works perfectly. So that is definitely the recommendation.

2
  • 1
    Microsoft.AspNetCore.Http.Abstractions is deprecated, avoid adding it.
    – Guru Stron
    Commented May 14 at 11:15
  • 1
    @GuruStron thanks yes 100%. Not sure what happened when I posted this, guess maybe old artifacts kicking about in bin folders or something, as at the time your solution did not seem to work for me. I have just tried again, removed the deprecated Microsoft.AspNetCore.Http.Abstractions packRef and added <FrameworkReference Include="Microsoft.AspNetCore.App" /> as per your answer, and everything is building and running cleanly. Many thanks ! Commented May 14 at 18:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.