NicoTek
NicoTek

Reputation: 1167

ASP.NET Core 3.1 project that is referencing a .NET 4.7.2 project in the same solution error 'System.Web.Configuration.WebConfigurationManager'

I have an ASP.NET Core 3.1 project that is referencing a .NET 4.7.2 project in the same solution. It compiles without problems but at runtime I get an error:

Could not load type 'System.Web.Configuration.WebConfigurationManager'.

I have installed the Microsoft.Windows.Compatibility package in the ASP.NET Core project, but that did not help.

Am I missing something or will this not work without some major refactoring of the .NET 4.7.2 project to not use that System.Web.Configuration.WebConfigurationManager namespace?

Upvotes: 4

Views: 1208

Answers (1)

Fei Han
Fei Han

Reputation: 27793

I have an ASP.NET Core 3.1 project that is referencing a .NET 4.7.2 project in the same solution. It compiles without problems but at runtime I get an error:

Could not load type 'System.Web.Configuration.WebConfigurationManager'

ASP.NET Core is a cross-platform framework, and ASP.NET Core use different configuration settings, for more information, please check:

I suppose that your .NET 4.7 project might be a class library and would be called by other applications.

As above error indicated System.Web.Configuration.WebConfigurationManager could not be compatible with .NET Core application.

If possible, you can try to modify your class library (.NET 4.7 project) to pass retrieved data (you configured within web.config, or appsettings.json) from your caller app to class library method instead of retrieving data via WebConfigurationManager in class library.

Besides, you can also to modify and add additional method to make it working with ASP.NET Core configuration settings, like below.

public class MyClassLibrary
{
    private readonly IConfiguration Configuration;
    public MyClassLibrary()
    {
    }
    public MyClassLibrary(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public void MethodForNet()
    {
        var val = WebConfigurationManager.AppSettings["mykey"];

        //code logic here
    }

    public void MethodForCore()
    {
        var val = Configuration["AppSettings:mykey"];

        //code logic here
    }

    //other methods
}

Upvotes: 3

Related Questions