Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import Microsoft.AspNetCore.Hosting.IWebHostEnvironment?

The following code in my project gets the following error.

public static IServiceCollection AddInfrastructure(this IServiceCollection services, 
  IConfiguration configuration, 
  Microsoft.AspNetCore.Hosting.IWebHostEnvironment environment) // Error
{
    //    .....
    return services;
}

Error CS0234 The type or namespace name 'IWebHostEnvironment' does not exist in the namespace 'Microsoft.AspNetCore.Hosting' (are you missing an assembly reference?)

I already import the nuget package.

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.2.7" />
    <PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.0.0" />
  </ItemGroup>

enter image description here

like image 703
ca9163d9 Avatar asked Nov 04 '19 19:11

ca9163d9


1 Answers

If you want to use Microsoft.AspNetCore.Hosting.IWebHostEnvironment in a separate library project other than your web project, you have to explicitly add the Microsoft.AspNetCore.App package reference to the library project by editing the project file.

In Dotnet 5.0 also, we have to follow same approach.

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

 <PropertyGroup>
  <OutputType>Library</OutputType>
  <TargetFramework>net5.0</TargetFramework>
 </PropertyGroup>

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

 <ItemGroup>...</ItemGroup>

</Project>
like image 190
Roshin Raj V Avatar answered Sep 26 '22 03:09

Roshin Raj V