ASP.NET MVC / I use VkNet to post entries to a group in VK

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

After that, the Action logic 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 VkontakteService class itself with the WallPost method lies in a neighboring project (Class Library) with a connected VkNet

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, can anyone encountered such errors, I just see such a strange log for the first time?

  • one
    Bring package.config - Pavel Mayorov
  • perhaps it is worth doing a restore package for all projects in the solution - Grundy

3 answers 3

VkNet version 1.21 uses the Newtonsoft.Json package version 8.0.0.2

At the same time, in the section Dependencies in nuget it is> = - that is why the latest version was pulled. This is the error of the authors of the library VkNet.

You need to do one of three things:

  1. Update VkNet to latest version
  2. Downgrade Newtonsoft.Json to version 8.0.0.2
  3. Prescribe redirection (assemblyBinding) for the assembly of Newtonsoft.Json from version 8.0.0.2 to 9.0.0.1 in the web.config file
  • Thanks, I'll try - tCode
  • Newtonsoft.Json helped downgrade and then had to do downgrade for HtmlAgilityPack, thanks for the answer - tCode

Maybe VkNet requires Newtonsoft.Json version higher than you use.

  • Newtonsoft.Json pulled himself when adding VkNet ... - tCode
  • I checked @tCode - adding VkNet pulls up the right version of Newtonsoft.Json. Most likely, a newer version of yours was pulled along with ASP.NET MVC - Pavel Mayorov
  • @PavelMayorov how to be? - tCode
  • @tCode I wrote in my answer - Pavel Mayorov
  • @PavelMayorov a, ok - tCode

The exception says that the version of the assembly Newtonsoft.Json Version=9.0.0.0 cannot be downloaded. As soon as there is a problem with NuGet packages, some tambourine dances can help, namely:

  1. Clear the solution (it is better to clean everything additionally with your hands);
  2. Restore your NuGet-packages, right-clicked through the Restore NuGet Packages solution (make sure that the desired NuGet-package appeared in your packages folder for NuGet-packages) or reinstall the necessary NuGet-package through the Package Manager Console ;
  3. Ensure that the links in the project point to the existing version of the NuGet package (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 used in conjunction with Newtonsoft.Json, Version=8.0.0.0 . Link to the source . Apparently it makes sense to lower the version of Newtonsoft.Json .

If you delve into the exception ( Exception from HRESULT: 0x80131040 ), you can find out what the exception code means when you try to call a method from an assembly that does not exist. In other words, the difference in versions of Newtonsoft.Json . In one version there is something, in the other there is none. Hence the pitfalls. You can try to first lower the version of Newtonsoft.Json , if it does not help, try to restore the packages (despite what they say in the comments).

  • These dances will not help - because the versions of the loadable libraries will not change from them, they will remain incompatible. - Pavel Mayorov
  • @PavelMayorov, here are two options. The first is that which is written in the exception, cannot find the library, and the second I added in the answer - use a lower version of Newtonsoft.Json . - Denis Bubnov
  • @PavelMayorov, and you minus the fault for me that I shared possible solutions to the problem, which helped me earlier, but which did not satisfy you? - Denis Bubnov
  • No, because these options will not help in this particular case. - Pavel Mayorov
  • The solution was cleared, the packages were restored - they did not grind, in the evening I would try to rewrite the version in app.config - tCode