0

We use IHttpClientFactory in a .NET 4.7 COM+ class. To be able to use IHttpClientFactory, we added the Nuget package Microsoft.Extensions.Http version 7.0, which installed the following dlls in the packages directory:

Microsoft.Bcl.AsyncInterfaces.7.0.0\lib\net462\Microsoft.Bcl.AsyncInterfaces.dll
Microsoft.Extensions.DependencyInjection.Abstractions.7.0.0\lib\net462\Microsoft.Extensions.DependencyInjection.Abstractions.dll
Microsoft.Extensions.DependencyInjection.7.0.0\lib\net462\Microsoft.Extensions.DependencyInjection.dll
Microsoft.Extensions.Http.7.0.0\lib\net462\Microsoft.Extensions.Http.dll
Microsoft.Extensions.Logging.7.0.0\lib\net462\Microsoft.Extensions.Logging.dll
Microsoft.Extensions.Logging.Abstractions.7.0.0\lib\net462\Microsoft.Extensions.Logging.Abstractions.dll
Microsoft.Extensions.Options.7.0.0\lib\net462\Microsoft.Extensions.Options.dll
Microsoft.Extensions.Primitives.7.0.0\lib\net462\Microsoft.Extensions.Primitives.dll
System.Buffers.4.5.1\lib\net461\System.Buffers.dll
System.Diagnostics.DiagnosticSource.7.0.0\lib\net462\System.Diagnostics.DiagnosticSource.dll
System.Memory.4.5.5\lib\net461\System.Memory.dll
System.Runtime.CompilerServices.Unsafe.6.0.0\lib\net461\System.Runtime.CompilerServices.Unsafe.dll
System.Threading.Tasks.Extensions.4.5.4\lib\net461\System.Threading.Tasks.Extensions.dll
System.ValueTuple.4.5.0\lib\net47\System.ValueTuple.dll

We installed these dlls in the GAC with gacutil. Upon calling the method HttpClient.PostAsync we get the exception:

Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

  1. What do we have to do, so that COM+ can find this dll?
  2. Is it safe to install the above dlls in the GAC on a production server with Windows Server 2012?
  3. The name "System.Runtime.CompilerServices.Unsafe.dll" does not suggest safeness. Is it really safe for a production environment?
  4. Is it safe to use HttpClientFactory in a COM+ class?
  5. The following solution demonstrates the problem: https://github.com/pappasa/TestHttpClientFactory

Best regards,

Alexandros

Alex
  • 419
  • 1
  • 5
  • 14
  • [There is no version 7.0 of the `System.Net.Http` NuGet package - the most recent is 4.3.4](https://www.nuget.org/packages/System.Net.Http/#versions-body-tab) - why and how does your post say you're using version 7.0?. – Dai May 04 '23 at 11:55
  • Your GitHub repo only contains VB.NET projects (and the C# ASP.NET project) - I don't see any actual use of COM+ beyond an `Import System.EnterpriseServices` line. – Dai May 04 '23 at 11:57
  • @Dai Well he clearly has ```Microsoft.Extensions.Http.7.0.0\lib\net462\Microsoft.Extensions.Http.dll``` being used from the output shown. And I could find this link https://www.nuget.org/packages/Microsoft.Extensions.Http/7.0.0 – majixin May 04 '23 at 11:59
  • @majixin The OP wrote _"we added the Nuget package `System.Net.Http` version 7.0"_ - they didn't mention `Microsoft.Extensions.Http` by name. – Dai May 04 '23 at 12:02
  • 1
    OP, why are you talking about COM+ at all here? I don't see how COM+ is relevant at all here: you can accomplish what you want with normal assembly references, and you shouldn't be touching the GAC at all. – Dai May 04 '23 at 12:03
  • @Dai Correct, but they've shown ```Microsoft.Extensions.Http``` in the output, so I expect the OP is conflating them. – majixin May 04 '23 at 12:04
  • Excuse me, we added Nuget package Microsoft.Extensions.Http (which contains IHttpClientFactory). I erroneously wrote System.Net.Http. – Alex May 04 '23 at 13:44
  • Our business logic runs on COM+, because we need distributed transactions, and therefore, we have to make REST calls from COM+. You can see that the class APIAccess inherits from ServicedComponent, and in the post-build events, the APIAccess project is registered in the GAC (with gacutil) and in COM+ (with regsvcs) – Alex May 04 '23 at 13:47
  • @Alex Distributed Transactions don't depend on COM+ - have you seen the types in [the `System.Transactions` namespace](https://learn.microsoft.com/en-us/dotnet/api/system.transactions)? – Dai May 05 '23 at 00:31
  • Also, what do you mean by "make REST calls **from** COM+"? All of your posted code is VB.NET or C#, which means it runs within the CLR, and not from within a native (i.e. not-.NET) COM+ host-process - (unless it is? in which case you haven't posted anything that suggests that...) – Dai May 05 '23 at 00:33
  • Yes, I know that you can do distributed transactions with System.Transactions, but our whole business logic runs now on COM+, and we cannot change that for the time beeing. We are planning to migrate away from COM+, but only in two or three years. – Alex May 05 '23 at 07:14
  • The posted code, and in particular the class APIAccess is in VB.NET and inherits from System.EnterpriseServices.ServicedComponent, that means, it runs in COM+. I use COM+ as a synonym for Component Services. That means, it runs in the process dllhost.exe. Not only native libraries may run in COM+, .NET libraries may also run in COM+ (or else Component Services) – Alex May 05 '23 at 07:18

0 Answers0