Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 311
kiran Avatar asked Sep 22 '17 08:09

kiran


3 Answers

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

Install-Package System.Configuration.ConfigurationManager

in NuGet Package Manager

like image 148
Rilcon42 Avatar answered Oct 09 '22 16:10

Rilcon42


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.

like image 16
Jacek Blaszczynski Avatar answered Oct 09 '22 17:10

Jacek Blaszczynski


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

like image 3
prcontra Avatar answered Oct 09 '22 18:10

prcontra