0

I have an ASP.NET Core application running in Docker and I need to pass to the container (at start) a bool variable whether on not to apply the migration.

For example:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
  if(newDB)
    Seed(true);
  else
    Seed(false);

 ...
}

1 Answer 1

1

You can use a configuration variable which value will be overridden an by environment variable on run.

public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
           .SetBasePath(env.ContentRootPath)
           .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
           .AddEnvironmentVariables("APP_");

        Configuration = builder.Build();
    }

public IConfigurationRoot Configuration { get; }

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
   var newDB = Convert.ToBoolean(Configuration["NewDB"]));
(...)

appsettings.json:

{
  "Logging": {
    (..)
   },
  "NewDB": false
}

And when running container pass: docker run -e APP_NewDB='true' ...

Sign up to request clarification or add additional context in comments.

4 Comments

I get an exception saying Unhandled Exception: System.FormatException: Could not parse the JSON file. Error on line number '27': '},....
are you not missing any comma in your json file? Please share it if you can.
{ "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Warning" } }, { "NewDB": false } }
You sholdn't use another braces around the newDB param - braces indicate another struct and you didn't name it, hence the error. Correct one: { "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Warning" } }, "NewDB": false }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.