0

I have some legacy (.NET 4.6) code that uses ConfigurationManager to access AppSettings. I'm trying to use this class library in an Azure Function v3 and it fails with error:

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

Before I spend the time to remove the dependency on System.Configuration it would be great to get confirmation (documented) that this specific scenario is not supported -- I can't find it anywhere.

Am I missing something simple for the interop or is it unsupported?

Stringer Bell
  • 265
  • 1
  • 4
  • 13
  • https://www.itnota.com/use-system-configuration-configurationmanager-dot-net-core/ – mxmissile Mar 26 '20 at 22:01
  • @mxmissile that link is about using the ConfigurationManager when upgrading a legacy .NET app to .NET Core. My question is about using a third-party library that uses it. Moreover, before I posted this question I did try the suggested solution and any other I could find. Nothing worked. – Stringer Bell Mar 26 '20 at 22:10
  • 1
    So you just forgot to deploy it? Good things might still happen if you give up on old bad practices, System.Configuration is high on that list. – Hans Passant Mar 26 '20 at 22:47
  • No, and your comment is in line with what I'm seeing on other threads. This has nothing to do with deployment issues. The Azure Function is compiling in VS but at runtime when the code tries to execute the legacy class library it fails with the error mentioned. – Stringer Bell Mar 26 '20 at 23:09
  • Does this answer your question? [Cannot add System.Configuration.ConfigurationManager to .NET core application](https://stackoverflow.com/questions/46473508/cannot-add-system-configuration-configurationmanager-to-net-core-application) – Lex Li Mar 27 '20 at 00:25
  • 1
    No, again this is about running a class library that uses .NET4.6 and uses the System.Configuration API within an Azure Function v3 (.Net CORE 3.1) – Stringer Bell Mar 27 '20 at 01:27

1 Answers1

1

Azure Function version 2 and above based on .Net core, you cannot use the existing .net Framework (4.6) library if they are not compatible.

When you will use existing .Net framework class library, you have to put your all configuration in .Json file. There is no concept of AppSetting in function. If you have only ConfigurationManager class issue, you can do below changes

.Net Framework 4.6

public string DatabaseId
    {
        get
        {
            return ConfigurationManager.AppSettings["DatabaseId"];
        }
    }

**.Net Core 3.1 **

public string DatabaseId
    {
        get
        {
            return Environment.GetEnvironmentVariable("DatabaseId");
        }
    }

If you don't want to touch your existing code, you can use Azure Function v1 with ConfigurationManager.

Updated Answer

ConfigurationManager is back, you can see this link but I didn't found any documentation, how to use new configuration Manager. New ConfigurationManager is .netstandard 2.0 compatible.

My looking Source code I'm assuming it will read app setting from XML/JSON file but not sure how we can utilize this in .netstandard.

I have install System.Configuration.ConfigurationManager Nuget in my sample application (Console, Web and Function) but didn't get any values from JSON file.

enter image description here

Pankaj Rawat
  • 4,037
  • 6
  • 41
  • 73
  • Thanks, that's the conclusion I came to as well. Is there any documentation that clearly calls this out? Where do I find a list of "compatible" APIs? Where does it say that System.Configuration is not supported in .NET Core? – Stringer Bell Mar 27 '20 at 16:41
  • Updated my answer with extra detail – Pankaj Rawat Mar 28 '20 at 07:47
  • Sorry I didn't fully follow that. Is your class library .NET4.6? Did it run correctly? – Stringer Bell Mar 29 '20 at 01:31
  • All projects in above screenshot from .net core 3.1 and I install System.Configuration.ConfigurationManager from nuget – Pankaj Rawat Mar 30 '20 at 05:18
  • Right, so that's not really addressing the question then because it's specifically about interop between .NET Framework and .NET Core – Stringer Bell Mar 30 '20 at 17:09