Jeroen
Jeroen

Reputation: 63719

Running ASP.NET Core application in full IIS without publishing gives 502.5 error

I'm trying to host an ASP.NET Core application in IIS on my local machine, and I'm getting a 502.5 error. My question is different from "ASP.NET Core 1.0 on IIS error 502.5" because I'm not publishing my app, but (for testing purposes) trying to have IIS (not express) on my dev machine serve up the app.

To reproduce:

  1. Open VS2017
  2. Create a new ASP.NET Core Web Application (.NET Core)
  3. Choose the "Web API" template and target "ASP.NET Core 1.1" (no authentication)

    Your Main looks like this now:

    public static void Main(string[] args)
    {
        var host = new WebHostBuilder()
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>()
            .UseApplicationInsights()
            .Build();
    
        host.Run();
    }
    

    Your csproj looks like this:

    <Project Sdk="Microsoft.NET.Sdk.Web">
    
      <PropertyGroup>
        <TargetFramework>netcoreapp1.1</TargetFramework>
      </PropertyGroup>
    
      <ItemGroup>
        <Folder Include="wwwroot\" />
      </ItemGroup>
      <ItemGroup>
        <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />
        <PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" />
        <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" />
        <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.2" />
      </ItemGroup>
      <ItemGroup>
        <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.1" />
      </ItemGroup>
    
    </Project>
    
  4. Hit F5 to run and localhost:6565/api/values open up

  5. Add a "Web Configuration File" to the root of the project
  6. Uncomment the system.webServer section looking like this:

    <system.webServer>
        <handlers>
          <remove name="aspNetCore"/>
          <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
        </handlers>
        <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />
    </system.webServer>
    
  7. Open the IIS Manager GUI

  8. Choose "Add Website", pick a name and a port, set the path to the folder where the web.config is located
  9. As per Microsoft's instructions set the Application Pool's ".NET CLR version" to "No Managed Code"
  10. Browse to your new application, e.g. http://localhost:8089/api/values

    Result: HTTP Error 502.5 - Process Failure
    Expected: same result as in step 4.

I've tried to exclude all causes mentioned in the top answer to the other question:

In addition I've tried a few other things:

With ASP.NET MVC applications on the .NET Framework you could just spin up a new Website in full IIS that points to the MVC project's folder and it would "just work".

What do you need to do to get this to work with ASP.NET Core? Is it even possible to get this flow working without publishing?

Upvotes: 3

Views: 4004

Answers (6)

Imran Rafique
Imran Rafique

Reputation: 339

I followed the Answer of @Jim Lowrey and follow some additional steps:

  • installed the "Development time IIS support"
  • installed the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation version 6.0.24 as i was using .net6
  • installed the dotnet-hosting-6.0.24-win.exe Note: in my Windows 10, i have both SDK .net 6 and .net 7 installed.

Upvotes: 0

jsabrooke
jsabrooke

Reputation: 412

I'm not sure if anything has changed since the previous answers as I've only just started using ASP.NET Core, but see @Sнаđошƒаӽ's answer here. It just worked for me on ASP.NET Core 3.1.

Note that changes in cshtml files won't be reflected without building or publishing. To get runtime compilation of Razor files so that changes appear after just a page refresh, use Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation as explained here.

Upvotes: 0

Jim Lowrey
Jim Lowrey

Reputation: 109

I was able to get ASP.Net Core 2 to run behind IIS in development mode.

  1. Create a new dotnet app. For example run 'dotnet new react -o my-new-app' from the tutorial. Run npm install as directed.

  2. Install AspNetCoreModule.

  3. In IIS create a new site with no managed code and set the App Pool to run under LocalSystem. It will need this to write to C:\WINDOWS\system32\config\systemprofile. Set the physical path to the directory created from step 1.

  4. Put the following web.config in the directory from step 1.

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <!--
        Configure your application settings in appsettings.json. Learn more at http://go.microsoft.com/fwlink/?LinkId=786380
      -->
      <system.webServer>
        <handlers>
          <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
        </handlers>
        <aspNetCore processPath="C:\Program Files\dotnet\dotnet.exe"
                    arguments="run --project C:\<your-path-to>\my-new-app" 
                    stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false">
            <environmentVariables>
              <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
            </environmentVariables>
        </aspNetCore>
      </system.webServer>
    </configuration>
    

From there AspNetCoreModule orchestrates the interaction between IIS and dotnet.exe and the reverse proxy is set up. As a bonus all the webpack compiling, bundling and hot reloading worked just as they did with 'dotnet run'.

Upvotes: 1

Arman
Arman

Reputation: 846

You can use the new feature of Visual Studio 15.3 and newer called Development time IIS support.

Please see the related issue and the blog post for the new feature.

Upvotes: 1

Daboul
Daboul

Reputation: 2733

As @Dmitry is saying, I'm not sure you can plug IIS on an 'unpublished' folder, but there is something more, as stated in https://learn.microsoft.com/en-us/aspnet/core/publishing/iis, you need a module to enable IIS to plug itself on an ASP.NET Core module which is called ".NET Core Windows Server Hosting", have you installed it?

Upvotes: 0

Dmitry
Dmitry

Reputation: 16795

What do you need to do to get this to work with ASP.NET Core

Execute all required instructions, not only section you like. You should do publishing.

Is it even possible to get this flow working without publishing?

No.

During publishing your project is compiled and your web.config is modified to run compiled app (without SDK).

Upvotes: 2

Related Questions