Search code examples
c#asp.net-core.net-corepackagelibraries

How to reference ASP.NET Core 6 types in .NET 6 library?


I am working on internal libraries for my organization. I need to reference certain types that normally "belong" to web apps, eg. to provide some extensions:

  • WebApplicationOptions
  • HttpContext

If I use <Project Sdk="Microsoft.NET.Sdk">, i don't have references to these types and can't find any nuget package which contains them. If i use <Project Sdk="Microsoft.NET.Sdk.Web">, i can't build my class library because it is treated as application and has to specify entry point.

So, my question in general: what is the right way to write libraries for ASP.NET Core 6?

Back in core 2.x days there were packages for everything, like Microsoft.AspNetCore.Http.Abstractions. They are all stuck at version 2.2.0 and i assume they're not usable now?


Solution

  • You need to add a FrameworkReference to your project file instead of a PackageReference as described here.

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

    Right-click your project in the Solution Explorer and select "Edit Project File" to open the file and look for the <ItemGroup> section, creating a new one if it does not exist.