Can't use System.Configuration.Configuration manager in a .NET Standard2.0 library on .NET FX4.6
Asked Answered
R

4

67

I have an assembly created in NetStandard2.0. It reads AppSettings using System.Configuration.ConfigurationManager. I have installed nuget package of System.Configuration.ConfigurationManager with version 4.4.X which is suitable for NetStandard2.0.

When I refer this assembly in console app (.Net Core) it is reading AppSettings properly, but when I refer this assembly in old .NetFramework(4.6.X) console app it is not working and throwing an exception.

Please see the code below.

Assembly 1: NetStandard 2.0

Nuget: System.Configuration.ConfigurationManager 4.4.0

using System.Configuration;

namespace Bootstrapper.Lib
{
   public class Bootstrapper
   {
     public Bootstrapper()
     {

     }

     public void LoadAppSettings()
     {
         string serachPattern= 
         ConfigurationManager.AppSettings["AssemblySearchPattern"];
     }
  }

}

Console App: NetFx 4.6.X

using System;
using Bootstrapper.Lib;
namespace Bootstrapper.Console
{
  class Program
  {
    static void Main(string[] args)
    {
        new Bootstrapper().LoadAppSettings();
    }
  }
}

Exception After Run:

'Could not load file or assembly 'System.Configuration.ConfigurationManager, 
 Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one 
 of its dependencies. The system cannot find the file specified.'

It will work with Console App developed using .NetCore.

Please help!!!

Rufus answered 22/9, 2017 at 8:57 Comment(4)
Microsoft has just announced a "Windows Compatibility Pack for .NET Core". That, among other things, contains System.Configuration.ConfigurationManager (but turns your .NET core app "Windows only" in turn). I don't know if it is already available. Just google for the term in quotes and see what you can find out.Trumpetweed
Did you enable automatic binding redirect generation as described in github.com/dotnet/announcements/issues/31 ?Les
@MartinUllrich Yes, I tried it Today but getting the same exception.Rufus
Solved it!. just added nuget of System.Configuration.ConfigurationManager 4.4.0 to console applicationRufus
N
78

As @kiran mentioned in a comment you can solve this by running:

Install-Package System.Configuration.ConfigurationManager

in NuGet Package Manager

Nonalignment answered 19/6, 2018 at 22:46 Comment(3)
@Xander In my case, I installed it in NetFramework(4.6.X) console app.Rufus
You can install this nugget package manager from here nuget.org/packages/System.Configuration.ConfigurationManager/… All instructions on how to install it are there. As of this time you will also need to have NETFramework 4.6.1 and above.Blancablanch
Any idea why this happens? I understand the solution but don't understand which component is declaring dependency on that assemblyLinneman
R
16

It is not possible to create .NET Standard library which references System.Configuration.ConfigurationManager package and uses ConfigurationManager class. Once library adds reference to .NET Core specific package it ceases to be portable .NET Standard library since it is bound to framework specific package.

.NET Standard 2.0 does not contain System.Configuration.ConfigurationManager API. Therefore, the only way to use this API is to build one version of the library against .NET Core System.Configuration.ConfigurationManager package which can be used on .NET Core and have a second version of the library which is build against .NET FX System.Configuration assembly and can be used on .NET FX.

Repetitive answered 25/10, 2017 at 22:23 Comment(1)
This answer is now outdated and the correct answer is https://mcmap.net/q/294338/-can-39-t-use-system-configuration-configuration-manager-in-a-net-standard2-0-library-on-net-fx4-6Rosiarosicrucian
O
3

Had the same issue and after installing the same System.Configuration.ConfigurationManager package in the FX4.6 project resolved this issue.

Overglaze answered 13/12, 2018 at 12:18 Comment(0)
T
3

Alternative solutions are the following:

1. Copy .Net Standard dependencies

Add below line to your .Net Standard csproj file (Assembly 1) as described here.

<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>

Then rebuild Console App and System.Configuration.ConfigurationManager.dll with other dependencies of Assembly 1 will be copied to bin directory of Console App.

In my case it copies the below list of dlls Dependencies of .Net Standard

Interesting thing is that when I use another solution and install System.Configuration.ConfigurationManager NuGet package to FX4.6 Console App directly, I have another set of dependencies in result: Dependencies of .Net Framework

Notice that System.Configuration.ConfigurationManager.dll as well as other dlls have different size. As I understand, in first case it was copied from %userprofile%\.nuget\packages\system.configuration.configurationmanager\5.0.0\lib\netstandard2.0, but in second case from %userprofile%\.nuget\packages\system.configuration.configurationmanager\5.0.0\lib\net461

Anyway, both solutions work, so I'm not sure which is more correct.

or

2. Use multi-targeting

In your .Net Standard csproj file (Assembly 1) set second target to net461 as described here with line

<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>

Then on build two versions of library will be generated - in \bin\Release\net461 and in \bin\Release\netstandard2.0. If your Console App targets .Net Framework 4.6.1 or higher, it will automatically take correct version of Assembly 1 as dependency (the one from \net461).

The benefit of that approach is that net461 version of library doesn't require dependency from System.Configuration.ConfigurationManager.dll and can be distributed without it.

Talapoin answered 28/1, 2021 at 13:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.