Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeLoadException Could not load type 'WebConfigurationManager' when calling .Net Framework Library via Web API Core app

I have a Web API application created using .Net Core 2.1. A controller from within this web application calls a Business Layer Class library that I also developed using .Net Core 2.1. So far so good...

The .Net Core Business Class Library references a commonly used .Net Framework 4.6.1 Class Library that we have also developed. This library is primarily used to communicate with Azure service bus queues.

As a result this commonly used .Net Framework Class Library in turn references and makes use of the Microsoft .Net Framework Assembly, Microsoft.ServiceBus as shown in the line of code below.

MessagingFactory messagingFactory = MessagingFactory.CreateFromConnectionString(configValue);

As you can see the line of code above that is within our commonly used .Net Framework Class Library passes a string value (i.e. configValue) to a static method that exists within the Microsoft.ServiceBus assembly.

However, whenever the line of code above executes I get the following Error:

An unhandled exception occurred while processing the request.

TypeLoadException: Could not load type 'System.Web.Configuration.WebConfigurationManager' from assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

Out of interest I added in the following line of code which I get the same error:

string configValue = WebConfigurationManager.AppSettings["connectionString"];

Clearly, the issue is that the System.Web .Net Framework assembly isn't loaded.

However, given that the web application is .Net Core then how can I make this web application load the underlying .Net Framework assembly that the commonly used library requires?


Solutions Considered - (Unsuitable)

  • I had a similar issue before which was easily solved by installing the necessary NuGet package but on this occasion I have not been able to find such a package.
  • Our team has another Web API application in .Net Framework which calls this commonly used class library without issue however this is not an option and we need to keep the current Web API application based on .Net Core.
  • I also considered having the commonly used library re-written as a .Net Standard 2.0 Class Library but this given the size of the library this would not be a realistic approach and it won't necessarily address the underlying issue.

Other Possible Solutions

  • Load a Web.config from within the .Net Core Web API application that contains a configSections section referencing the assembly System.Web. I tried this but it didn't work which I'm sure if I just didn't apply all steps necessary. Is this possible and if so any suggestions?
  • Is there an alternative way to load the .Net Framework assembly from within the .Net Core Web API?
  • Sacrifice using the commonly used library and find an alternative (sample) code base that provides similar functionality (i.e. make use of Microsoft.ServiceBus to work with our Azure queues) but that is either .Net Core or .Net Standard based. But similar to the .Net Standard solution above I am unsure how much functionality would need replicated and also unsure of a starting point. Thoughts?

Thanks!

like image 835
MartyG Avatar asked Jul 15 '18 21:07

MartyG


1 Answers

I had the same issue and after investigating i found that my azure function app run time version is 2.0 ~ and according to documentation version 2 is .NET Core 2 and function app version 1.0 ~ is .NET Framework 4.7 . and my visual studio function app project have .NET Framework 4.6.1 . So, I just had to change my function app version to ~ 1.0 .

In a Nutshell

  • If you have visual studio function app in .NET Core 2.0 then only use function app version 2.0
  • If you have visual studio function app in .NET Framework 4.7 then only use function app version 1.0

enter image description here

like image 106
Jawand Singh Avatar answered Nov 04 '22 21:11

Jawand Singh