3

I've created a minimal web API in .NET 7 and I am trying to extract some of the code to a separate assembly. This code is using IResult, which was introduced in .NET 7.

How do I reference IResult from a regular class library?

According to MSDN, the type is supposed to be in the assembly Microsoft.AspNetCore.Http.Abstractions.dll in the NuGet Microsoft.AspNetCore.App.Ref v7.0.3, but that is a platform component that cannot be referenced directly by assemblies.

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
Reyhn
  • 997
  • 1
  • 11
  • 22
  • Have you tried changing your csproj file to be ``? – DavidG Apr 05 '23 at 16:17
  • @DavidG Yes, the referencing works then, but it will change the project from class library to a web application, which requires a main entry method. – Reyhn Apr 06 '23 at 08:59

1 Answers1

5

For a class library using Microsoft.NET.Sdk, you'll need to add a FrameworkReference.

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

See the documentation.

As of .NET Core 3.0, projects using the Microsoft.NET.Sdk.Web MSBuild SDK implicitly reference the shared framework. Projects using the Microsoft.NET.Sdk or Microsoft.NET.Sdk.Razor SDK must reference ASP.NET Core to use ASP.NET Core APIs in the shared framework.

Your .csproj file might look like below.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
  </PropertyGroup>

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

</Project>
Kit
  • 20,354
  • 4
  • 60
  • 103
pfx
  • 20,323
  • 43
  • 37
  • 57