ASP.NET Core 3.0 get_HostingEnvironment() Method not found in extension
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:
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.
Comments
-
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 thevar 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 tonetcoreapp3.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 over 4 yearsIt's not available on either
HostBuilderContext
(fromIHostBuilder.ConfigureAppConfiguration
) orWebHostBuilderContext
(fromIWebHostBuilder.ConfigureAppConfiguration
) -
Edward over 4 yearsAs you have found,
Microsoft.AspNetCore.Hosting.Abstractions
are in different locations betweennetcoreapp3.0
andnetstandard2.0
. Fromnetstandard2.0
tonetcoreapp3.0
, there are many break changes, you may keep targeting tonetcoreapp3.0
with the workaround in your origianl post. ForMicrosoft.AspNetCore.Hosting.Abstractions
with version 3.0, its target isnetcoreapp3.0
which means, you could not use it innetstandard2.0
. -
JvR over 4 years100% @TaoZhou - the only confusing thing is still why I can inspect the value during debug. Any thoughts on this?
-
Jaded about 3 yearsWorked 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.