.NET Core 2.2 无法发布到 Azure
.NET Core 2.2 can't be published to Azure
我有 ASP.NET Core 2.2 项目,它可以 运行 在本地成功。但是当我尝试将其发布到 Azure 时出现错误:
Unhandled Exception: System.ArgumentException: The path must be absolute.
Parameter name: root
at Microsoft.Extensions.FileProviders.PhysicalFileProvider..ctor(String root, ExclusionFilters filters)
at Tms.Core.Infrastructure.TmsFileProvider..ctor(IHostingEnvironment hostingEnvironment) in C:\Users\rover\Source\Repos\TMS\Libraries\Tms.Core\Infrastructure\TmsFileProvider.cs:line 23
at Tms.WebApi.Startup.ConfigureServices(IServiceCollection services) in C:\Users\rover\Source\Repos\TMS\Web\Tms.WebApi\Startup.cs:line 222
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services)
at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()
at Microsoft.AspNetCore.Hosting.Internal.WebHost.Initialize()
at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
at Tms.WebApi.Program.Main(String[] args) in C:\Users\rover\Source\Repos\TMS\Web\Tms.WebApi\Program.cs:line 17
其中程序 class 具有以下代码:
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
启动ConfigureServices
方法:
var provider = services.BuildServiceProvider();
var hostingEnvironment = provider.GetRequiredService<IHostingEnvironment>();
CommonHelper.DefaultFileProvider = new TmsFileProvider(hostingEnvironment);
其中:
public TmsFileProvider(IHostingEnvironment hostingEnvironment)
: base(File.Exists(hostingEnvironment.WebRootPath) ? Path.GetDirectoryName(hostingEnvironment.WebRootPath) : hostingEnvironment.WebRootPath)
{
var path = hostingEnvironment.ContentRootPath ?? string.Empty;
if (File.Exists(path))
path = Path.GetDirectoryName(path);
BaseDirectory = path;
}
(不要问我这个代码,这不是我的代码,我只是必须使用它)
我很困惑,为什么 Azure 知道我的本地路径 C:\Users\rover\Source\Repos\TMS\Web\Tms.WebApi\
,它在哪里描述以及如何解决部署问题?
我发现了问题,当wwwroot
文件夹不存在时,hostingEnvironment
可以为空。然后 .NET Core 在 Azure
上抛出这样的异常
我有 ASP.NET Core 2.2 项目,它可以 运行 在本地成功。但是当我尝试将其发布到 Azure 时出现错误:
Unhandled Exception: System.ArgumentException: The path must be absolute.
Parameter name: root
at Microsoft.Extensions.FileProviders.PhysicalFileProvider..ctor(String root, ExclusionFilters filters)
at Tms.Core.Infrastructure.TmsFileProvider..ctor(IHostingEnvironment hostingEnvironment) in C:\Users\rover\Source\Repos\TMS\Libraries\Tms.Core\Infrastructure\TmsFileProvider.cs:line 23
at Tms.WebApi.Startup.ConfigureServices(IServiceCollection services) in C:\Users\rover\Source\Repos\TMS\Web\Tms.WebApi\Startup.cs:line 222
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services)
at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()
at Microsoft.AspNetCore.Hosting.Internal.WebHost.Initialize()
at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
at Tms.WebApi.Program.Main(String[] args) in C:\Users\rover\Source\Repos\TMS\Web\Tms.WebApi\Program.cs:line 17
其中程序 class 具有以下代码:
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
启动ConfigureServices
方法:
var provider = services.BuildServiceProvider();
var hostingEnvironment = provider.GetRequiredService<IHostingEnvironment>();
CommonHelper.DefaultFileProvider = new TmsFileProvider(hostingEnvironment);
其中:
public TmsFileProvider(IHostingEnvironment hostingEnvironment)
: base(File.Exists(hostingEnvironment.WebRootPath) ? Path.GetDirectoryName(hostingEnvironment.WebRootPath) : hostingEnvironment.WebRootPath)
{
var path = hostingEnvironment.ContentRootPath ?? string.Empty;
if (File.Exists(path))
path = Path.GetDirectoryName(path);
BaseDirectory = path;
}
(不要问我这个代码,这不是我的代码,我只是必须使用它)
我很困惑,为什么 Azure 知道我的本地路径 C:\Users\rover\Source\Repos\TMS\Web\Tms.WebApi\
,它在哪里描述以及如何解决部署问题?
我发现了问题,当wwwroot
文件夹不存在时,hostingEnvironment
可以为空。然后 .NET Core 在 Azure