Im encoding a class library (called mylibrary.dll), which itself references some other libraries β uses the following DLLs (taken from package.config to review the version):
<package id="EntityFramework" version="6.0.0" targetFramework="net45" /> <package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net45" /> <package id="Newtonsoft.Json" version="8.0.2" targetFramework="net45" /> <package id="System.Data.SQLite" version="1.0.99.0" targetFramework="net45" /> <package id="System.Data.SQLite.Core" version="1.0.99.0" targetFramework="net45" /> <package id="System.Data.SQLite.EF6" version="1.0.99.0" targetFramework="net45" /> <package id="System.Data.SQLite.Linq" version="1.0.99.0" targetFramework="net45" /> <package id="UnmanagedExports" version="1.2.7" targetFramework="net45" />`
mylibrary.dll is a shell that provides some managed code to a caller who expects unmanaged code (in other words, where entries from the embedded DLL are expected).
If I test the open interface mylibrary.dll using the NUnit testing methods, there is no error at all. But if I call the same methods through the same interface from the targetapplication.exe file, I recognize the following situations:
- Test Method A: Calling a simple JSON operation for a string (used by Newtonsoft.JSON) and works fine.
- Test Method B: Calls the method that PostAsync does and, in addition,
var vResponseObject = wait vResponse.Content.ReadAsAsync <ApiResponse <T β () ConfigureAwait (false) ;.
Behind the scenes, the ReadAsAsync call uses Newtonsoft.JSON to deserialize an object of type <T> . This function seems to be generating an error:
Failed to load file or assembly "Newtonsoft.Json, Version = 6.0.0.0, Culture = neutral, PublicKeyToken = ..........." or one of its dependencies. The located assembly manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
HTTPContent.ReadAsAsync is provided by Microsoft.AspNet.WebApi.Client (extends System.Net.Http), which itself depends on Newtonsoft.JSON version 6.0.x (see the NuGet dependency for this package). Version 6.0.x is not installed, instead of version 8.0.x. Thus, there is a need for assebly binding redirection, which is controlled in app.config:
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0" /> </dependentAssembly> </assemblyBinding>
Now I do not know how to solve this. In my project, Microsoft.AspNet.WebApi.Client is the only other library that references Newtonsoft.JSON (version 6.0.x, the same version that the error tells me about). It seems that binding redirection is simply ignored. Because it is not a βFile Exception Not Foundβ, I think it can find version 8 of the dll, but 6 is expected, right? All DLLs are in the same directory as targetapplication.exe
Update with a partial solution: As a workaround, I was able to avoid an external call to Newtonsoft.Json via System.Net.Http.Formatting.dll
//vResponseObject = await vResponse.Content.ReadAsAsync<ApiResponse<T>>().ConfigureAwait(false); string vStr = await vResponse.Content.ReadAsStringAsync(); vResponseObject = JsonConvert.DeserializeObject<ApiResponse<T>>(vStr);
But this is really not a valid solution for further development, if I need to code calls like mydll β thirdparty.dll β anotherthirdparty.dll