51504

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?


<h3>Answer1:</h3>

I too faced the same problem and solved with this simple change in the project file.

<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>

This will bundle the appsettings.json and other configuration JSON files into the Single file and will be unpacked to the temp location when ran.

Refer here.

来源:https://stackoverflow.com/questions/59366046/how-to-handle-appsettings-for-net-core-3-1-self-contained-single-file-publish

Recommend

  • regex.h in msys2 (windows )
  • How to handle Appsettings for .net core 3.1 self contained single file publish
  • new MailMessage() Throws 'The handle is invalid'
  • Custom configuration settings, custom ClientSettingsSection
  • Reimplementing ToUpper()
  • Disabling predication in gcc/g++
  • Freeform XML configuration section body in app.config
  • Azure classic cloud service cannot RDP
  • What is are the differences between ConfigureServices and Configure in ASP .NET Core?
  • Median of large amount of numbers for each sets of given size
  • Select running balance from table credit debit columns
  • Silent installation of Apache Tomcat
  • Memory leaks when using pthead_exit() to exit thread
  • ExtJS: How to use itemSelector for XTemplate having nested loop?
  • GitLab runner on Windows and dealing with & %ERRORLEVEL%
  • How to change selected item position to top in Recyclerview?
  • Train two models concurrently
  • Unable to create a textclip in moviepy (imagemagick succesfully installed?) - got Utf8 Error
  • Call a specific instance of a service in Azure Service Fabric
  • Python read xml with related child elements
  • What is the correct way to combine two UserControls into one with two states?
  • Spring Mvc submit/delete checked (selected) records from table
  • Using Autofac with AJAX-enabled WCF Service
  • can I build CMakeLists.txt from a set of smaller files (to improve the readability and maintainabili
  • Detect when MathJax has finished loading in UIWebView
  • Vue.js 2: Vue cannot find files from /assets folder (v-for)
  • Python Equivalent of Java's 'Keystore'?
  • How to create mirrored image effect with CSS single element
  • what do lines starting with double-slash '//' mean in a .npmrc?
  • python socket.sendto
  • Reader monad - how does it conform to Monad interface?
  • Unable to run testNG tests from maven
  • Synchronous Calls with jqGrid?
  • How to find angle formed by the blades of a wind turbine with respect to a horizontal imaginary axis
  • Unable to start a WebView from an AsyncTask
  • C# code can't “see” the methods in my C++ dll
  • Copy and paste data from multiple workbooks to a worksheet in another Workbook
  • All Event listing on specified date in Google Calender api (V3) in java?
  • Regex not working in java 1.5
  • Separating definition/instantiation of template classes without 'extern'
  • How to run chrome.tabs.insertCSS from the background page on each page?
  • java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/SpringDispatcher-ser
  • Dynamic XML Schema Validates Subsection of Document
  • How to include associated objects using gon in Rails/jQuery
  • how to get the location(lat/lng) on google maps v3 from the location(x,y)
  • Running R's aov() mixed effects model from Python using rpy2