Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

Ask Question
  • New Project , Class Library (.NET Standard)
  • On project, properties, changed from 2.0 to 2.1
  • Modified Class1 :
  • public class Class1
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    
  • This fixed (correctly?) the IApplicationBuilder red squiggly :
  • 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.

    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)
                    Reading your first link, it is explained that is is the otherway round : IHostingEnvironment is being replaced with IWebHostEnvironment. You can confirm this by adding a two new ASP.NET Core projects :  the 2.2 Startup.cs contains : Configure(IApplicationBuilder app, IHostingEnvironment env);  whereas the 3.1 project contains : Configure(IApplicationBuilder app, IWebHostEnvironment env)
    – Naked Coder
                    Feb 11, 2020 at 14:04
                    This answer does not work if you are using <Project Sdk="Microsoft.NET.Sdk">. In order for this to work, you have to switch to <Project Sdk="Microsoft.NET.Sdk.Web"> which also forces you to have a static Program.Main() so it is not an actual class library anymore.
    – BearsEars
                    Oct 12, 2020 at 17:16
    

    This seems to work in a .NET Core 3.1 Class Library project file, but not a .NET Standard 2.1 one :

    I added the following to the project file :

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

    Now the build succeeds with :

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

    Is there a way for a .NET Standard 2.1 Class Library project to support the new IWebHostEnvironment ?

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.