Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Liam Dilley 156 posts 382 karma points
    Jan 12, 2023 @ 05:56
    Liam Dilley
    0

    Help needed - Environment setup

    Hi All, I am trying to work out the setup to build to run on a staging server vs local development and a production server.

    The Staging server will have a different database (SQL Server) connection to a Production server.

    I have a an appsettings.json and.. appsettings.Staging.json appsettings.Production.json With a different connection string in other two files.

    I learned in the csproj file you can add the following code...

    <Choose>
    <When Condition="'$(Configuration)' == 'Staging'">
      <ItemGroup>
        <None Include="appsettings.Staging.json" CopyToOutputDirectory="Always" CopyToPublishDirectory="Always" />
        <None Include="appsettings.json" CopyToOutputDirectory="Never" CopyToPublishDirectory="Never" />
        <Content Remove="appsettings.*.json;appsettings.json" />
      </ItemGroup>
    </When>
    <When Condition="'$(Configuration)' == 'Production'">
      <ItemGroup>
        <None Include="appsettings.Production.json" CopyToOutputDirectory="Always" CopyToPublishDirectory="Always" />
        <None Include="appsettings.json" CopyToOutputDirectory="Never" CopyToPublishDirectory="Never" />
        <Content Remove="appsettings.*.json;appsettings.json" />
      </ItemGroup>
    </When>
    <Otherwise>
      <ItemGroup>
        <None Include="appsettings.json" CopyToOutputDirectory="Always" CopyToPublishDirectory="Always" />
        <!--<None Include="appsettings.Staging.json" CopyToOutputDirectory="Never" CopyToPublishDirectory="Never" />-->
      </ItemGroup>
    </Otherwise>
    

    So when I run the publish command there was only the right appsettings file. This did not work because on IIS it is looking for appsettings.json and that is required.

    So I included both and they are there in the published file set. I then tested and edited the appsettings.json in the IIS folder with the wrong database and recycling the app pool I found the appsettings.json is always read. So how do I get different build's to have the different settings?

    JSON example..

    {
      "$schema": "appsettings-schema.json",
      "Serilog": {
        "MinimumLevel": {
          "Default": "Information"
        },
        "WriteTo": [
          {
            "Name": "Async",
            "Args": {
              "configure": [
                {
                  "Name": "Console"
                }
              ]
            }
          }
        ]
      },
      "Umbraco": {
        "CMS": {
          "Content": {
            "MacroErrors": "Throw"
          },
          "Tours": {
            "EnableTours": false
          },
          "Hosting": {
            "Debug": true
          },
          "RuntimeMinification": {
            "UseInMemoryCache": true,
            "CacheBuster": "Timestamp"
          }
        }
      },
      "ConnectionStrings": {
        "umbracoDbDSN": "Server=bert.pretty.local;Database=umbraco11test;User Id=USER;Password=PASSWORD",
        "umbracoDbDSN_ProviderName": "Microsoft.Data.SqlClient"
      }
    }
    

    Can anyone help?

    The goal:

    • Being able to debug and work locally in the office connect to database.
    • Publish for staging and have different settings like debugging on, different database etc.
    • Publish for production and have settings where debug and like are off and a different database connection string.

    I think I am close? There were a few things for .net7 but they are not how Umbraco setup and running on those guides kills Umbraco.

  • Huw Reddick 1757 posts 6123 karma points MVP c-trib
    Jan 12, 2023 @ 09:58
    Huw Reddick
    0

    Hi Liam,

    This is my understanding of how it works (I may be wrong)

    When you Publish your site from VS, there is a setting for configuration, it uses this value to determine what gets written to the appsettings.json file.

    So, if you have an appsettings.Staging.json file and you want these values to be used, te configuration setting for the publish should be set to 'staging' then when you publish, any settings that are in appsettings.staging will overwrite/replace any equivalent settings in the appsettings.json which will be used by the app when running

    I didn't have to make any manual changes to my csprj for this to work, just added a staging configuration to the 'Solution configurations' settings in VS

  • Liam Dilley 156 posts 382 karma points
    Jan 13, 2023 @ 01:33
    Liam Dilley
    0

    I am going through visual studio code and the CLI command but I looked in Visual studio and this did not appear to be the case for me.

    You may need to test that for yourself by making a wrong database name in the connection string in that other settings file and doing that. If you see it all still run it is because it is just using the appsetting.json which is what I have been seeing.

  • Huw Reddick 1757 posts 6123 karma points MVP c-trib
    Jan 13, 2023 @ 06:33
    Huw Reddick
    0

    I don't use visual studio code, I use visual studio and it works how I described above I tested withe different publish configurations and it worked as expected. I will have a look with VSC over the weekend to see if you need to do anything different, but in VS it is the publishing which combines the different settings files into the published appsettings.json that will be used when the site is run.

  • Huw Reddick 1757 posts 6123 karma points MVP c-trib
    Jan 13, 2023 @ 07:38
    Huw Reddick
    0

    It would seem that using the command line and VSC it behaves differently than publishing from VS, odd, I would have expected it to behave the same.

  • Huw Reddick 1757 posts 6123 karma points MVP c-trib
    Jan 13, 2023 @ 17:45
    Huw Reddick
    0

    Hi Liam,

    From doing some research, I believe this is what you need to do.

    On your webserver website you need to set the aspnet core environment variable to Staging

    see here for info https://learn.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-7.0

    Once you have done that, IIS should load your appsettings.Staging.json file

    The default JsonConfigurationProvider loads configuration in the following order:

    1. appsettings.json
    2. appsettings.{Environment}.json : For example, the appsettings.Production.json and appsettings.Development.json files. The environment version of the file is loaded based on the IHostingEnvironment.EnvironmentName. For more information, see Use multiple environments in ASP.NET Core. appsettings.{Environment}.json values override keys in appsettings.json. For example, by default:

    In development, appsettings.Development.json configuration overwrites values found in appsettings.json. In production, appsettings.Production.json configuration overwrites values found in appsettings.json. For example, when deploying the app to Azure.

Please Sign in or register to post replies

Write your reply to:

Draft