ASP.NET Core 3.0 get_HostingEnvironment() Method not found in extension

JvR picture JvR · Sep 30, 2019 · Viewed 10.2k times · Source

Below code to replicate error for some extensions I'm trying to create in a new ASP.NET Core 3.0 API project.

using ClassLibrary1;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;

namespace WebApplication1
{
  public class Program
  {
    public static void Main(string[] args)
    {
      CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
              webBuilder.UseStartup<Startup>();

              webBuilder.ConfigureAppConfiguration((context, config) =>
              {
                // this works fine
                var env = context.HostingEnvironment;
              });

              webBuilder.CustomConfigureAppConfiguration();
            })
            .ConfigureAppConfiguration((context, config) =>
            {
              // this works fine
              var env = context.HostingEnvironment;
            })
            .CustomConfigureAppConfiguration();
  }
}

This Extensions class needs to be in a different project. Seems to work fine when in same main project.

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

// This project can be either netstandard2.0 or netcoreapp3.0
namespace ClassLibrary1
{
  public static class Extensions
  {
    public static IWebHostBuilder CustomConfigureAppConfiguration(this IWebHostBuilder hostBuilder)
    {
      hostBuilder.ConfigureAppConfiguration((context, config) =>
      {
        // this fails with System.MissingMethodException: 'Method not found: 'Microsoft.AspNetCore.Hosting.IHostingEnvironment Microsoft.AspNetCore.Hosting.WebHostBuilderContext.get_HostingEnvironment()'.'
        var env = context.HostingEnvironment;
      });

      return hostBuilder;
    }

    public static IHostBuilder CustomConfigureAppConfiguration(this IHostBuilder hostBuilder)
    {
      hostBuilder.ConfigureAppConfiguration((context, config) =>
      {
        // this fails with System.MissingMethodException: 'Method not found: 'Microsoft.Extensions.Hosting.IHostingEnvironment Microsoft.Extensions.Hosting.HostBuilderContext.get_HostingEnvironment()'.'
        var env = context.HostingEnvironment;
      });

      return hostBuilder;
    }
  }
}

It only fails at runtime, and only when accessing the HostingEnvironment. When removing/commenting the var env = context.HostingEnvironment; assignment, I can still inspect & view it fine in debug.

Any idea what I'm missing here?

EDIT

Updating the ClassLibrary1 project to netcoreapp3.0 and adding <FrameworkReference Include="Microsoft.AspNetCore.App" /> like below works:

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

  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.2.0" />
  </ItemGroup>

</Project>

I'd still like to keep the project as netstandard2.0 if possible. Perhaps some specific NuGet package I need for that?

Answer

JvR picture JvR · Sep 30, 2019

For the netstandard2.0 project, I could only get the HostingEnvironment in the HostBuilderContext (from IHostBuilder.ConfigureAppConfiguration) - with the Microsoft.Extensions.Hosting.Abstractions package installed:

public static IHostBuilder CustomConfigureAppConfiguration(this IHostBuilder hostBuilder)
{
  hostBuilder.ConfigureAppConfiguration((context, config) =>
  {
    // this works with Microsoft.Extensions.Hosting.Abstractions installed
    var env = context.HostingEnvironment;
  });

  return hostBuilder;
}

HostingEnvironment from WebHostBuilderContext (from IWebHostBuilder.ConfigureAppConfiguration) still throws an exception when executed by a netcoreapp3.0 project. This worked fine with previous versions.

Also I can still inspect it during debug when commented out: enter image description here

Something still seems a bit off, but at least this works now.

UPDATE

After more digging I found the difference in references:
- netcoreapp3.0 - via shared framework Microsoft.AspNetCore.Hosting.Abstractions, Version=3.0.0.0
- netstandard2.0 via NuGet Microsoft.AspNetCore.Hosting.Abstractions, Version=2.2.0.0

v2.2 exposes Microsoft.AspNetCore.Hosting.IHostingEnvironment which has been deprecated in v3.0

As of 2019/10/01 v3.0 is not available on NuGet.