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

13,756

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.

Share:
13,756
JvR
Author by

JvR

.NET Software Developer in Cape Town

Updated on July 15, 2022

Comments

  • JvR
    JvR almost 2 years

    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?

  • JvR
    JvR over 4 years
    It's not available on either HostBuilderContext (from IHostBuilder.ConfigureAppConfiguration) or WebHostBuilderContext (from IWebHostBuilder.ConfigureAppConfiguration)
  • Edward
    Edward over 4 years
    As you have found, Microsoft.AspNetCore.Hosting.Abstractions are in different locations between netcoreapp3.0 and netstandard2.0. From netstandard2.0 to netcoreapp3.0, there are many break changes, you may keep targeting to netcoreapp3.0 with the workaround in your origianl post. For Microsoft.AspNetCore.Hosting.Abstractions with version 3.0, its target is netcoreapp3.0 which means, you could not use it in netstandard2.0.
  • JvR
    JvR over 4 years
    100% @TaoZhou - the only confusing thing is still why I can inspect the value during debug. Any thoughts on this?
  • Jaded
    Jaded about 3 years
    Worked for me with .NET Core 3.1/.NET Standard 2.1. Added <PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="5.0.0" /> into shared library.