4
  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.

Naked Coder
  • 121
  • 1
  • 5

2 Answers2

5

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)
        {

        }
    }
}

Thomas N
  • 623
  • 1
  • 4
  • 14
  • 2
    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 '20 at 14:04
  • 1
    Ah yes my bad, I will update the answer to avoid confusion – Thomas N Feb 11 '20 at 15:21
  • 1
    This answer does not work if you are using ``. In order for this to work, you have to switch to `` which also forces you to have a static Program.Main() so it is not an actual class library anymore. – BearsEars Oct 12 '20 at 17:16
4

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 ?

Naked Coder
  • 121
  • 1
  • 5