Runtime Loading Azure.Search.Common.5.0.2 fails to load Microsoft.Rest.ClientRuntime

199 Views Asked by At

I am trying to load the dll for Microsoft.Azure.Search.Common.5.0.2 at runtime. From what I see on the nuget page, and from what nuget downloads when installing this, it depends upon Microsoft.Rest.ClientRuntime.2.3.12 . However, when I directly try to load the Azure.Search.Common dll using the following:-

Assembly.LoadFrom(dllPath).GetTypes().Select(t => t.Namespace).Distinct().ToList<string>();

I get the following Loader Exception:-

{"Could not load file or assembly 'Microsoft.Rest.ClientRuntime, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.":"Microsoft.Rest.ClientRuntime, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"}

I am loading the dll after installing from nuget. Specifically, I am the loading the following dll:

Microsoft.Azure.Search.Common\Microsoft.Azure.Search.Common.5.0.2\lib\net452\Microsoft.Azure.Search.Common.dll

Just to be sure, I have also gone though my csproj and config files, to check for some version redirection, but that doesn't seem to be the case.

Context: I want to know all possible namespaces that a project might be using, hence I am downloading and loading all nuget packages to get their namespaces, amongst other things.

EDIT:

I tried to further analyse the dll by loading it into ILSpy. On loading the dll, ILSpy also shows that it refers to version 2.0.0.0 of Microsoft.Rest.ClientRuntime. It also fails to load that references and gives the following errors:-

// Microsoft.Rest.ClientRuntime, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 // Assembly reference loading information: // There were some problems during assembly reference load, see below for more information! // Error: Could not find reference: Microsoft.Rest.ClientRuntime, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35

// System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a (unresolved) // Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed (unresolved) // Microsoft.Rest.ClientRuntime.Azure, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 // Assembly reference loading information: // There were some problems during assembly reference load, see below for more information! // Error: Could not find reference: Microsoft.Rest.ClientRuntime.Azure, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35

1

There are 1 best solutions below

2
On

By the way the following worked:

var ldAssmbly = Assembly.LoadFrom("Microsoft.Azure.Search.Common.dll");
var results = ldAssmbly.GetTypes().Select(t => t.Namespace).Distinct().ToList<string>();

enter image description here

The Trick is to copy all the dependencies to the output folder (like I did). Microsoft kindly built the Assembly Loader in a Cool way where it can scan for dependencies in the current path. Take advantage of that :)

It does depend on v2.0.0.0 - below is from IL manifest

enter image description here

Can you check your App.config - Whether a assembly redirect is added when you Install the nuget package? That may be the reason it doesnt work when you Reflection Load the Assembly.

Update:- Yeah - Seems Dodgy:-

after nuget install - it does say the dependency is v2.3.12 However, the added dll shows v2.0.0 in VisualStudio. enter image description here dll file properties says v2.3.12 (which comes from AssemblyInfo version attribute)

enter image description here

But upon IL inspection - its clearly v2.0.0 (which is what VisualStudio shows)

enter image description here