47013

# How to handle Appsettings for .net core 3.1 self contained single file publish

<h3>Question</h3>

I have a new .NET Core 3.1 worker class that is hosted as a Windows Service. I am using the default appsettings.json and appsettings.environment.json that were created by the template. The appsettings is loaded from the hostContext during ConfigureServices

.ConfigureServices((hostContext, services) => { services.AddHostedService<Worker>(); services.Configure<BasicSettings>(hostContext.Configuration.GetSection("AppSettings")); }); 

I want to be able to edit the appsettings after it is deployed so that I can change settings in production. It works correctly during debugging on my machine. I updated the csproj file to have the following code to try and make the appsettings.json not get included in the Single file.

 <None Include="appsettings.json"> <CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToPublishDirectory>Always</CopyToPublishDirectory> <ExcludeFromSingleFile>true</ExcludeFromSingleFile> </None> <None Include="appsettings.Development.json;appsettings.Production.json;"> <CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToPublishDirectory>Always</CopyToPublishDirectory> <DependentUpon>appsettings.json</DependentUpon> <ExcludeFromSingleFile>true</ExcludeFromSingleFile> </None> </ItemGroup> 

After adding this the publish process does create the single exe as well as the 3 appsettings.json files but does not solve it.

When the windows service starts up it expands the single exe to the folder C:\Users\ServiceLogonUser\AppData\Local\Temp.net\ServiceName\SomeRandomThing and this has the appsettings.json that exists in the project at publish. Not the appsettings.json that is copied next to the exe. If I delete this folder, it is recreated but again with the appsettings.json that existed at publish. How with a single exe publish can it read the appsettings.json from the same folder so that the file can be edited in after publish?

<None Include="appsettings.json"> <CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToPublishDirectory>Always</CopyToPublishDirectory> <ExcludeFromSingleFile>false</ExcludeFromSingleFile> </None> <None Include="appsettings.Development.json;appsettings.Production.json;"> <CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToPublishDirectory>Always</CopyToPublishDirectory> <DependentUpon>appsettings.json</DependentUpon> <ExcludeFromSingleFile>false</ExcludeFromSingleFile> </None>