Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class Library (Standard 2.1 or Core 3.1) : namespace name 'IWebHostEnvironment' could not be found

  1. New Project , Class Library (.NET Standard)
  2. On project, properties, changed from 2.0 to 2.1
  3. Modified Class1 :
public class Class1
{
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {

    }
}
  1. This fixed (correctly?) the IApplicationBuilder red squiggly :
  2. However the library project does not compile : "Error CS0246 The type or namespace name 'IWebHostEnvironment' could not be found ..."

What references should be added for these two interfaces please ? Or are you only supposed to only ever use these interfaces within the web site project ?

Also same result with a .NET Core 3.1 Class Library. Visual Studio 16.4.4

Any help much appreciated.

like image 892
Naked Coder Avatar asked Dec 31 '22 07:12

Naked Coder


1 Answers

You might want to follow this guide (for migrating 2.2 to 3.0)

https://learn.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.1&tabs=visual-studio

This explains what happened https://github.com/dotnet/AspNetCore/issues/7749

Update:

Net Standard

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

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
<!-- Should work with either if you have sdks installed and do restore -->
<!--<TargetFramework>netstandard2.1</TargetFramework>-->
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.2.7" />    
    <PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.1" />
  </ItemGroup>

</Project>

With class file

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Hosting;

namespace ClassLibrary2
{
    public class Class1
    {
        public void Configure(IApplicationBuilder app, IHostEnvironment env)
        {

        }
    }
}

Net Core 3.1

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

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
  </ItemGroup>
</Project>

With class file

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;

namespace ClassLibrary3
{
    public class Class1
    {
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {

        }
    }
}

like image 72
Thomas N Avatar answered May 10 '23 20:05

Thomas N