Azure Webjob 与 NewtonSoft 不兼容

Azure Webjob Incompatibility with NewtonSoft

我的 Azure WebJob 处理程序启动时出现以下异常:

System.IO.FileLoadException was unhandled FileName=Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
FusionLog==== Pre-bind state information === LOG: DisplayName = Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed (Fully-specified) LOG: Appbase = file:///C:/Programming/ConnellCampaigns/src/UploadProcessor/bin/Debug/net46/win7-x64/ LOG: Initial PrivatePath = NULL Calling assembly : Microsoft.Azure.WebJobs.Host, Version=1.1.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35. === LOG: This bind starts in default load context. LOG: Using application configuration file: C:\Programming\ConnellCampaigns\src\UploadProcessor\bin\Debug\net46\win7-x64\UploadProcessor.exe.Config LOG: Using host configuration file: LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config. LOG: Redirect found in application configuration file: 6.0.0.0 redirected to 6.0.0.0. LOG: Post-policy reference: Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed LOG: Attempting download of new URL file:///C:/Programming/ConnellCampaigns/src/UploadProcessor/bin/Debug/net46/win7-x64/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.

HResult=-2146234304 Message=Could not load file or assembly 'Newtonsoft.Json, Version=6.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)
Source=Microsoft.Azure.WebJobs.Host StackTrace: at Microsoft.Azure.WebJobs.Host.Protocols.PersistentQueueWriter1.<EnqueueAsync>d__0.MoveNext() at System.Runtime.CompilerServices.AsyncTaskMethodBuilder1.Start[TStateMachine](TStateMachine& stateMachine) at Microsoft.Azure.WebJobs.Host.Protocols.PersistentQueueWriter`1.EnqueueAsync(T message, CancellationToken cancellationToken) at Microsoft.Azure.WebJobs.Host.Loggers.PersistentQueueLogger.LogHostStartedAsync(HostStartedMessage message, CancellationToken cancellationToken) at Microsoft.Azure.WebJobs.Host.Executors.JobHostContextFactory.LogHostStartedAsync(IFunctionIndex functionIndex, HostOutputMessage hostOutputMessage, IHostInstanceLogger logger, CancellationToken cancellationToken) at Microsoft.Azure.WebJobs.Host.Executors.JobHostContextFactory.d__b.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Azure.WebJobs.Host.Executors.JobHostContextFactory.d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Azure.WebJobs.JobHost.d__f.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Azure.WebJobs.JobHost.d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Azure.WebJobs.JobHost.Start() at Microsoft.Azure.WebJobs.JobHost.RunAndBlock() at UploadProcessor.Program.Main(String[] args) in C:\Programming\ConnellCampaigns\src\UploadProcessor\Program.cs:line 25 InnerException:

只有当我在 project.json 中指定 NewtonSoft 的 JSON 程序集的最新版本 - 9.0.1 时才会发生这种情况。

我可以通过指定库 (6.0.8) 的最后一期 v​​6 来消除错误。但这会引入依赖性警告。

我不明白为什么 Microsoft.Azure.WebJobs.Host 坚持使用旧版本的库。它的 nuget 页面说它与任何版本兼容 >= 6.0.8.

我该如何解决这个问题?

您在上面发布的异常跟踪表明您的应用程序配置文件中有一个绑定重定向:

Redirect found in application configuration file: 6.0.0.0 redirected to 6.0.0.0.

尝试更改它以重定向到您实际使用的程序集版本:

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
  </dependentAssembly>
</assemblyBinding>

重要说明:绑定重定向使用程序集版本,而不是文件版本。所以对于 Json.Net 9.0.x,这将是 9.0.0.0。如果您尝试使用文件版本作为重定向的新版本,重定向将不起作用,因为它与实际程序集的程序集版本不匹配。