3

Is there any way to use Ocelot with multiple Configuration files and environments like ocelot.service1.Development.json?

Unfortunately the documentation seems to be outdated and also it doesn't handle my specific request. I saw that it is able to have multiple files that will be merged Documentation:

ocelot.service1.json
ocelot.service2.json

//Program.cs
return WebHost.CreateDefaultBuilder(args)
    .ConfigureAppConfiguration((host, config) => {
        config
            .AddOcelot(host.HostingEnvironment)
            .AddEnvironmentVariables();
    })
    .UseStartup<Startup>();
}

It works but doesn't meet my requirements.

The documentation also describes following setup Documentation:

ocelot.Development.json
ocelot.Staging.json

//Program.cs
return WebHost.CreateDefaultBuilder(args)
    .ConfigureAppConfiguration((host, config) => {
        config
            .AddJsonFile("ocelot.{host.HostingEnvironment.EnvironmentName}.json", true, true)
            .AddEnvironmentVariables();
    })
    .UseStartup<Startup>();
}

But this solution also doesn't meet my requirements.

Is there any way how I can combine both ways?

SNO
  • 793
  • 1
  • 10
  • 30
  • Yes, it is possible, but what is your requirements? Show us what files you have – Roman Marusyk Jan 31 '20 at 08:57
  • I guess I found another solution that fits to my needs. I excluded the config file from project and put it directly on the server. Due to the high number of ReRoutes I wanted to split them into several files. Each of that files should be environment specific. – SNO Jan 31 '20 at 09:18

1 Answers1

1

I've come up with a solution:

I edited the extension method .AddOcelot(folder, env). I've added a new parameter appNames which is an array of the names in the files to read from appName.ocelot.Dev.json to find the files.

Screenshots:

  1. program.cs
  2. File structure
  3. Modified Extension
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Arthur Muller
  • 61
  • 2
  • 10
  • 1
    Can you edit your post to paste code as _text_? You can then _format_ it as code. This will make it easier for people to read and reference. We like to avoid _screenshots_ of code on Stack Overflow. Thank you in advance. – Jeremy Caney Oct 04 '21 at 00:57