0

I'm trying to use HttpClient on a VB.NET page (Windows Server 2019 IIS 10), but getting BC30002 error. Tracing the error in IIS, I see warning BC40056 (Namespace not found) on line:

Imports System.Net.Http

Running gacutil, seems that the assembly is correctly installed on server:

>gacutil.exe /l system.net.http
Microsoft (R) .NET Global Assembly Cache Utility.  Version 4.0.30319.0
Copyright (c) Microsoft Corporation.  All rights reserved.

The Global Assembly Cache contains the following assemblies:
  system.net.http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL

Number of items = 1

Also tryed to add the following to web.config (should not be necessary and didn't help):

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    </dependentAssembly>
  </assemblyBinding>
</runtime>

Other assemblies load fine (including plain System.Net).

Where am I wrong ?

Massimo
  • 90
  • 1
  • 8
  • 1
    Which version of .net are you using in your project and how are you deploying it? – Hursey Jun 29 '22 at 08:36
  • It's simple page mypage.aspx.vb with no deployment (plain text editing). CLR version in the AppPool is 4.0.30319. – Massimo Jun 29 '22 at 08:47
  • 1
    Is your project using .NET or .NET Framework? See [here](https://dotnet.microsoft.com/en-us/learn/dotnet/what-is-dotnet-framework) for more info. If using .NET, according to [Global assembly cache APIs are obsolete](https://learn.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/5.0/global-assembly-cache-apis-obsolete): _.NET Core and .NET 5 and later versions eliminate the concept of the global assembly cache (GAC) that was present in .NET Framework. As such, all .NET Core and .NET 5+ APIs that deal with the GAC either fail or perform no operation._ – Tu deschizi eu inchid Jun 29 '22 at 13:41
  • Have you tried adding reference to the dll in your project? – Fawlty Jun 29 '22 at 16:39
  • Besides these, do you have any other error messages? For example use F12 or check Event viewer. – samwu Jun 30 '22 at 07:53
  • Is an old project still in .NET Framework 4. Never considered to migrate it to new .Net 6. I don't know even what it takes. Anyway HttpClient is present also in the Framework, since 4.5. The doubt I have is about the .Net CLR. I've installed .NET Framework 4.8 on my server, so the HttpClient is present, but the version of CLR, inside .Net Framework 4.8, is 4.0. May this mean that HttpClient is not present in CLR? Afaik IIS only uses CLR. – Massimo Jun 30 '22 at 10:57
  • .Net Framework 4.0 is no longer supported. See [here](https://learn.microsoft.com/en-us/lifecycle/products/microsoft-net-framework) for more information. – Tu deschizi eu inchid Jun 30 '22 at 14:45
  • I'm using .Net Framewrok 4.8, that's still supported. Microsoft says: "You don't need to migrate your .NET Framework apps, but for new development, use .NET 5 or later." – Massimo Jul 01 '22 at 10:03

1 Answers1

0

Finally understood that, even if .NET Framework 4.8 supports HttpClient, CLR is still at 4.0 version (that don't support HttpClient). Optimal solution, as noted by user9938, would be migrating to .NET 6, but, for an old app where I just need to add a small integration, was easier for me to use old WebClient, instead of HttpClient. This works like a charm:

Imports System.Net
...
Using client As New WebClient()
   BackJson = client.DownloadString(APIUri)
End Using

WebClient is still supported (as of .Net 6.0.6), even if not recommended for new development.

Massimo
  • 90
  • 1
  • 8
  • I never stated that HttpClient wasn't supported in .NET Framework 4.8. I merely mentioned that .NET Framework 4.0 was no longer supported. Ensure that your project is using .NET Framework 4.8. Then ensure that .NET Framework 4.8 is installed on the server. Then add it for IIS. I don't have Server 2019, but in Win 10, open `Control Panel (View by: Small icons).` Click `Programs and Features`. Click `Turn Windows features on or off`. Then expand `Internet Information Services`. Expand `World Wide Web Services`. Expand `Application Development Features.` Check `ASP.NET 4.8`. Click `OK` – Tu deschizi eu inchid Jul 01 '22 at 17:16
  • Yes, this is enabled (in Windows Server is under Add/Remove roles in Server Manager), but IIS still sees CLR 4.0. This beacuse .Net Framework 4.8 includes CLR 4.0. So, 4.8 can be used in Visual Studio, but not in JIT compiler of IIS, that uses, maximum, 4.0. No time to recompile and debug everything in VS. Was easier to use WebClient. – Massimo Jul 04 '22 at 08:54