azure - .NET Core 2.2 无法发布到 Azure

标签 azure asp.net-core deployment .net-core publish

我有 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

其中 Program 类具有以下代码:

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上抛出这样的异常

关于azure - .NET Core 2.2 无法发布到 Azure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56904032/

相关文章:

azure - 查询特定格式

performance - Azure Redis 内存使用情况(w3wp.exe 转储)洞察

c# - Azure DevOps Pipeline 无法找到来自其他项目的 DLL

asp.net - 无法在 MySql DbContext 的 dotnet ef 迁移中创建对象错误,但适用于 Sqlite DbContext

sql-server - 如何从 VS 2012 数据库项目创建部署脚本?

c# - 远程调试 - .NET Core 调试器不支持 Windows PDB - 如何正确发布?

c# - 使用未部署在 Azure 中的应用程序访问 Azure Key Vault 存储的 secret

c# - 读取net core api 3.1中的AuthorizationFilterContext

deployment - 将 elasticsearch 服务直接暴露给客户端或将其放在中间件后面

当作业已在运行时, azure 逻辑应用程序重试