Home > Mobile >  Can't import System.Net.Http on VB.Net page
Can't import System.Net.Http on VB.Net page

Time:07-02

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 ?

CodePudding user response:

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.

  • Related