Swashbuckle.aspnetcore: CLI: Allow setting environment at runtime

Created on 31 Aug 2018  路  9Comments  路  Source: domaindrivendev/Swashbuckle.AspNetCore

I would like the ability to specify the environment of the API when running the CLI to control which appsettings files get used. Currently, it defaults to Production, but our production values are tucked away into secrets and loaded outside the appsettings file. I'd like to point it to Development.

dotnet run has the ability to set environment via --environment and has the ability to set environment variables in general using =. It also can pull the ASPNETCORE_ENVIRONMENT from the launchSettings.json

I can still set ASPNETCORE_ENVIRONMENT at the user/system level, but I'd prefer the flexibility of one of these other options.

Most helpful comment

So it turns out the dotnet run call just takes in additional arguments which are then added to the app configuration object. You can see this option here
image

ASPNETCORE_ENVIRONMENT just happens to be translated to the environment configuration option. So by using --environment Development or environment=Development at the end of the dotnet run command, you are setting the environment configuration. Adding --foo bar or foo=bar would set foo to bar in the configuration usable during startup.

The additional flexibility comes from trying to use the command inside the csproj file like so
<Target Name="SwaggerToFile" AfterTargets="AfterBuild"> <Exec Command="dotnet swagger tofile --output $(OutputPath)swagger.json $(OutputPath)$(AssemblyName).dll v1" /> </Target>
For whatever reason, I can't have two commands (e.g. set = && dotnet swagger tofile) in the same command which means I can't set the environment variable this way, hence why I'd like it as part of the actual tofile command.

It is a niche case, but I figured I'd ask.

All 9 comments

I don't see --environment as an option for dotnet run:

screen shot 2018-09-04 at 10 53 04 pm

Also, I don't understand how the introduction of such a parameter would provide any more flexibility over passing ASPNETCORE_ENVIRONMENT as an environment variable to the command as follows:

> ASPNETCORE_ENVIRONMENT=development dotnet swagger tofile --output wwwroot/api-docs/v1/swagger.json bin/Debug/netcoreapp2.0/MyAPI.dll v1

So it turns out the dotnet run call just takes in additional arguments which are then added to the app configuration object. You can see this option here
image

ASPNETCORE_ENVIRONMENT just happens to be translated to the environment configuration option. So by using --environment Development or environment=Development at the end of the dotnet run command, you are setting the environment configuration. Adding --foo bar or foo=bar would set foo to bar in the configuration usable during startup.

The additional flexibility comes from trying to use the command inside the csproj file like so
<Target Name="SwaggerToFile" AfterTargets="AfterBuild"> <Exec Command="dotnet swagger tofile --output $(OutputPath)swagger.json $(OutputPath)$(AssemblyName).dll v1" /> </Target>
For whatever reason, I can't have two commands (e.g. set = && dotnet swagger tofile) in the same command which means I can't set the environment variable this way, hence why I'd like it as part of the actual tofile command.

It is a niche case, but I figured I'd ask.

Just ran into this missing option to set environment. Seems to be needed when using the Manage User Secrets feature of VS, then without the environment = Development setting, the "dotnet swagger tofile..." command embedded in a .csproj fails if you use config properties for oauth2 setup in the Startup.cs.

I have a similar use case where we wish to use the CLI to create contract diffs during PRs. Unfortunately we access secrets in our Startup.cs file that are not available at build time and are only available when we deploy. This causes a particular piece of our Startup to error because it's attempting to use a configuration variable that's null.

Being able to specify configuration options similar to the Environment would allow us to use the CLI without making code changes.

I have the same use case as persalmi. I'm using Manage User Secrets with a script in the .csproj file

<Target Name="SwaggerToFile" AfterTargets="AfterBuild">
    <Exec Command="dotnet swagger tofile --output swagger.json $(TargetPath) v1" />
</Target>

I'm getting an error that settings are null.

I finally able to make it work

Set env variable like this
image

Notice there is NO && ^ between setting env variable and calling dotnet swagger

Within the project, dont forget to put the appsettings.Development.json

image

If using the CLI as part of you build pipelines on Azure DevOps, you can do it like this:

- task: CmdLine@2
  displayName: 'Generating swagger json'
  inputs:
    script: |
      set ASPNETCORE_ENVIRONMENT=Development
      swagger tofile --serializeasv2 --output swagger.json MyService.Dll 1.0
    workingDirectory: <your dotnet publish directory>

@nascosto - it seems there's very reasonable solutions here, can this be closed?

Yeah. Let's close this for now. Thanks.

Was this page helpful?
0 / 5 - 0 ratings