[Solved] HTTP Error 500.30 – ASP.NET Core app failed to start
HTTP Error 500.30 – ASP.NET Core app failed to start, this error is obvious while hosting your Web API or Blazor, Web application developed in ASP.NET CORE, even in .Net 5. Azure app service responses suggest what might have caused this error - could it be app service stopped or throw an exception is the project is trying to host, usually on the configuration appsettings.json or startup.cs
This error is not a big deal to leave unattended. Azure App service hosting is much faster than earlier. To host Web API or Web Application to App Service, the app service must be active and run.
Key scenarios
Step 1: What is the Error?
Now the story changed like this, I have hosted my Web API earlier, and was working successfully. Later I continue to implement the logic to capture the exceptions in a log file to wwwroot folder in Web API Project Solution.
I now try to re-host the Web API to Azure App service - boom, the error appeared as below captured image.
By reading the suggested error from the Azure service would give us a piece of generic information. To spot the specific reason it is required to look at the exception log created by the app service.
Now, where do I find the error captured by Azure?
Login to your Azure Portal.
Please note that in this article we are trying to host web API to Azure App Service. So it is required to find the logs created by azure under App Service.
Step 2: What caused the Error?
At the end of step 1 directs you to log in to the Azure portal. Go to Home > Resources > your App Service > Click on Diagnosis and solve problems, filter the error and find the exception message captured by App service.
Now let's take a look at the image below with numbers marked.
Application '/LM/W3SVC/2138455566/ROOT' physical root 'D:\home\site\wwwroot\' hit unexpected managed exception,exception code = '0xe0434352'. First 30KB characters of captured stdout and stderr logs: crit: Microsoft.AspNetCore.Hosting.Diagnostics[6] Application startup exception System.ArgumentNullException: Value cannot be null. (Parameter 'path1') at System.IO.Path.Combine(String path1, String path2).
The arrow points out number 6 the line number in which the error is captured and the exception is thrown.
What does that error say?
At the very first line of the error description in #6 points to \wwwroot, and further next line System.ArgumentNullException: Value cannot be null. (Parameter 'path1)
The issue is very clear, in the startup.cs line number 122, you are trying to pass an argument that does not exist in Azure App Service.
Now let's go back to Visual Studio Solution Web API Project.
Step 3: How do I solve the Error?
Open Visual Studio Project Solution Explorer, click on Startup.cs file, and locate to Configure method, and line number #122.
What does that line trying to do?
The line is trying to create a text log file under wwwroot folder "logs" of Azure App Service, the folder path would be wwwroot\logs\
but the Azure does not have the folder and the parameter wenvironment.WebRootPath
the value will always be null.
This is the reason, Azure host has failed to start.
How do I rectify the error?
Add null checking to environment WebRootPath if empty create a directory under wwwroot folder and then add the log error to it.
Startup.cs
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
//app.UseExceptionHandler("/error-development");
}
else
{
app.UseHsts();
//app.UseExceptionHandler("/error-staging");
}
// log errors
if (string.IsNullOrWhiteSpace(env.WebRootPath))
{
env.WebRootPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot");
}
// Create log folder if not exists
if (!Directory.Exists(Path.Combine(env.WebRootPath, "logs"))) Directory.CreateDirectory(Path.Combine(env.WebRootPath, "logs"));
loggerFactory.AddFile($"{Path.Combine(env.WebRootPath, "logs")}/log.txt");
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
//app.UseExceptionHandler("/error-development");
}
else
{
app.UseHsts();
//app.UseExceptionHandler("/error-staging");
}
// log errors
if (string.IsNullOrWhiteSpace(env.WebRootPath))
{
env.WebRootPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot");
}
// Create log folder if not exists
if (!Directory.Exists(Path.Combine(env.WebRootPath, "logs"))) Directory.CreateDirectory(Path.Combine(env.WebRootPath, "logs"));
loggerFactory.AddFile($"{Path.Combine(env.WebRootPath, "logs")}/log.txt");
}
Checking "Remove additional files at destination" will remove the existing files at Azure App Services files and replace them with the latest version of files every time publish from Visual Studio.
How do I do that?
In the Publish profile, click on "Edit" under File Publish Options check the box "Remove additional files at the destination".
Click on Save and now click on Publish button to host successfully.
That's it to solve the problem.
Summary
This article explains the solution for HTTP Error 500.30 - ASP.NET Core app failed to start.
This error is prompted when we try to host Web API (.NET 5) from Visual Studio. Then we traced the exception captured by the Azure App Service in Diagnosis and solve problems and found that our Web API was trying to create a log file under this folder wwwroot\logs path that doe not exist in Azure App Service.
Then we added environment.WebRootPath null checks and then if the folder does not exist create a folder and then combine folder path log file to store.
We also learned a tip, In Publish profile edit to enable "Remove additional files at the destination" to help remove all existing files from the server every time publish from Visual Studio.
The key point is to ensure Azure App Service is up and running before hitting the Publish button from Visual Studio.
0 Comments