下面的代码复制了我尝试在新的 ASP.NET Core 3.0 API 项目中创建的某些扩展的错误。
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();
}
}
该类Extensions需要位于不同的项目中。在同一个主项目中似乎工作正常。
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;
}
它仅在运行时失败,并且仅在访问HostingEnvironment. 当删除/评论var env = context.HostingEnvironment;作业时,我仍然可以在调试中检查和查看它。
知道我在这里缺少什么吗?
慕田峪7331174
慕后森
千巷猫影
噜噜哒