C# ASP.NET strange error when calling a method from the Class Library project

ASP.NET MVC / I use VkNet to publish records to a group in VK

At a certain time, the task scheduler makes a GET request with a key for a certain time.Action (https://mydomain.com/Service/Vk?key=SECRET_KEY)

After that, the logic Action is executed and at the end, the method responsible for working with VkNet is called:

VkontakteService.WallPost(APPID, "LOGIN", "PWD", OWNERID, message);

Method:

public static void WallPost(ulong appId, string login, string password, long ownerId, string message)
{
    try
    {
        VkApi api = new VkApi();
        api.Authorize(new ApiAuthParams
        {
            ApplicationId = appId,
            Login = login,
            Password = password,
            Settings = Settings.All
        });

        api.Wall.Post(new WallPostParams
        {
            OwnerId = ownerId,
            FromGroup = true,
            Message = message,
            Signed = false,
        });
    }
    catch (Exception ex)
    {
        Log.Error($"Ошибка при добавлении записи в группу ВК: {ex}");
    }
}

As a result, I get a strange Exception:

[2016-11-21 23:13:11.3353][Error]: Ошибка при добавлении записи в группу ВК: Could not load file or assembly 'Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
System.IO.FileLoadException: Could not load file or assembly 'Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
File name: 'Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'
   at VkNet.VkApi.Call(String methodName, VkParameters parameters, Boolean skipAuthorization)
   at VkNet.Categories.WallCategory.Post(WallPostParams params)
   at MyProject.Services.VkontakteService.WallPost(UInt64 appId, String login, String password, Int64 ownerId, String message) in D:\MyProject\trunk\Sources\MyProject.Services\VkontakteService.cs:line 27

=== Pre-bind state information ===
LOG: DisplayName = Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
 (Fully-specified)
LOG: Appbase = file:///D:/MyProject/trunk/Sources/MyProject.Host/
LOG: Initial PrivatePath = D:\MyProject\trunk\Sources\MyProject.Host\bin
Calling assembly : VkNet, Version=1.21.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: D:\MyProject\trunk\Sources\MyProject.Host\web.config
LOG: Using host configuration file: C:\Users\Home\Documents\IISExpress\config\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
LOG: Attempting download of new URL file:///C:/Users/Home/AppData/Local/Temp/Temporary ASP.NET Files/root/ee02bc37/45707ec2/Newtonsoft.Json.DLL.
LOG: Attempting download of new URL file:///C:/Users/Home/AppData/Local/Temp/Temporary ASP.NET Files/root/ee02bc37/45707ec2/Newtonsoft.Json/Newtonsoft.Json.DLL.
LOG: Attempting download of new URL file:///D:/MyProject/trunk/Sources/MyProject.Host/bin/Newtonsoft.Json.DLL.
WRN: Comparing the assembly name resulted in the mismatch: Major Version
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

The class itself VkontakteService with the method WallPost are stored in a neighboring project (Class Library) with VkNet connected

App.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
        <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    </configSections>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="HtmlAgilityPack" publicKeyToken="bd319b19eaf3b43a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.4.9.5" newVersion="1.4.9.5" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

Tell me what could be the problem, maybe someone has encountered such errors, I just see such a strange log for the first time?

 1
Author: tCode, 2016-11-21

3 answers

VkNet version 1.21 uses the Newtonsoft package.Json version 8.0.0.2

At the same time, in the Dependencies section in nuget, it is > = - that's why the latest version has been pulled up. This is an error by the authors of the VkNet library.

You need to do one of three things:

  1. Update VkNet to the latest version
  2. Downgrade Newtonsoft.Json up to version 8.0.0.2
  3. Specify the redirection (assemblyBinding) for the Newtonsoft build.Json from version 8.0.0.2 to 9.0.0.1 in in the web. config file
 1
Author: Pavel Mayorov, 2016-11-22 06:31:32

Perhaps VkNet requires a version of Newtonsoft.Json is higher than you use

 3
Author: Dmitry Rogov, 2016-11-21 22:14:22

An exception indicates that the build version Newtonsoft.Json Version=9.0.0.0 cannot be loaded. As soon as there is a problem with NuGet packages, some dancing with a tambourine can help, namely:

  1. Clean the solution (it is better to clean everything with your hands additionally);
  2. To restore your NuGet packages, right-click on the solution Restore NuGet Packages (make sure that the necessary NuGet package appeared in your packages folder for NuGet packages) or reinstall the necessary NuGet package via Package Manager Console;
  3. Make sure that the links in the project point to the existing version of NuGet packages (Configs and project files);
  4. Rebuild the solution and try again.

On GitHub found the use of VkNet Version=1.21.0.0, it is shared with Newtonsoft.Json, Version=8.0.0.0. Link to the source. Apparently, it makes sense to downgrade the version Newtonsoft.Json.

If you delve deeper into the exception (Exception from HRESULT: 0x80131040), you can find out what the exception code means, an attempt to call a method from the assembly that does not exists. In other words, the difference is in the versions of Newtonsoft.Json. One version has something, the other doesn't. Hence the pitfalls. You can first try to downgrade the version Newtonsoft.Json, if it does not help-try to restore the packages (despite what they say in the comments).

 1
Author: Denis Bubnov, 2016-11-22 07:22:48