调试时,ASP.NET Core Web API总是以退出码0(0x0)退出
我目前正在处理一个ASP.NET Core Web API项目解决方案,它包含以下三个子项目目录:
- API/
- Core/
- Persistence/
对于我的项目,我希望 Core 来处理 Persistence 的服务与生命周期。理想情况下,我希望 API 只是进入 Core 项目的一个简单网关——它不应该知道关于 Persistence 或其他子项目的任何事情。
为了解决这个问题,我在 Core 中创建了一个运行时类:
namespace Kumi.Core;
public class KumiRuntime(IServiceProvider services) : IHostedService
{
public async Task StartAsync(CancellationToken cancellationToken)
{
using var scope = services.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<KumiDbContext>();
await db.Database.MigrateAsync(cancellationToken);
}
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}
以及一个用于处理服务的服务扩展类:
namespace Kumi.Core;
public static class KumiServiceExtension
{
public static IServiceCollection AddKumiRuntime(this IServiceCollection services)
{
services.AddDbContext<KumiDbContext>();
services.AddHostedService<KumiRuntime>();
return services;
}
}
随后我将 Core 的运行时添加到ASP.NET Core Web API的服务中,在 Program.cs 文件中:
builder.Services.AddKumiRuntime();
Core 使用来自 Persistence 项目的以下 DbContext:
namespace Kumi.Persistence;
public class KumiDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseNpgsql("CONNECTION");
base.OnConfiguring(optionsBuilder);
}
}
问题
每当我使用 dotnet run 运行Web API时,一切正常。然而进入调试模式后,项目会先构建,然后以退出码0 (0x0) 退出:
程序 '[17734] Kumi.API.dll' 已以退出码0 (0x0) 退出
我不确定为什么它会持续退出,而不是等待HTTP连接。我尝试手动构建并运行该项目,结果也同样正常。
我在VS Code中进行调试,确实安装了C# Dev Kit扩展,并使用“为构建和调试生成资源”命令生成了 launch.json 和 tasks.json 文件。
我的 launch.json:
{
"version": "0.2.0",
"configurations": [
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/dotnet/vscode-csharp/blob/main/debugger-launchjson.md.
"name": ".NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/Kumi.API/bin/Debug/net10.0/Kumi.API.dll",
"args": [],
"cwd": "${workspaceFolder}/Kumi.API",
"stopAtEntry": false,
// Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser
"serverReadyAction": {
"action": "openExternally",
"pattern": "\\bNow listening on:\\s+(https?://\\S+)"
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"sourceFileMap": {
"/Views": "${workspaceFolder}/Views"
}
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
}
]
}
我的 tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/kumi.slnx",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary;ForceNoAlign"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/kumi.slnx",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary;ForceNoAlign"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"--project",
"${workspaceFolder}/kumi.slnx"
],
"problemMatcher": "$msCompile"
}
]
}
解决方案
在进一步排查问题后,我发现似乎.NET 10.0.2在 macOS上存在调试问题:
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。