Reading from app.config in .NET Core application referencing .NET Standard library - .net-core

I'm converting over some existing code from .NET Framework to .NET Standard for use in .NET Core apps. The library project has been converted over and is building fine. It contains some database access code that uses EF Core. Due to the legacy nature of the consumers for this code I wanted to continue using the App.Config or Web.Config files with Configuration Manager. To do this I added the System.Configuration.ConfigurationManager NuGet package to the library project.
The library is validated with a unit test project that uses MSTest. The project was targeting .NET Framework 4.7.1, which could consume the .NET Standard 2.0 library package. When targeting .NET Framework, all of the unit tests were passing.
After changing the unit test project to target .NET Core 2.0, the database code is no longer able to find the connection strings stored in the App.Config file of the unit test project. When I debug the test and inspect the ConfigurationManager.ConnectionStrings collection, I only see one defined that appears to be a SQL Express connection likely coming from the Machine.Config file.
Has anyone had success accessing App.Config from a .NET Core app passing through a .NET Standard library with the ConfigurationManager compatibility library?

Still there is a known issue in Microsoft.Net.Test.Sdk and it is because when you use ConfigurationManager in test applications using .Net Core, ConfigurationManager is looking for testhost.dll.config and not your standard assembly config file.
There is a non-pleasant workaround for this issue based on this discussion in github which you can copy your App.Config file into your output directory with name testhost.dll.config by putting this element in your test csproj file:
<Target Name="CopyCustomContent" AfterTargets="AfterBuild">
<Copy SourceFiles="App.config" DestinationFiles="$(OutDir)\testhost.dll.config" />
</Target>
And then you can use ConfigurationManager.OpenMappedExeConfiguration to load your config file.
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = "testhost.dll.config";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
And for example for reading an appsetting you can do like this:
var setting = Config.AppSettings.Settings[key];

Related

App.config not loading into dotnet core nunit project

I have a testing project that I'm running on netcore 3.0, referencing a framework library. The library uses ConfigurationManager to access app settings from App.config. I have an App.config in the root of the testing library and it's compiling into the bin/Debug/netcoreapp3.0 folder without a problem; I can see a file called Tests.dll.config there. However, it's not loading into the project, and when I try calling Configuration.AppSettings.AllKeys I get nothing.
I've been trying to do some research but the only solution that is being suggested is to ensure that there is an App.config file in the root of the test project (see here). I do have this file, it's compiling fine, but it's not getting used. What can be the problem?

Convert .Net Framework 4.6.2 project to .Net core project

I Have a solution which contains the bunch of class libraries which is developed by .Net framework 4.6.2. I have to convert those class libraries into .Net core. Is there any best and fastest way to convert instead for rewrite the code.
This appears to be an official Microsoft resource for doing the migration. Summarized below:
(recommended) Retarget all projects you wish to port to target the .NET Framework 4.7.2 or higher.
(recommended) Use the .NET Portability Analyzer to analyze your assemblies and see if they're portable to .NET Core.
(recommended) Install the .NET API analyzer into your projects to identify APIs throwing PlatformNotSupportedException on some platforms and some other potential compatibility issues.
Convert all of your packages.config dependencies to the PackageReference format with the conversion tool in Visual Studio.
Create new projects for .NET Core and copy over source files, or attempt to convert your existing project file with a tool.
Port your test code.
Most of BCL is still the same API-wise, so conversion is definitely viable for consideration. Yes, there may be incompatibilities in your code (or more often - with your dependencies) and the easiest way to check is to try building it with .net core.
For more details about when to convert (and when to rewrite) or about options of performing the conversion you could follow this guide: Upgrading to .NET Core and .NET Standard Made Easy.
The easiest way to switch a .net framework project to a .netcore project is to open the csproj file and change the TargetFramework from something like this
<TargetFramework>net462</TargetFramework>
to something like this
<TargetFramework>netcoreapp3.1</TargetFramework>
You could also change it to .net standard, in case you want compatibility between .net core and .net framework consumer projects, by changing it to this:
<TargetFramework>netstandard2.0</TargetFramework>
You could target multiple frameworks like so:
<TargetFrameworks>net462;netstandard2.0</TargetFrameworks>
Ensure you use the correct version number and obviously depending on what this project already targets, things are going to break and will need fixing. For example, you can't use a .net framework class library with a .net core project.
A more detailed process is provided here:
https://learn.microsoft.com/en-us/dotnet/core/porting/
I just ran into the same error that you saw (as per your comment to #ImrePĆ¼hvel) when I was trying to migrate a CLI project from .NET Framework to netcoreapp3.1:
"The expression "[Microsoft.Build.Utilities.ToolLocationHelper]::GetPathToStandardLibraries(_, netcoreapp3.1, '', x64, '', '')" cannot be evaluated. Input string was not in a correct format. C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets"
In my case, it was due to a misreading of the instructions.
The old framework had a tag:
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
This needs to be changed to:
<TargetFramework>netcoreapp3.1</TargetFramework>
NOT
<TargetFrameworkVersion>netcoreapp3.1</TargetFrameworkVersion>
I had simply changed v4.5 --> netcoreapp3.1 in the TargetFrameworkVersion tag without changing the tag name to TargetFramework.
So double-check that you changed:
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
to
<TargetFramework>netcoreapp3.1</TargetFramework>
(or whatever .NET Core version you want)
and NOT:
<TargetFrameworkVersion>netcoreapp3.1</TargetFrameworkVersion>

Getting xunit to supply app.config in .NET Core 2.0

I'm trying to get SpecFlow to run under .NET core, using xunit as the test runner. As part of the operation of the test, SpecFlow loads its configuration with a call to ConfigurationManager.GetSection("specFlow").
The project references the System.Configuration.ConfigurationManager package to enable this call under .NET Core, however it seems to want to load configuration from my NuGet cache.
.nuget\packages\microsoft.testplatform.testhost\15.3.0\lib\netstandard1.5\testhost.dll.config
As a result, the SpecFlow configuration section is not found and it falls back to trying to load an NUnit component (which naturally fails).
Is there a way to get the ConfigurationManager to pick up configuration from a different location, so calls to GetSection will return the expected configuration values?

.net Framework to .net Core

Im doing a project with .net Framework , but to run on the linux i need .net Core..
I heard that it is possible to change .net Core to .net Framework only with changes on the project.csproj with this:
<TargetFramework>netcoreapp1.1</TargetFramework>
to
<TargetFramework>v4.5.2</TargetFramework>
and i tried to do the opposite , but i got some erros on the project...
There's a way to do that?
Thanks!
You can not switch a .NET Framework project to .NET Core that easy.
You might change an empty project but not one you already started coding.
Possibly you'll need to rewrite or start from scratch.
There are some major differences between two
Format of configuration files (web.config vs appsettings.json)
See this post on providing backward compatibility for App.config, appSettings.config and Web.config XML-based configuration providers
Used libraries
Startup files (Global.asax vs Startup.cs)
Lack of static objects in .Net Core. Like Session and Application objects -
which is a good thing btw.
Many of the .Net Framework libraries are dependant on
app.config/web.config files

.NET Core Class Library with NLog targeting .Net 4.6.1

I'm currently creating a common library from my work that would implement an abstracted logger using NLog.
I've created a .NET Core class library targeting .NET 4.6.1 and implemented NLog, but when I try to execute a unit test that I've created, I've noticed that the nlog.config file is not being picked up. I've also followed the instruction stated https://github.com/NLog/NLog.Extensions.Logging, but I've noticed that it only targets Asp.Net Core and not for .NET Core Class Library solution.
My question is, (given that it is my first time to foray to .NET core) is the instructions stated on the link above applicable for .Net Core class libraries too, or is there a different way to implement this?
Or should I end up not using .Net Core and go for more traditional .NET Class library implementation instead?
Many thanks!
I've created a .NET Core class library targeting .NET 4.6.1 and implemented NLog, but when I try to execute a unit test that I've created, I've noticed that the nlog.config file
It difficult to find the config file when running as unit test. Different frameworks are using different locations. The following options should work
Manually find the config file location as pass it to NLog. Use
LogManager.Configuration = new XmlLoggingConfiguration(filePath, true);
instead of ConfigureNLog() , or
Create a config with the API, or
Create the XML as string and feed it to NLog: instead of ConfigureNLog, use
XElement element = XElement.Parse(configXml);
LogManager.Configuration = new XmlLoggingConfiguration(element.CreateReader(), "nlog.config");
I've noticed that it only targets Asp.Net Core and not for .NET Core Class Library solution.
It works with or without ASP.NET. The ASP.NET parts will be moved to the NLog.Web package in the future

Resources