Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can't find IWebHostEnvironment in Microsoft.AspNetCore.Hosting.Abstractions assembly in a .NET Core class library

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>
like image 586
SteinTheRuler Avatar asked Jul 16 '20 04:07

SteinTheRuler


People also ask

What is the replacement for iwebhostenvironment in ASP NET Core?

This type is obsolete and will be removed in a future version. The recommended alternative is Microsoft.AspNetCore.Hosting.IWebHostEnvironment. Learn how the Startup class in ASP.NET Core configures services and the app's request pipeline. Discover how to handle errors in ASP.NET Core apps.

Should I use iwebhostenvironment or ihostingenvironment?

It is recommended that you use IWebHostEnvironment instead. The reasoning behind this presumably was that IHostingEnvironment has multiple implementations for the same type in .NET Core in different packages. The AspNetCore specific version in Microsoft.AspNetCore.Hosting looks like this:

What does the Red Line below the iwebhostenvironment type mean?

In the code blow, a red kind of line appears below the IWebHostEnvironment type for the env parameter saying that "The type or namespace 'IWebHostEnvironment' could not be found (are you missing a using directive or an assembly reference?)" public async Task Invoke (HttpContext context, IWebHostEnvironment env) { // Some code here.

What is hosting startup Assembly in ASP NET Core?

Use hosting startup assemblies in ASP.NET Core. An IHostingStartup (hosting startup) implementation adds enhancements to an app at startup from an external assembly. For example, an external library can use a hosting startup implementation to provide additional configuration providers or services to an app.


1 Answers

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.

like image 147
madoxdev Avatar answered Oct 08 '22 03:10

madoxdev