在不发布的情况下在完整IIS中运行ASP.NET Core应用程序会出现502.5错误。

3
我正在尝试在我的本地计算机上使用IIS托管一个ASP.NET Core应用程序,但是我遇到了502.5错误。我的问题与"ASP.NET Core 1.0 on IIS error 502.5"不同,因为我没有发布我的应用程序,而是(出于测试目的)尝试让IIS(而不是express)在我的开发机器上提供应用程序服务。
复现步骤如下:
  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.

我尝试排除另一个问题的最佳答案中提到的所有原因:
  • 将应用程序池的标识设置为LocalSystem
  • 在控制台上检查是否可以运行dotnet --version(即是否在PATH上可用)

此外,我还尝试了其他一些方法:

  • processPath设置为"%LAUNCHER_PATH%\bin\Debug\netcoreapp1.1\WebApplication1.dll的变体
  • processPath指向dotnet.exe或其位置
  • 重新阅读文档以找出一种方法来解决这个问题。

对于基于.NET Framework的ASP.NET MVC应用程序,您可以在完整的IIS中启动一个指向MVC项目文件夹的新网站,它会“自动工作”。

你需要做什么才能使它与ASP.NET Core一起工作?即使没有发布,也有可能使此流程工作吗?

可能是ASP.NET Core 1.0在IIS上的502.5错误的重复问题。 - Pranav Patel
@PranavPatel 我在问题中已经解释过为什么它不同,对吗?我想要跳过“发布”步骤,就像你在ASP.NET 4.6 MVC应用程序中可以做的那样。 - Jeroen
6个回答

2
您需要做什么才能使其与ASP.NET Core兼容?
请执行所有必要的说明,而不仅仅是您喜欢的部分。您应该进行发布。
没有发布,这个流程甚至可能无法运行。
在发布期间,将编译您的项目并修改您的web.config以运行已编译的应用程序(无需SDK)。

1
我成功地让ASP.Net Core 2在开发模式下运行在IIS后面。
  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>
    
从那里开始,AspNetCoreModule协调IIS和dotnet.exe之间的交互,并设置反向代理。作为额外的奖励,所有webpack编译、捆绑和热重载都与“dotnet run”一样工作。

我使用了这个答案并按照一些额外的步骤进行操作:
  • 安装了"开发时间IIS支持"
  • 安装了Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation 6.0.24版本,因为我使用的是.net6
  • 安装了dotnet-hosting-6.0.24-win.exe
- undefined

1
你可以使用Visual Studio 15.3及以上版本的新功能——"开发时IIS支持"。请参考相关问题博客文章了解该新功能。

0
我按照@Jim Lowrey的答案,并跟随了一些额外的步骤:
  • 安装了“开发时间IIS支持”
  • 安装了Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation 6.0.24版本,因为我使用的是.net6
  • 安装了dotnet-hosting-6.0.24-win.exe 注意:在我的Windows 10上,我同时安装了.net 6和.net 7的SDK。

0

就像@Dmitry所说的那样,我不确定你是否可以在“未发布”的文件夹上插入IIS,但还有一点需要注意,正如https://learn.microsoft.com/en-us/aspnet/core/publishing/iis中所述,你需要一个模块来启用IIS自己插入ASP.NET Core模块,这个模块被称为“.NET Core Windows Server Hosting”,你安装了吗?


0

我不确定自从上次回答以来是否有任何变化,因为我刚开始使用ASP.NET Core,但请参考@Sнаđошƒаӽ的答案这里。在ASP.NET Core 3.1上对我有效。

请注意,在不构建或发布的情况下,cshtml文件的更改不会反映出来。要在只需刷新页面后即可看到更改的Razor文件进行运行时编译,请使用Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation,如此处所述。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接