26

I can't reference the IWebHostEnvironment element in my .NET Core class library. I have added NuGet packages Microsoft.AspNetCore.Hosting.Abstractions and Microsoft.Extensions.DepedencyInjection.Abstractions, but it still can't find the type. In the documentation, IWebHostEnvironment is in the Microsoft.AspNetCore.Hosting.Abstractions assembly, but I can't seem to reference the correct assembly.

Is there any other assembly I need to reference?

Project file:

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

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <RootNamespace>LundbeckConsulting.Components.Core</RootNamespace>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.6" />
    <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.6" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\LC.Components\LC.Components.csproj" />
  </ItemGroup>

</Project>

2 Answers 2

69

Please add this to your project file:

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

You shouldn't need to specify any other packages for framework. Please follow this link

Pay attention to FrameworkReference attribute.

8
  • 1
    Emphasis on FrameworkReference! This can be easily missed.
    – badsyntax
    Nov 1, 2021 at 7:50
  • 1
    Saved a lot of time ! I am using .NET 5
    – Taha Ali
    Apr 18, 2022 at 4:55
  • Any context for why this type isn't included in a NuGet package? Oct 13, 2022 at 18:06
  • is there a counterpart adjustment for framework 4.7.2? Jan 6 at 6:59
  • 1
    You are a miracle-maker! :-O
    – Alexander
    Feb 7 at 16:45
0

If you look at the documentation for that interface, you'll see it's introduced with version 3.0.

You are referencing version 2.2.0.

2
  • 2
    This is not the right answer. 2.2.0 is the current version of that package even with .NET 5.0. Sep 9, 2021 at 3:45
  • It was incorporated into ASP.NET Core 3.
    – CodeCaster
    Sep 9, 2021 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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