Proposal: add Carter as a hosted service in a generic host
Banyc opened this issue · comments
Motivation
REST servers should be treated the same as user defined services. The nowadays usage of the REST functionality heavily relies on WebApplicationBuilder, a web-centric design. But, in fact, the REST server is a remote process interface, no difference than the message queue adaptors for Kafka, RabbitMQ alike or than grpc. In the original
version, the message queue adapters look inappropriately inferior to the mere REST server.
Also, WebApplication is in itself a IHostedService as illustrated here (https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-6.0#:~:text=a%20web%20app%2C-,one%20of,-the%20IHostedService%20implementations) and demonstrated here (https://github.dev/dotnet/aspnetcore/blob/ab80f42b175c8fdace4198a1cdcd3f856b2544e6/src/Hosting/Hosting/src/GenericHostWebHostBuilderExtensions.cs#L61). Aided with the principle of decoupling, It could be another feasible motive to back this proposal.
Desired API
var builder = Host.CreateDefaultBuilder(args);
builder.ConfigureServices((ctx, services) =>
{
// the REST thing
services.AddCarter(configurator: c =>
{
c.WithModule<HomeModule>();
var carterPort = int.Parse(ctx.Configuration["Carter:Port"]);
Console.WriteLine($"Starting Carter on port {carterPort}");
c.WithListenOn(new IPEndPoint(System.Net.IPAddress.Any, carterPort));
});
// the message queue thing
services.AddHostedService<MessageQueueConsumer>();
});
var host = builder.Build();
await host.RunAsync();
Reference
This really isn't a carter request, you want to be able to run asp.net core as a background service. Carter builds on ASP.NET Core and shouldn't need to build anything like this.
You can already do this today by using the existing ASP.NET Core extension method ConfigureWebHost
or ConfigureWebHostDefaults
.