当 运行 我的 .NET 6 Web API 作为独立服务时如何防止 404?

How to prevent a 404 when running my .NET 6 Web API as a self contained service?

我制作了一个简单的 .NET 6 Web API。当 运行ning 从 Visual Studio 时(只需按 F5),这工作正常。但是,当 运行 将我的应用程序作为独立服务使用时,我会收到 404 请求,表示本应成功的调用。如何确保我的独立服务开始时表现相同?

这是我的 Program.json

using Microsoft.Extensions.Hosting.WindowsServices;


var options = new WebApplicationOptions
{
    Args = args,
    ContentRootPath = WindowsServiceHelpers.IsWindowsService() ? AppContext.BaseDirectory : default
};

var builder = WebApplication.CreateBuilder(options);
builder.Services.AddRazorPages();

builder.Host.UseWindowsService();

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.MapControllers();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
//app.UseEndpoints(endpoints => { endpoints.MapRazorPages(); });


await app.RunAsync();

这是我的 csproj


<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
      <ImplicitUsings>enable</ImplicitUsings>
      <IsTransformWebConfigDisabled>true</IsTransformWebConfigDisabled>
      <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <UserSecretsId>XXXXX-78E75397-5A01-4397-9481-E423B4BF54C2</UserSecretsId>
  </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
        <PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
        <PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="6.0.0" />
    </ItemGroup>
</Project>

我是这样测试的:

  1. 打开解决方案文件并按 F5。
  2. 运行 dotnet publish --no-build -c Release 从包含 csproj 的文件夹中执行。这会在 XXXXX\bin\Release\net6.0\win-x64 侧生成一个 exe,所以我 运行 通过从命令提示符转到此文件夹然后 运行 exe 运行 来自此文件夹的 exe。

在这两种情况下,我都使用 curl -I http://localhost:5000/swagger/index.html 进行测试。

这 returns 在执行选项 2 时出现 404(使用 dotnet publish)。当从 visual studio 中 运行ning 时,我只得到 200。

如何确保 dotnet publish 的结果与我的应用程序在从 Visual Studio 运行ning 时的行为相同?

在这两种情况下,服务都是 self-hosted。另一种选择是使用 IIS。

禁用 Swagger 的原因是因为代码仅在开发环境中显式启用它。

if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

环境由 ASPNETCORE_ENVIRONMENT 环境变量的值控制。除非您在用户或系统的环境变量中明确设置该环境变量,否则它将不存在,并且您的服务将 运行 处于 Production 模式。

Shell 脚本通常在执行依赖它们的程序之前设置环境变量。 Visual Studio 在启动程序之前设置在 launchsettings.json 中指定的环境变量。如果您尝试 运行 VS 之外的程序,您必须自己设置它们,例如:

SET ASPNETCORE_ENVIRONMENT=Development

myservice.exe

launchsettings.json is used to specify launch profiles, not just environment variables. This includes the hosting model, command line arguments and more. Typically it's used by an IDE like Visual Studio but you can also launch a .NET program with dotnet run 使用带有 --launch-profile <NAME> 选项的特定配置文件

文档说明此文件未部署,因此它从未出现在 publish 文件夹中:

The launchSettings.json file:

  • Is only used on the local development machine.
  • Is not deployed.
  • Contains profile settings.