selenium: [🐛 Bug]: OneTimeSetUp: System.IO.FileLoadException : Could not load file or assembly 'System.Text.Json, Version=8.0.0.0

What happened?

Ever since updating Selenium.WebDriver past 4.23.0, running integration tests fails on the OneTimeSetUp. To be clear, I can run my full integration test suite on 4.23.0, but I get the same setup failures on 4.24.0 and 4.25.0. For some reason it seems to still be looking for System.Text.Json, Version=8.0.0.0 even though the dependency requires 8.0.4. I notice that when trying to install 8.0.0 manually, it says there is a high severity vulnerability which is why I assume the update to require 8.0.4 exists.

Something is still stuck trying to download System.Text.Json 8.0.0 even after the update of the webdriver beyond 4.23.0

How can we reproduce the issue?

Install Selenium.WebDriver v4.25.0 and try to run integration test suite.

Relevant log output

Message: 
OneTimeSetUp: System.IO.FileLoadException : Could not load file or assembly 'System.Text.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

  Stack Trace: 
SeleniumManager.BinaryPaths(String arguments)
DriverFinder.BinaryPaths()
DriverFinder.GetDriverPath()
ChromiumDriver.GenerateDriverServiceCommandExecutor(DriverService service, DriverOptions options, TimeSpan commandTimeout)
ChromiumDriver.ctor(ChromiumDriverService service, ChromiumOptions options, TimeSpan commandTimeout)
ChromeDriver.ctor(ChromeOptions options)
Setup.Init() line 42

Operating System

Windows 11

Selenium version

Selenium.WebDriver v4.25.0

What are the browser(s) and version(s) where you see this issue?

Chrome

What are the browser driver(s) and version(s) where you see this issue?

Selenium.WebDriver.ChromeDriver v129.0.6668.10000

Are you using Selenium Grid?

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 16 (9 by maintainers)

Most upvoted comments

Yes, exactly. For those users targeting full framework, it will be necessary to ensure that they’re either placing the redirects manually or re-working their project files to automatically generate them.

I have run into a very similar error as originally reported, but, at least in my exact case, this does not appear to be directly related to the Selenium assembly or packaging. In my case, taking on 4.26.1 (from a 3.x version) introduced the issue in the context of running some legacy MSTest v1 .orderedtest suites. I’ve since dropped back to 4.23.0 for the moment to work around this.

I have distilled down a reproducible example, which I’ll discuss in “Issue 2”, below. The key point here is most likely that we’re targeting .NET 4.8. A secondary indicator is apt to be not automatically producing the assembly binding redirects.

I have created what should be a pretty straightforward (and hopefully consistent) repro case and pushed it to a repository - kcamp/GH-14600-selenium-4-26-1-repro

Details of my machine -

OS Name:                   Microsoft Windows 10 Enterprise
OS Version:                10.0.19045 N/A Build 19045
Microsoft Visual Studio Professional 2022 (64-bit) - Current Version 17.10.6
Microsoft .NET Framework Version 4.8.09037

I have a few data points to share and expand on and will try to break them up as best I can 👍

Issue 1 - failure due to System.Text.Json - MSTest Deployment

First, the outright failure stemming from my [very specific!] MSTest scenario. This is a secondary presentation and in my opinion, is 100% unrelated to the issue in question, nor is it any sort of root cause. It’s simply a presentation of an error that is an unfortunate red-herring. I do want to share it for completeness and in case it helps others start an investigation of their own.

The stack trace:

System.IO.FileNotFoundException: Could not load file or assembly 'System.Text.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified.
File name: 'System.Text.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
   at OpenQA.Selenium.Command.get_ParametersAsJsonString()
   at OpenQA.Selenium.Remote.HttpCommandExecutor.HttpRequestInfo..ctor(Uri serverUri, Command commandToExecute, HttpCommandInfo commandInfo)
   at OpenQA.Selenium.Remote.HttpCommandExecutor.<ExecuteAsync>d__34.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 OpenQA.Selenium.Remote.DriverServiceCommandExecutor.<ExecuteAsync>d__10.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 OpenQA.Selenium.WebDriver.<ExecuteAsync>d__63.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 OpenQA.Selenium.WebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
   at OpenQA.Selenium.WebDriver.StartSession(ICapabilities capabilities)
   at OpenQA.Selenium.WebDriver..ctor(ICommandExecutor executor, ICapabilities capabilities)
   at OpenQA.Selenium.Chrome.ChromeDriver..ctor(ChromeDriverService service, ChromeOptions options, TimeSpan commandTimeout)
   at MyNameSpace.InstantiateBrowser(IWebDriver& browser, BrowserType browserType, String downloadPath)

I could clearly see that the [presumably correct] System.Text.Json.dll assembly was in the \bin\Debug folder. Enabling fusion logging showed the issue to be the attempts to probe the shadow/deployment directory. MSTest is [https://stackoverflow.com/questions/19649632/all-assemblies-are-not-getting-copied-in-test-result-ms-test](not copying) the System.Text.Json.dll to that folder, which is leading to the failure. We can see the dump from fusion, below.

*** Assembly Binder Log Entry  (11/21/2024 @ 11:14:22 AM) ***

The operation failed.
Bind result: hr = 0x80070002. The system cannot find the file specified.

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable  C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\QTAgent32_40.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: DisplayName = System.Text.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51
 (Fully-specified)
LOG: Appbase = file:///C:/Path/To/TestProject/bin/debug/TestResults/Principal_HOSTNAME 2024-11-21 11_13_57/Out
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = QTAgent32_40.exe
Calling assembly : WebDriver, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Path\To\TestProject\bin\debug\TestResults\Principal_HOSTNAME 2024-11-21 11_13_57\Out\TestAssemblyName.DLL.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: System.Text.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///C:/Path/To/TestProject/bin/debug/TestResults/Principal_HOSTNAME 2024-11-21 11_13_57/Out/System.Text.Json.DLL.
LOG: Attempting download of new URL file:///C:/Path/To/TestProject/bin/debug/TestResults/Principal_HOSTNAME 2024-11-21 11_13_57/Out/System.Text.Json/System.Text.Json.DLL.
LOG: Attempting download of new URL file:///C:/Path/To/TestProject/bin/debug/TestResults/Principal_HOSTNAME 2024-11-21 11_13_57/Out/System.Text.Json.EXE.
LOG: Attempting download of new URL file:///C:/Path/To/TestProject/bin/debug/TestResults/Principal_HOSTNAME 2024-11-21 11_13_57/Out/System.Text.Json/System.Text.Json.EXE.
LOG: All probing URLs attempted and failed.

📣 For anyone finding this and attempting to debug through it, the best thing to do is probably going to be to enable fusion logging to better understand what exactly is being probed/discovered and what the underlying mismatch might be.

  • In the case of @Guitrum they could see what is/is not resolving.
  • In the case of @mvaneijken I’d enable fusion logging for all assembly binds to see what version of the assembly is loading, which may speak to what the type initialization exception might be about.

Issue 2 - Visual Studio Test Explorer failure due to System.Runtime.CompilerServices.Unsafe

In an effort to create a repro case, “out of the box” in a new project, I failed. Visual Studio appears to do what’s right and it all works. I tried both net8.0 and net48. However, some more digging and thinking and I was able to get it. This goes back to the explicit suppression of the assembly binding redirect generation, mentioned above. Absent the redirects, this is the exception I get when running MSTest.TestFramework tests in VS2022 Test Explorer and presents with the following failure:

  Message: 
Initialization method selenium_system_text_json_repro.SeleniumSystemTextJsonRepro.Setup threw exception. System.IO.FileLoadException: Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040).
TestCleanup method selenium_system_text_json_repro.SeleniumSystemTextJsonRepro.TearDown threw exception. System.NullReferenceException: Object reference not set to an instance of an object..

  Stack Trace: 
MemoryExtensions.AsSpan(String text)
JsonEncodedText.Encode(String value, JavaScriptEncoder encoder)
SeleniumManagerSerializerContext.cctor()
TestCleanup Stack Trace
SeleniumSystemTextJsonRepro.TearDown() line 55

This error is suppressed (at least for me) when creating a fresh .csproj and the project automatically generates assembly binding redirects. I had to specify <AutoGenerateBindingRedirects>false</AutoGenerateBindingRedirects> in my repro project to emulate the example I am seeing in our product regression tests.

It is interesting, though, that if we are observing this in the fusion log, we can also see that there’s a assembly load error reported for System.Text.Json, it’s just not bubbling to the top of the test failure.

While the error surfacing in the runner is related to System.Runtime.CompilerServices.Unsafe, we can see fusion logs for both that assembly and System.Text.Json both.

System.Runtime.CompilerServices.Unsafe

*** Assembly Binder Log Entry  (11/22/2024 @ 9:49:58 AM) ***

The operation failed.
Bind result: hr = 0x80131040. No description available.

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable  D:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\IDE\Extensions\TestPlatform\testhost.net48.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: DisplayName = System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
 (Fully-specified)
LOG: Appbase = file:///C:/Users/kcamp/source/repos/selenium-repro/selenium-system-text-json-repro-ms/bin/Debug/net48
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = NULL
Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: D:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\IDE\Extensions\TestPlatform\testhost.net48.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: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///C:/Users/kcamp/source/repos/selenium-repro/selenium-system-text-json-repro-ms/bin/Debug/net48/System.Runtime.CompilerServices.Unsafe.DLL.
LOG: Assembly download was successful. Attempting setup of file: C:\Users\kcamp\source\repos\selenium-repro\selenium-system-text-json-repro-ms\bin\Debug\net48\System.Runtime.CompilerServices.Unsafe.dll
LOG: Entering run-from-source setup phase.
LOG: Assembly Name is: System.Runtime.CompilerServices.Unsafe, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
WRN: Comparing the assembly name resulted in the mismatch: Major Version
ERR: The assembly reference did not match the assembly definition found.
ERR: Run-from-source setup phase failed with hr = 0x80131040.
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

System.Text.Json

*** Assembly Binder Log Entry  (11/22/2024 @ 9:49:58 AM) ***

The operation failed.
Bind result: hr = 0x80131040. No description available.

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable  D:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\IDE\Extensions\TestPlatform\testhost.net48.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: DisplayName = System.Text.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51
 (Fully-specified)
LOG: Appbase = file:///C:/Users/kcamp/source/repos/selenium-repro/selenium-system-text-json-repro-ms/bin/Debug/net48
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = NULL
Calling assembly : WebDriver, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: D:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\IDE\Extensions\TestPlatform\testhost.net48.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: Post-policy reference: System.Text.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///C:/Users/kcamp/source/repos/selenium-repro/selenium-system-text-json-repro-ms/bin/Debug/net48/System.Text.Json.DLL.
LOG: Assembly download was successful. Attempting setup of file: C:\Users\kcamp\source\repos\selenium-repro\selenium-system-text-json-repro-ms\bin\Debug\net48\System.Text.Json.dll
LOG: Entering run-from-source setup phase.
LOG: Assembly Name is: System.Text.Json, Version=8.0.0.4, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51
WRN: Comparing the assembly name resulted in the mismatch: Revision Number
ERR: The assembly reference did not match the assembly definition found.
ERR: Run-from-source setup phase failed with hr = 0x80131040.
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

To make the test work, we can add in the assembly binding redirect for just System.Runtime.CompilerServices.Unsafe (below). It is important to note that we will still see the fusion log for the failed resolution of System.Text.Json, but this seems to be a non-critical issue, at least in the simplified case. I suspect that adding the redirect for System.Text.Json would be the correct thing to do, despite our stripped down example not seeming to have issues.

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

Issue 3 - VSTest.Console failure due to System.Text.Json

An interesting behavior emerged when I was authoring some Cake scripts to help your team reproduce these - I was able to accurately reproduce the behavior seen in NUnit (below, Issue 4) from the console in VSTest. It turns out that in Issue 2, as I had alluded to, we need to place the redirect for System.Text.Json as well to make the tests run from the command line.

========================================
Test-VSTest
========================================
Executing task: Test-VSTest
Executing: "C:/Users/kcamp/source/repos/selenium-repro/tools/Microsoft.TestPlatform.17.12.0/tools/net462/Common7/IDE/Extensions/TestPlatform/vstest.console.exe" "C:/Users/kcamp/source/repos/selenium-repro/selenium-system-text-json-repro-ms/bin/Debug/net48/selenium-system-text-json-repro-ms.dll"
VSTest version 17.12.0 (x64)

Starting test execution, please wait...
A total of 1 test files matched the specified pattern.
  Failed BasicChromeTest [317 ms]
  Error Message:
   Initialization method selenium_system_text_json_repro_ms.SeleniumSystemTextJsonRepro.Setup threw exception. System.IO.FileLoadException: Could not load file or assembly 'System.Text.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040).
TestCleanup method selenium_system_text_json_repro_ms.SeleniumSystemTextJsonRepro.TearDown threw exception. System.NullReferenceException: Object reference not set to an instance of an object..
  Stack Trace:
      at OpenQA.Selenium.SeleniumManager..cctor()

TestCleanup Stack Trace
   at selenium_system_text_json_repro_ms.SeleniumSystemTextJsonRepro.TearDown() in C:\Users\kcamp\source\repos\selenium-repro\selenium-system-text-json-repro-ms\SeleniumSystemTextJsonRepro.cs:line 55

Adding both redirects resolves this error:

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Text.Json" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-8.0.0.4" newVersion="8.0.0.4" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

Issue 4 - NUnit failure due to System.Text.Json

Given what I learned in the second issue, I was able to reproduce (I am pretty sure) the exact case that @Guitrum reported.

  Message: 
OneTimeSetUp: System.TypeInitializationException : The type initializer for 'OpenQA.Selenium.SeleniumManager' threw an exception.
  ----> System.IO.FileLoadException : Could not load file or assembly 'System.Text.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

  Stack Trace: 
SeleniumManager.BinaryPaths(String arguments)
DriverFinder.BinaryPaths()
DriverFinder.GetDriverPath()
ChromiumDriver.GenerateDriverServiceCommandExecutor(DriverService service, DriverOptions options, TimeSpan commandTimeout)
ChromiumDriver.ctor(ChromiumDriverService service, ChromiumOptions options, TimeSpan commandTimeout)
ChromeDriver.ctor(ChromeDriverService service, ChromeOptions options, TimeSpan commandTimeout)
SeleniumSystemTextJsonRepro.Setup() line 49
--FileLoadException
SeleniumManager.cctor()

This one, unlike the previous issue, required an assembly binding redirect for both assemblies in order to work, but once in place, the test does work without issue.

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Text.Json" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-8.0.0.4" newVersion="8.0.0.4" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

Hope this helps, if there is anything I can help to clarify, please tag me.