0

I have a library that makes use of the old style appSettings in app.config configuration file. For example:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="exampleKey" value="exampleValue" />
  </appSettings>
</configuration>

I realise this is not good practice, but I cannot modify this library and need to use it as is.

In a .Net Core project (I'm trying this in a Unit Test project, but also need it to work in an Asp.Net Web API project), how do I configure an application configuration file so that when the library attempts to load its appsettings (presumably with a ConfigurationManager.AppSettings command) it loads them correctly? Is this possible?

John Darvill
  • 1,274
  • 11
  • 17
  • Are you sure that is the only thing in that library that is dependent on .NET Framework? I don't know if it is practical for you but you could wrap the library with a web service and call it via RPC instead of in-process. – Crowcoder Dec 24 '19 at 12:19
  • Does this answer your question? [Is ConfigurationManager.AppSettings available in .NET Core 2.0?](https://stackoverflow.com/questions/47591910/is-configurationmanager-appsettings-available-in-net-core-2-0) – Federico Dipuma Dec 24 '19 at 12:27
  • @FedericoDipuma - thanks for the link. Not exactly, I don't think. I have installed the System.Configuration.ConfigurationManager NuGet package, but my "web.config" file in my Web Application isn't being used at runtime. Instead a file in my user directory ending "Url_vs0cocxwpbys4l4e3gswszew4ioczhkr\\1.0.0.0\\user.config" is used. – John Darvill Dec 24 '19 at 14:31
  • Ah! I think I have it partially working, thanks to this answer: https://stackoverflow.com/questions/40186445/access-web-config-settings-in-asp-net-core-app – John Darvill Dec 24 '19 at 14:41

1 Answers1

0

I did manage to piece together the answers to this from other Stack Overflow threads, but just in case anyone comes across this:

In the actual web application, this was solved just by creating an app.config file (that sits alongside any web.config or appsettings.json files you also have) and setting its Build Action to Content and Copy if newer. For more info see this thread.

In the unit test project, this was solved by creating an app.config in the same way as above, but also adding a build action in the csproj file instructing the file to be copied to the output directory and renamed to testhost.dll.config. For more info see this thread.

John Darvill
  • 1,274
  • 11
  • 17