Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net Core solution migrated from 2.2 to 3.1 won't run when published

Tags:

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.

like image 345
Jason Goodwin Avatar asked Dec 31 '19 14:12

Jason Goodwin


2 Answers

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();
}
like image 100
Jason Goodwin Avatar answered Oct 29 '22 22:10

Jason Goodwin


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();
        });
like image 27
Yush0 Avatar answered Oct 29 '22 21:10

Yush0