2

I have tried about twenty solutions I have found online on how to prevent appsettings.{somevalue}.json from being published with the wrong environment. Basically I have a "development" variant and "production" variant and both are set to Copy If Newer under properties in the project. I have 2 publish profiles publishing a Worker Service app to a remote directory, one for debug configuration to my development environment (where the DOTNET_ENVIRONMENT is set to Development) and one to a similar production environment. I want to exclude the opposite environments config file from the publish or build automatically. Simple solution is to delete the file myself, however I want to make sure this is automated when published. Part of the issue is the complete lack of information on what goes in these pubxml files or csproj file that allows you to remove or delete files. Other questions on stack exchange have noted this lack of information as well.

Here is what I have recently tried and I have tried placing these blocks in both pubxml files and csproj file both inside and outside of the PropertyGroup node but none of these work:

1.

    <Target Name="Debug" AfterTargets="AfterPublish">
        <Message Text="Development Publish Message"></Message>
        <Delete Files="appsettings.Production.json" /> 
    </Target>

(I have also tried specifying variables for that json file's location as well as hardcoding its actual path in the final publish. This does nothing and that message isn't shown anywhere)

2.

    <ItemGroup>
        <Content Remove="appsettings.Production.json" />
    </ItemGroup>
    <ExcludeFilesFromDeployment>
       bin\Debug\netcoreapp3.1\appsettings.Production.json;
    </ExcludeFilesFromDeployment>
    <ItemGroup>
        <Content Update="appsettings.Production.json" CopyToPublishDirectory="Never" />
    </ItemGroup>
(this one is straight from Microsoft but like many others, does nothing)
https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/visual-studio-publish-profiles?view=aspnetcore-6.0
    <Choose>
        <When Condition="'$(Configuration)' == 'Debug'">
          <ItemGroup>
            <None Include="appsettings.Production.json" CopyToOutputDirectory="Never" CopyToPublishDirectory="Never" />
          </ItemGroup>
        </When>
        <When Condition="'$(Configuration)' == 'Release'">
            <ItemGroup>
                <None Include="appsettings.Development.json" CopyToOutputDirectory="Never" CopyToPublishDirectory="Never" />
            </ItemGroup>
        </When>
    </Choose>

I could continue but nothing I have found works. Is this even possible?

Thanks!

Shubius
  • 23
  • 4

0 Answers0