Home > database >  What Nuget packages must be referenced by an ASP.NET Core 6 application parts project
What Nuget packages must be referenced by an ASP.NET Core 6 application parts project

Time:03-03

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://docs.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();
}

CodePudding user response:

You can just add <FrameworkReference Include="Microsoft.AspNetCore.App"/> to library projects .csproj file as mentioned in the docs:

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

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

  <!--... rest of file-->
</Project>
  • Related