Oleg Sh
Oleg Sh

Reputation: 9011

.NET Core 2.2 can't be published to Azure

I have ASP.NET Core 2.2 project and it can be run locally successfully. But when I try to publish it to Azure I get an error:

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

where Program class has the following code:

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();
}

Startup ConfigureServices method:

        var provider = services.BuildServiceProvider();
        var hostingEnvironment = provider.GetRequiredService<IHostingEnvironment>();
        CommonHelper.DefaultFileProvider = new TmsFileProvider(hostingEnvironment);

where:

    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;
    }

(don't ask me about this code, it's not my code, I just have to use it)

I confused, why Azure knows my local path C:\Users\rover\Source\Repos\TMS\Web\Tms.WebApi\, where it's described and how to solve problem with deploy?

Upvotes: 2

Views: 3404

Answers (1)

Oleg Sh
Oleg Sh

Reputation: 9011

I found the problem, hostingEnvironment can be null, when wwwroot folder does not exist. Then .NET Core throws such exception on Azure

Upvotes: 3

Related Questions