2

In ASP.NET 4, I was able to use app.config XML to transform my variables with Octopus variables.

<appSettings xdt:Transform="Replace">
    <add key="LogLevel" value="#{Project.LogLevel}"/>

I have been trying to look around for a similar solution for .NET Core, but have only been able to find suggestions for the appropriate environment. I would like to avoid having different .config files for each environment (app.production.json, app.staging.json), I would just like to have another app.release.config file.

From my understanding of using the different environments, my json would look like this:

{
    "LogLevel" : Debug
}

And it will replace the original value in my appsettings.json. However, like I mentioned, I don't want to have to go into the file to make variable changes.

Is there a functionality in .NET Core with something more flexibility like this?

{
    "LogLevel" : #{Project.LogLevel}
}
Tseng
  • 61,549
  • 15
  • 193
  • 205
m1771vw
  • 703
  • 2
  • 11
  • 21

2 Answers2

0

enter image description hereWell, one way of doing it is to have two separate appsettings.json files for each environment with the corresponding LogLevel in each.

Now on Octo, create a variable named Environment set the value and apply appropriate scope (development/staging/production), and on *Target Files* set it to something like appRoot\appsetting.#{Environment}.json

Useful Link http://docs.octopusdeploy.com/display/OD/Deploying+ASP.NET+Core+Web+Applications

Assuming your app code and or appsettings looks something like this for local debugging purpose

appSettings.Development.json

{
  "LogLevel":"Debug"
}

appSettings.Production.json

{
  "LogLevel":"Trace"
}

namespace MySampleApp
{
    public static void Main(string[] args)
    {
        var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");          

        var builder = new ConfigurationBuilder()
            .AddJsonFile($"appsettings.json", true, true)
            .AddJsonFile($"appsettings.{environmentName}.json", true, true)
            .AddEnvironmentVariables();
       //set the ASPNETCORE_ENVIRONMENT on octo
        Configuration = builder.Build();
    }   
}

Edit

An alternate way is you could move all config settings to Web.config file - Disclaimer : I havent tried it personally - as I followed the method above - to have different appSetting.json file. I am saying this based on the fact that I could add an xml file for Swagger setting, hence suggesting that it could work for web.config file as well.

 var builder = new ConfigurationBuilder().AddXmlFile("Web.config") 
Jaya
  • 3,721
  • 4
  • 32
  • 48
  • Is there something similar to XDT Transform for JSON files in Octopus? Would like to use Octopus to change the variables rather than having multiple environment JSON files that have different variables hard coded in. Or is this way the only way to change the variables right now? – m1771vw Dec 05 '16 at 17:59
  • I haven't personally tried this, but you could move all variables to web.config files and use the `AddXMLFile` in your `startup.cs` - will update my answer above to reflect the same – Jaya Dec 05 '16 at 19:23
  • 1
    After looking into it more (will need to test to see if this is what I need), but Octopus currently has JSON Transforms implemented -- where if you set an Octopus variable "foo" to a value "bar", it will be written into the target JSON file as `{ "foo":"bar"}`. [This](http://docs.octopusdeploy.com/display/OD/JSON+Configuration+Variables+Feature) was actually at the bottom of the link you provided, didn't quite understand it until I read it again today, but I do think this is what I was looking for! Will also update answer later if this is works out. – m1771vw Dec 05 '16 at 19:36
  • Sure, makes sense, when we started with octo deploying core projects about 6 months ago they did not have any support for the same, hence used the one I suggested first.. Good to know.. glad you figured this one out! – Jaya Dec 05 '16 at 20:25
0

I recently went through a similar process, converting over to .Net Core. I created environment based JSON files e.g. appsettings.development.json, appsettings.production.json. I then pass in the environment at runtime to selectively load the correct config at runtime - using config.AddJsonFile. The last matching key wins therefore you are achieving what is essentially a partial transform (As described here https://stackoverflow.com/a/37874308/1001408). I also don't store any sensitive config values in the transform files so I am then able to use the Octopus Deploy variable substitution without fear of overwriting any values at runtime.

Mark Walsh
  • 3,241
  • 1
  • 24
  • 46