JasperFx / oakton

Parsing and Utilities for Command Line Tools in .Net
http://jasperfx.github.io/oakton
Apache License 2.0
308 stars 41 forks source link

Cannot set environment because IHost is already constructed in .NET 6 web app #75

Open azborgonovo opened 1 year ago

azborgonovo commented 1 year ago

Symptom

Running a command like dotnet run -- db-patch file.sql --environment Staging results in the following error:

image

Context

I tried to set up my .NET 6 web application as described in the docs. In summary, my Program.cs file looks like the following:

var builder = WebApplication.CreateBuilder(args);
builder.Host.ApplyOaktonExtensions();
// ... other changes in the host builder (e.g. builder.Services.AddAuthentication())
var app = builder.Build();
// ... changes in the app (e.g. app.UseAuthentication())
return await app.RunOaktonCommands(args);

My development environment is a macOS with Apple silicon.

azborgonovo commented 1 year ago

There is a workaround/containment that can be used locally (at least for setting up env variables).

Set the environment with the launchprofile instead of via --environment parameter:

dotnet run --launch-profile Staging -- db-patch file.sql

agross commented 1 year ago

That workaround unfortunately does not work for me. I receive the same "is already constructed" as above.