Reload configuration when env variable has changed

前端 未结 2 1209
逝去的感伤
逝去的感伤 2021-01-05 04:42

In Startup.cs file I have

public Startup(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
           


        
相关标签:
2条回答
  • 2021-01-05 05:00

    so what you want to do is read weather a file has changed or not during runtime? you can use a FileSystemWatcher to detect when the file has been changed or edited, so in a sense you can, but detecting weather or not a specific variable has changed is a bit more messy, because you'd only be able to detect when the entire file has had a change to it, and then check if the wanted var has changed yourself.

    FileSystemWatcher MSD

    but if we're talking about the app reciving a new environment change during runtime, you can use a hacky solution, using a NamedPipeStream you could send environment changes by either starting another process to tell the main one to update Var X to Y or by like mentioning earlier but instead having a sub process detecting it and sending it back up the chute.

    NamedPipeStream Server MSD NamedPipeStream Client MSD

    0 讨论(0)
  • 2021-01-05 05:07

    Are there any ways to change a value of env variable at runtime (without restarting docker container)

    No. (And even a restart isn't enough: you need to delete and recreate the container.)

    This follows the ordinary Unix model. A process can set the initial environment for its child process, but once it's exec'd the child, it has no more control over the environment any more. docker exec launches a new process in the container namespace and so if you change an environment variable there it will only affect that process and not the main container process.

    There are a significant number of options that can only be set during the initial docker run command. This includes environment variables, and also includes volume mounts and published ports. Critically, it also includes the underlying image: if you ever have a new build of your application, or need to update the underlying OS distribution for a security issue, you will be forced to delete and recreate your container. In my experience docker rm is extremely routine, and you should plan for it to happen regularly.

    0 讨论(0)
提交回复
热议问题