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

Could not load file or assembly 'Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' #1983

Closed
EgoPingvina opened this issue Oct 19, 2021 · 6 comments
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@EgoPingvina
Copy link

Environment details

  • Programming language: C#
  • OS: Ubuntu 18.04-x64
  • Language runtime version: .Net 5.0
  • Package version: Google.Apis.Auth v1.4.9

Steps to reproduce

  1. publish ASP project dotnet publish -c Release -r ubuntu.18.04-x64 --self-contained false --output "D:\dev\Backend\src\WebApp\bin\Linux"
  2. copy to folder on ubuntu server \var\www\backend
  3. start service
    sudo systemctl enable kestrel-credion-backend.service
    sudo systemctl start kestrel-credion-backend.service
    systemctl daemon-reload
  4. see an error on sudo systemctl status kestrel-credion-backend.service and the full exception with stacktrace in logs \var\log\syslog

Details

I have an ASP Core project (.Net 5.0) with Newtonsoft.Json v13.0.1 and FirebaseAdmin v2.2.0 which uses Google.Apis.Auth v1.4.9 which depends on Newtonsoft v12.0.0.

I create build for deploy by comand dotnet publish -c Release -r ubuntu.18.04-x64 --self-contained false --output "D:\dev\Backend\src\WebApp\bin\Linux" and copy Linux folder's content into /var/www/backend.

There is service for it at /etc/systemd/system:

[Unit]
Description=Credion backend

[Service]
WorkingDirectory=/var/www/backend
ExecStart=/usr/bin/dotnet /var/www/backend/WebApp.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-example
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target

and after it restart service by:
sudo systemctl enable kestrel-credion-backend.service
sudo systemctl start kestrel-credion-backend.service
systemctl daemon-reload

If check status sudo systemctl status kestrel-credion-backend.service you will see an error Process: 21075 ExecStart=/usr/bin/dotnet /var/www/backend/WebApp.dll (code=killed, signal=ABRT).

For details go to log file syslog at /var/log and you can see:

Oct 19 15:15:30 credionix dotnet-example[17385]: Unhandled exception. System.InvalidOperationException: Error deserializing JSON credential data.
Oct 19 15:15:30 credionix dotnet-example[17385]:  ---> System.TypeInitializationException: The type initializer for 'Google.Apis.Json.NewtonsoftJsonSerializer' threw an exception.
Oct 19 15:15:30 credionix dotnet-example[17385]:  ---> 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)
Oct 19 15:15:30 credionix dotnet-example[17385]: File name: 'Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'
Oct 19 15:15:30 credionix dotnet-example[17385]:    at Google.Apis.Json.NewtonsoftJsonSerializer..ctor()
Oct 19 15:15:30 credionix dotnet-example[17385]:    at Google.Apis.Json.NewtonsoftJsonSerializer..cctor()
Oct 19 15:15:30 credionix dotnet-example[17385]:    --- End of inner exception stack trace ---
Oct 19 15:15:30 credionix dotnet-example[17385]:    at Google.Apis.Json.NewtonsoftJsonSerializer.get_Instance()
Oct 19 15:15:30 credionix dotnet-example[17385]:    at Google.Apis.Auth.OAuth2.DefaultCredentialProvider.CreateDefaultCredentialFromStream(Stream stream)
Oct 19 15:15:30 credionix dotnet-example[17385]:    --- End of inner exception stack trace ---
Oct 19 15:15:30 credionix dotnet-example[17385]:    at Google.Apis.Auth.OAuth2.DefaultCredentialProvider.CreateDefaultCredentialFromStream(Stream stream)
Oct 19 15:15:30 credionix dotnet-example[17385]:    at Google.Apis.Auth.OAuth2.GoogleCredential.FromStream(Stream stream)
Oct 19 15:15:30 credionix dotnet-example[17385]:    at Google.Apis.Auth.OAuth2.GoogleCredential.FromFile(String path)
Oct 19 15:15:30 credionix dotnet-example[17385]:    at Credion.Domain.Providers.FirebaseMessageProvider.Setup() in D:\dev\Backend\src\.layers\Domain\Providers\Messages\FirebaseMessageProvider.cs:line 26
Oct 19 15:15:30 credionix dotnet-example[17385]:    at Credion.App.Program.<>c.<CreateHostBuilder>b__3_3(IWebHostBuilder web) in D:\dev\Backend\src\WebApp\Program.cs:line 65
Oct 19 15:15:30 credionix dotnet-example[17385]:    at Microsoft.Extensions.Hosting.GenericHostBuilderExtensions.<>c__DisplayClass0_0.<ConfigureWebHostDefaults>b__0(IWebHostBuilder webHostBuilder)
Oct 19 15:15:30 credionix dotnet-example[17385]:    at Microsoft.Extensions.Hosting.GenericHostWebHostBuilderExtensions.ConfigureWebHost(IHostBuilder builder, Action`1 configure, Action`1 configureWebHostBuilder)
Oct 19 15:15:30 credionix dotnet-example[17385]:    at Microsoft.Extensions.Hosting.GenericHostWebHostBuilderExtensions.ConfigureWebHost(IHostBuilder builder, Action`1 configure)
Oct 19 15:15:30 credionix dotnet-example[17385]:    at Microsoft.Extensions.Hosting.GenericHostBuilderExtensions.ConfigureWebHostDefaults(IHostBuilder builder, Action`1 configure)
Oct 19 15:15:30 credionix dotnet-example[17385]:    at Credion.App.Program.CreateHostBuilder(String[] args) in D:\dev\Backend\src\WebApp\Program.cs:line 31
Oct 19 15:15:30 credionix dotnet-example[17385]:    at Credion.App.Program.Main(String[] args) in D:\dev\Backend\src\WebApp\Program.cs:line 100
Oct 19 15:15:30 credionix systemd[1]: kestrel-credion-backend.service: Main process exited, code=killed, status=6/ABRT
Oct 19 15:15:30 credionix systemd[1]: kestrel-credion-backend.service: Failed with result 'signal'.

As we see error fires at FirebaseMessageProvider.cs:line 26 which is folowing:

26      _ = FirebaseApp.Create(
27             new AppOptions()
28              {
29                  Credential = GoogleCredential.FromFile(path),
30              });

Downgrade Newtonsoft.Json or append

  <PropertyGroup>
      <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
  </PropertyGroup>

  <Target Name="ForceGenerationOfBindingRedirects"
          AfterTargets="ResolveAssemblyReferences"
          BeforeTargets="GenerateBindingRedirects"
          Condition="'$(AutoGenerateBindingRedirects)' == 'true'">
    <PropertyGroup>
      <!-- Needs to be set in a target because it has to be set after the initial evaluation in the common targets -->
      <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
    </PropertyGroup>
  </Target>

didn't help.

Important remark: on Win10 and Windows Server all works fine.

@EgoPingvina EgoPingvina added priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. labels Oct 19, 2021
@jskeet
Copy link
Collaborator

jskeet commented Oct 19, 2021

I'd really like to try to reproduce this in a completely minimal fashion.

Two questions:

  • If you run it just by going to the published directory and using dotnet [name of dll] does it still fail? (I'd like to get all the systemd stuff out of the way if at all possible.)
  • If you change the Main method to call Google.Apis.Json.NewtonsoftJsonSerializer.CreateDefaultSettings(), I'd expect that to fail nice and early - does it?

If you could post the answers to these questions along with a project file, that would really help.

Note that you could also add an explicit dependency on Google.Apis.Auth version 1.55.0.

@EgoPingvina
Copy link
Author

dotnet [name of dll] returns the same error

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'
...

@jskeet
Copy link
Collaborator

jskeet commented Oct 20, 2021

Excellent - that should make it simpler to test, for one thing. If we can get this down to a minimal example (which could be expressed in terms of "start with dotnet new web, then modify the project file like this, then add this code") then we'll be able to make progress.

@EgoPingvina
Copy link
Author

add an explicit dependency on Google.Apis.Auth version 1.55.0.

doesn't help too

@EgoPingvina
Copy link
Author

https://stackoverflow.com/questions/45242353/dotnet-publish-outputs-old-packages
the problem in --output arg of dotnet deploy. I really hove no ideas, why when we use it, builder starts to cope old versions of dll and from where it takes them, but without --output it works.

@jskeet
Copy link
Collaborator

jskeet commented Oct 20, 2021

Okay, that sounds very odd, but also not something specific to this library, and not something we'd be able to address here.

Best of luck with implementing the workaround.

Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

No branches or pull requests

2 participants