This is the second project I have updated from ASP.Net Core 2.2 to 3.1. The first one runs fine. The second one runs fine in Visual Studio (2019), but when you publish it and run it from dotnet CLI the console just hangs indefinitely, no output in the console and I have enabled stdout and the output file is zero bytes.
The solution is hosted within IIS and when I try and run it through IIS I get the following entries in the application event log:
Application '/LM/W3SVC/2/ROOT' with physical root 'D:\wwwroot\InclusionService_UAT\' failed to start process with commandline 'C:\Program Files\dotnet\dotnet.exe .\InclusionService.Web.dll' at stage 'PostStartCheck', ErrorCode = '0x8027025a', assigned port 12973, retryCounter '1'.
and
Application '/LM/W3SVC/2/ROOT' with physical root 'D:\wwwroot\InclusionService_UAT\' failed to start process with commandline 'C:\Program Files\dotnet\dotnet.exe .\InclusionService.Web.dll' with multiple retries. Failed to bind to port '35033'. First 30KB characters of captured stdout and stderr logs from multiple retries: nothing more shown
This is my Program.cs which I have exactly the same in my other migrated solution:
public static void Main(string[] args)
{
var builder = new HostBuilder()
.ConfigureWebHostDefaults(opt =>
{
opt.UseStartup<Startup>();
opt.UseIISIntegration();
});
var host = builder.Build();
host.Start();
}
It would be much easier to debug if there was some output, but there's nothing to go on.
Please see Yush0's answer for the correct solution.
The problem was down to using the new IHost
instead of the existing IWebHost
in Program.cs
.
It's worth noting that IHost
worked fine with the migration to .NET Core 3.1 with Razor Pages, but this solution was and MVC application and would only work in IIS with IWebHost
.
When I put the original code back as below, the application fired up straight away in IIS:
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseIISIntegration();
}
I also had to make sure the web.config is correct since it doesn't get overwritten on publish anymore (at least when selfcontained). It should look as follows:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath=".\MyApp.exe" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
</system.webServer>
</location>
</configuration>
Specifying the hostingModel did the trick for me.
program.cs didn't seem to make a difference, but it looks as follows (without deprecated WebHostBuilder):
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.UseIIS();
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With