Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Google.Apis version doesn't work with newer version of Newtonsoft.Json #1464

Closed
csokmaib opened this issue Oct 1, 2019 · 18 comments
Closed
Assignees
Labels
needs more info This issue needs more information from the customer to proceed. type: question Request for information or clarification. Not an issue.

Comments

@csokmaib
Copy link

csokmaib commented Oct 1, 2019

I am using Google.Apis version 1.36.1 in a web application in .NET Framework 4.7.2 whizh works with a Newtonsoft.Json version 10.0.2. I want to include in my web application a class library in .NET Standard 2.0 which uses Newtonsoft.Json version 12.0.2, but I can't, because is an incompatibility issue between these 2 Newtonsoft version.

I wanted to upgrade the Newtonsoft.Json in web app from 10.0.2 into 12.0.2 but it says the Google.Apis doesn't support the newer version of the Newtonsoft.Json.

Any idea how can I solve this problem? Maybe Google.Apis should be do an upgrade?

Thanks in advance!

UPDATE:

I figured out what was the problem. I updated Newtonsoft from version 10.0.2 to 12.0.2. The assembly binding redirect appears into my web.config file, which is expected:

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

The problem why doesn't work the binding redirect was that namespace in my tag into my web.config:

<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">

I deleted the namespace xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0" from tag and the assembly binding redirect is working now.

This post was helped me: https://stackoverflow.com/questions/1751775/binding-redirect-problem-in-net/12011221#12011221

Thanks for all of you to try to helping me

@jskeet
Copy link
Collaborator

jskeet commented Oct 1, 2019

I've used Google Cloud APIs with recent versions of Newtonsoft.Json with no problems. Could you clarify exactly what errors you're seeing, and where you're seeing them? My guess is that this is a matter of assembly binding redirects getting messed up.

@jskeet jskeet self-assigned this Oct 1, 2019
@jskeet jskeet added type: question Request for information or clarification. Not an issue. needs more info This issue needs more information from the customer to proceed. labels Oct 1, 2019
@LindaLawton
Copy link
Collaborator

Question was cross posted on stack overflow

@csokmaib
Copy link
Author

csokmaib commented Oct 2, 2019

Here is my detailed scenario:

I updated Newtonsoft.Json to 12.0.2 via NuGet Package Manager. At this section

var certificate = new X509Certificate2(...);
const string user = "...";
var serviceAccountCredentialInitializer = new ServiceAccountCredential.Initializer(user)
{ Scopes = new[] { SheetsService.Scope.Drive } }.FromCertificate(certificate);
var credential = new ServiceAccountCredential(serviceAccountCredentialInitializer);
var service = new SheetsService(new BaseClientService.Initializer()
{ HttpClientInitializer = credential, ApplicationName = "My Application Name" });

when I initialize the service variable as new SheetsService(...), I got the following exception:

Could not load file or assembly 'Newtonsoft.Json, Version=10.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)":"Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed

Source = "Google.Apis.Core"
StackTrace = " at Google.Apis.Json.NewtonsoftJsonSerializer..ctor()\r\n at Google.Apis.Services.BaseClientService.Initializer..ctor()\r\n at MyClass.WriteInSheet(Int32 firNr, IList`1 omnData) in d:\Projects\MyApplication\App_Code\MyCode.cs:line...

Then I updated the followings via NuGet Package Manager:

Google.Apis from 1.36.1 to 1.41.1
Google.Apis.Auth 1.36.1 to 1.41.1
Google.Apis.Core 1.36.1 to 1.41.1
Google.apis.Sheets.v4 1.36.1.1428 to 1.41.1.1721
Newtonsoft.Json remains updated on 12.0.2

And I still got the exception.

I does not want to use Google Cloud Platform because is not free.

@jskeet
Copy link
Collaborator

jskeet commented Oct 2, 2019

Right - that sounds like the problem is missing assembly binding redirects. I suggest you check your config file for what assembly binding redirects are present, and see whether there's one for Newtonsoft.Json at all.

You may find that removing the Newtonsoft.Json dependency and then adding it again fixes the redirect. Or you could try deleting all the assembly binding redirects, and let VS recreate them for you. See this documentation for more details.

@csokmaib
Copy link
Author

csokmaib commented Oct 2, 2019

I uninstalled all Google.Apis packages and Newtonsoft too. I deleted all references for these packages. I reinstalled Newtonsoft first, then the rest of the Google.Apis packages, I checked my web.config to don't have any reference for these packages and the exception still appears.
The exception source is the Google.Apis.Core, it means that Google.Apis.Core is searching for the version 10.0.0.0 ? In other part of application where I use Newtonsoft, I don't have errors.

@jskeet
Copy link
Collaborator

jskeet commented Oct 2, 2019

Yes, Google.Apis.Core is built against 10.0. But that's where the assembly binding redirects come in - there should be one to tell the runtime "when something looks for 10.0, use 12.0.2 instead".

I've just tried this in a fresh ASP.NET MVC application, just "File / New Project", then adding Google.Apis.Core, then updating Newtonsoft.Json, and the assembly binding redirect appeared correctly. This is in web.config, within the assemblyBinding element:

<dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" />
    <bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.0.0" />
</dependentAssembly>

If you haven't got an element like that, you should just be able to add it in manually. It's a bit concerning that it wasn't generated automatically though - you may well have the same problem (with any package) in the future.

@csokmaib
Copy link
Author

csokmaib commented Oct 2, 2019

Unfortunately your suggestion not solved my problem. It's not working with these assembly binding redirects.
But I did the following steps and now it's working:

  • I downloaded from here the google-api-dotnet-client project
  • I updated manually the Newtonsoft.Json for Google.Apis, Google.Apis.Auth, Google.Apis.Core, I generated dlls and included them into Google.Apis.Sheets.v4
  • I uninstalled from Nuget package Google.Apis, Google.Apis.Auth, Google.Apis.Core and Google.Apis.Sheets.v4
  • I copied the generated dlls into my application for for Google.Apis, Google.Apis.Auth, Google.Apis.Core, Google.Apis.Sheets.v4

Thank you for your help!

@jskeet
Copy link
Collaborator

jskeet commented Oct 2, 2019

While I'm glad that's working for you, I don't think that's a sustainable approach. You'll have a lot of work to do any time you want to update the library, and you'll run into exactly the same problem if you later want to add any other dependencies that use a different version of Newtsoft.Json.

The assembly binding redirect should work, and I'd strongly encourage you to look further into why it's not working for you. I suspect this would be something specific to your project - without a way of reproducing the problem, there's nothing more we can do to help you.

I'm going to close this issue now, as I don't believe there's anything to do in this repo. If you want me to help you get to the bottom of the problem, I would suggest you try to reproduce the issue in a minimal way, and edit your Stack Overflow question so that anyone can reproduce it. We can then try to get the redirects working for you.

@jskeet jskeet closed this as completed Oct 2, 2019
@EgoPingvina
Copy link

I have the same problem.
I hav ASP Core project (.Net 5.0) with Newtonsoft.Json v13.0.1 and FirebaseAdmin v2.2.0 which uses Google.Apis.Auth with Newtonsoft v12.0.0 which does not exist.

@LindaLawton
Copy link
Collaborator

@jskeet has there been any talk about moving this library from Newtsoft.Json to System.Text.Json?

@jskeet
Copy link
Collaborator

jskeet commented Oct 18, 2021

@LindaLawton: We expose Newtonsoft.Json-specific types - changing it now would be a breaking change, requiring a major version bump. Maybe if we ever do create a new major version, it would be worth moving - but I'm not expecting that any time soon. (Note that System.Text.Json isn't available for .NET Framework prior to 4.6.1, and currently these libraries support 4.5 as well...)

@LindaLawton
Copy link
Collaborator

LindaLawton commented Oct 18, 2021

@jskeet as .NET Framework 4.5.2, 4.6, 4.6.1 will reach End of Support on April 26, 2022 does it matter if we still support 4.5?

Just a note for when you do decide to dump the version.

@jskeet
Copy link
Collaborator

jskeet commented Oct 18, 2021

@LindaLawton: Well, we'd have to see at that point - I'm just saying it's another reason why we couldn't do it right now.

We're not actually planning a major version bump anyway though, so please don't hold your breath :)

@EgoPingvina
Copy link

Sorry, but may be you have any ideas about my trouble?
What is interesting, when I run project on windows server all is fine, but after deploy on ubuntu it fire with this trouble.

Unhandled exception. System.InvalidOperationException: Error deserializing JSON credential data.
System.TypeInitializationException: The type initializer for 'Google.Apis.Json.NewtonsoftJsonSerializer' threw an exception.
System.IO.FileLoadException: Could not load file or assembly 'Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'. The located assembly's manifest definition does not match the assembly reference. (0x80131040)
File name: 'Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'
at Google.Apis.Json.NewtonsoftJsonSerializer..ctor()
at Google.Apis.Json.NewtonsoftJsonSerializer..cctor()
--- End of inner exception stack trace ---
at Google.Apis.Json.NewtonsoftJsonSerializer.get_Instance()
at Google.Apis.Auth.OAuth2.DefaultCredentialProvider.CreateDefaultCredentialFromStream(Stream stream)
--- End of inner exception stack trace ---
at Google.Apis.Auth.OAuth2.DefaultCredentialProvider.CreateDefaultCredentialFromStream(Stream stream)
at Google.Apis.Auth.OAuth2.GoogleCredential.FromStream(Stream stream)
at Google.Apis.Auth.OAuth2.GoogleCredential.FromFile(String path)
at Credion.Domain.Providers.FirebaseMessageProvider.Setup() in D:\dev\Backend\src\FirebaseMessageProvider.cs:line 26

@jskeet
Copy link
Collaborator

jskeet commented Oct 19, 2021

@EgoPingvina: Sorry, I thought I'd replied to you before; I must have been half way through and got distracted. Please file a new issue with more details. We can't really tell much just from what you've said. I assume there's a newer version being used somewhere and the library resolver isn't working properly for whatever reason - but until we can reproduce the issue or at least know more about the project structure, target framework version etc, it's going to be hard to help.

@EgoPingvina
Copy link

@jskeet

I have ASP Core project (.Net 5.0) with Newtonsoft.Json v13.0.1

I tried to downgrade Newtonsoft.Json to 12.0.3 and 12.0.1 - it didn't help. Also tried to downgrade FirebaseAdmin - didn't help too.

For deploy I use dotnet publish -c Release -r ubuntu.18.04-x64 --self-contained false --output "D:\dev\Backend\src\WebApp\bin\Linux"

@jskeet
Copy link
Collaborator

jskeet commented Oct 19, 2021

@EgoPingvina: Again, please file a new issue. If at all possible, it would be great for that new issue to contain a complete example so that we can reproduce the problem ourselves.

@EgoPingvina
Copy link

@jskeet #1983

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs more info This issue needs more information from the customer to proceed. type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

4 participants