ARTICLE AD BOX
I'm currently working on an ASP.NET Core Web API project solution that has the following three sub-project directories:
- API/ - Core/ - Persistence/For my project, I want the Core to handle the service and lifecycle of the Persistence. Ideally, I want the API to be nothing but a simple gateway into the Core project - it shouldn't know anything about the Persistence or other sub-projects.
To handle this, I have created a runtime class in 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; } }Along with a service extension class to handle services:
namespace Kumi.Core; public static class KumiServiceExtension { public static IServiceCollection AddKumiRuntime(this IServiceCollection services) { services.AddDbContext<KumiDbContext>(); services.AddHostedService<KumiRuntime>(); return services; } }I then add the Core's runtime to the ASP.NET Core Web API's services in the Program.cs file:
builder.Services.AddKumiRuntime();The Core use the following DbContext from the Persistence project:
namespace Kumi.Persistence; public class KumiDbContext : DbContext { protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseNpgsql("CONNECTION"); base.OnConfiguring(optionsBuilder); } }Issue
Whenever I run the Web API using dotnet run, it works perfectly fine. However, when I go into debug mode, the project builds and then exits with code 0 (0x0):
The program '[17734] Kumi.API.dll' has exited with code 0 (0x0)
I'm not sure why it keeps exiting and not waiting for HTTP connections. I tried building the project and running it manually and it works just as fine.
I'm running the debug in VS Code, I do have the C# Dev Kit extension installed, and have generated launch.json and tasks.json files using the "Generate Assets for Build and Debug" command.
My 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" } ] }My 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" } ] }