Cocona icon indicating copy to clipboard operation
Cocona copied to clipboard

Integrate with ASP.NET Core

Open stevefan1999-personal opened this issue 6 months ago • 3 comments

Is there anyway to not use the ASP.NET Core CLI parser but Cocona instead, as it would greatly help to integrate https://github.com/buehler/dotnet-operator-sdk to use just one hosted app instead of making two.

stevefan1999-personal avatar Dec 09 '23 07:12 stevefan1999-personal

This example works:

Program.cs:

var builder = CoconaApp.CreateBuilder(args);
builder.Services.AddSingleton<EmailService>();

var app = builder.Build();
app.AddCommands<EmailCommand>();
app.AddCommand(ExecuteAspNetCore);
app.Run();

void ExecuteAspNetCore()
{
  var builder = WebApplication.CreateBuilder(args);
  builder.Services.AddEndpointsApiExplorer();
  builder.Services.AddSwaggerGen();
  builder.Services.AddControllers();
  builder.Services.AddSqlServer<MyDbContext>(
    builder.Configuration.GetConnectionString(MyDbContext.ConnectionStringName));

  var app = builder.Build();
  app.MapControllers();
  app.UseSwagger();
  app.UseSwaggerUI(options => options.EnableTryItOutByDefault());
  app.Run();
}

ArwynFr avatar Feb 12 '24 13:02 ArwynFr

This example works:

Program.cs:

var builder = CoconaApp.CreateBuilder(args);
builder.Services.AddSingleton<EmailService>();

var app = builder.Build();
app.AddCommands<EmailCommand>();
app.AddCommand(ExecuteAspNetCore);
app.Run();

void ExecuteAspNetCore()
{
  var builder = WebApplication.CreateBuilder(args);
  builder.Services.AddEndpointsApiExplorer();
  builder.Services.AddSwaggerGen();
  builder.Services.AddControllers();
  builder.Services.AddSqlServer<MyDbContext>(
    builder.Configuration.GetConnectionString(MyDbContext.ConnectionStringName));

  var app = builder.Build();
  app.MapControllers();
  app.UseSwagger();
  app.UseSwaggerUI(options => options.EnableTryItOutByDefault());
  app.Run();
}

That's exactly the problem. First you have two sets of hosted builders and doesn't share with each other, and so some services are often duplicated, and that also means I possibly have to manage two builders like config hot reload and stuff...

stevefan1999-personal avatar Feb 12 '24 14:02 stevefan1999-personal

Also, its not compatible with WebApplicationFactory

ArwynFr avatar Feb 12 '24 15:02 ArwynFr