Google-api-dotnet-client: Could not load Newtonsoft.Json V9.0.0.0 on latest Google.Apis.YouTube.v3 1.16.0.582

Created on 1 Oct 2016  路  10Comments  路  Source: googleapis/google-api-dotnet-client

When using the latest YouTube v3 client library (Google.Apis.YouTube.v3) in a .NET 4.6.1 project.json app I get the following exception on runtime:


Exception Details

System.IO.FileLoadException occurred
FileName=Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
FusionLog==== Pre-bind state information ===
LOG: DisplayName = Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
(Fully-specified)
LOG: Appbase = file:///C:/Dev/Repositories/YouCast/Source/YouCast/bin/Debug/
LOG: Initial PrivatePath = NULL

Calling assembly : Google.Apis.YouTube.v3, Version=1.16.0.582, Culture=neutral, PublicKeyToken=4b01fa6e34db77ab.

LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Dev\Repositories\YouCast\Source\YouCast\bin\Debug\YouCast.vshost.exe.config
LOG: Using host configuration file:
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:/Dev/Repositories/YouCast/Source/YouCast/bin/Debug/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=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)
Source=mscorlib
StackTrace:
at System.ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type)
at System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, Assembly& lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, Object[] attributes, IList derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& ctorHasParameters, Boolean& isVarArg)
at System.Reflection.CustomAttribute.IsCustomAttributeDefined(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, RuntimeType attributeFilterType, Int32 attributeCtorToken, Boolean mustBeInheritable)
at System.Reflection.CustomAttribute.IsDefined(RuntimePropertyInfo property, RuntimeType caType)
at System.Reflection.RuntimePropertyInfo.IsDefined(Type attributeType, Boolean inherit)
at Newtonsoft.Json.Serialization.DefaultContractResolver.GetSerializableMembers(Type objectType)
at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateProperties(Type type, MemberSerialization memberSerialization)
at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateObjectContract(Type objectType)
at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateContract(Type objectType)
at Newtonsoft.Json.Serialization.DefaultContractResolver.ResolveContract(Type type)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObjectT
at Google.Apis.Json.NewtonsoftJsonSerializer.DeserializeT in C:\Users\cloudsharp\Documents\GitHub\google-api-dotnet-client\Src\Support\GoogleApis.Core\Apis\Json\NewtonsoftJsonSerializer.cs:line 124
at Google.Apis.Services.BaseClientService.d__331.MoveNext() in C:\Users\cloudsharp\Documents\GitHub\google-api-dotnet-client\Src\Support\GoogleApis\Apis\Services\BaseClientService.cs:line 265 --- 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 Google.Apis.Requests.ClientServiceRequest1.d__27.MoveNext() in C:\Users\cloudsharp\Documents\GitHub\google-api-dotnet-client\Src\Support\GoogleApis\ApisRequests\ClientServiceRequest.cs:line 0
--- 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 Google.Apis.Requests.ClientServiceRequest1.<ExecuteAsync>d__23.MoveNext() in C:\Users\cloudsharp\Documents\GitHub\google-api-dotnet-client\Src\Support\GoogleApis\Apis\Requests\ClientServiceRequest.cs:line 0 --- 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 Google.Apis.Requests.ClientServiceRequest1.d__22.MoveNext() in C:\Users\cloudsharp\Documents\GitHub\google-api-dotnet-client\Src\Support\GoogleApis\ApisRequests\ClientServiceRequest.cs:line 0
--- 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 System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Service.YoutubeFeed.d__5.MoveNext() in C:\Dev\Repositories\YouCast\Source\Service\YoutubeFeed.cs:line 60
InnerException:

The lowest call is Google.Apis.Json.NewtonsoftJsonSerializer.Deserialize in the Google.Apis.Core package.
By looking at the nuspec I see that .NET Standard gets v9 but the rest get v7 so I assume I get the package requiring v7 but the assemblies inside it actually require v9.

All 10 comments

Is this linked to #829?

It seems related, but not the same issue.
They want to advance to a newer version to get the latest and greatest while my usage of the library as it is breaks on runtime.

Please could you give details on how to reproduce this? I'd expect assembly binding to just "do the right thing". Can you reproduce it in a simple console app?

@jskeet I can. I just opened a new console app, added a project.json file with the latest YouTube v3 client library and called one of the APIs:

var youtubeService = new YouTubeService(new BaseClientService.Initializer
{
    ApiKey = "AIzaSyD0E4ozDor6cgdyQKHvOgLCrrQMEX226Qc",
    ApplicationName = "YouCast2",
});

var listRequestForUsername = youtubeService.Channels.List("snippet,contentDetails");
listRequestForUsername.ForUsername = "i3arnon";
listRequestForUsername.MaxResults = 1;
listRequestForUsername.Fields = "items(contentDetails,id,snippet)";

Console.WriteLine(await listRequestForUsername.ExecuteAsync());

I've published this app to a GH repo if you want to fork/clone: https://github.com/i3arnon/YouTubeClient

P.S: Yes, I know I shouldn't expose my API key. It's in the backlog :).

Thanks - will have a look when I get a moment. (I'm currently travelling, so it may well be next week.)

Hmm... just had a look. The current code doesn't even build for me with dotnet cli, with "The type 'Object' is defined in an assembly that is not referenced." errors. It all works when you change to use net45 instead. So that's one oddity.

Next, when I build with msbuild (in detailed), it appears to be using the netstandard1.3 dependency - it says:

Could not resolve this reference. Could not locate the assembly "Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors.
...
Required by "C:\Users\skeet\.nuget\packages\Google.Apis.YouTube.v3\1.16.0.582\lib\netstandard1.3\Google.Apis.YouTube.v3.dll".

Now we only explicitly have dependencies for net40 and net45 - see the nuspec file. I'd _expect_ net461 to pick up on the net45 one though.

@chrisdunelm have you seen anything like this before? It looks like net461 is basically borked...

This is really unfortunate.
Using the "Get Nearest Framework" tool it appears that netstandard1.3 is the preferred choice given the targets contained in our packages.
I suspect we may need to add an "net45" (or possibly "net46") target, both of which I've just verified is then used when targeting net46[1].
So yes, net461 is currently borked.
I'll start work now on a new release with a fix.

Being addressed in #848. I expect to get this released to nuget tomorrow.

This should now be fixed by v1.17 of the libraries, which are now available on nuget.

Works great. Thanks :)

Was this page helpful?
0 / 5 - 0 ratings