playwright-dotnet icon indicating copy to clipboard operation
playwright-dotnet copied to clipboard

Could not load file or assembly System.Text.Json originating from COM visible assembly

Open BasketGolfer opened this issue 4 years ago • 12 comments

Posting as a question, not a bug since my use case is unusual. Thank you in advance for any information.

I have an COM visible assembly (.dll) targeting .NET 4.7.2 that I register on target machine with with regasm /tlb /codebase I have COMVisible=True methods that call into Playwright methods.

When those COMVisible=True methods are called from a Winforms exe (also .Net 4.7.2) that references it, Playwright functions normally. This tells me everything on the machine is installed correctly.

However, if I call my "initialize Playwright" COM Visible method from Visual Basic for Applications (32 bit). I get System.IO.FileLoadException: Could not load file or assembly 'System.Text.Json, Version=6.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)

I set up Fusion logging and I can see that my paths for where .dll's live are correct (Expected Path is Here below)

OG: Appbase = file:///C:/.. Expected Path is Here LOG: Initial PrivatePath = NULL Calling assembly : Microsoft.Playwright, Version=1.20.0.0, Culture=neutral, PublicKeyToken=db12f80f85d8ba23.

LOG: This bind starts in default load context. LOG: No application configuration file found. 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=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51 LOG: Attempting download of new URL file:/// file:///C:/Expected Path is Here/System.Text.Json.DLL. WRN: Comparing the assembly name resulted in the mismatch: Revision Number ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

more of the Fusion log...

System.IO.FileLoadException: Could not load file or assembly 'System.Text.Json, Version=6.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) File name: 'System.Text.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' at Microsoft.Playwright.Transport.Connection..ctor() at Microsoft.Playwright.Playwright.d__0.MoveNext()

BasketGolfer avatar Mar 09 '22 14:03 BasketGolfer

Hey sorry for the late reply. Do you have a small repro which we can clone and run locally?

mxschmitt avatar Mar 18 '22 21:03 mxschmitt

Thanks for reaching out and sorry for the delay, I was out on vacation. Attached the zip in a separate comment below. Projects target 4.7.2 use x86 solution platform. .sln file stored in folder ReplicateDir projects stored in sub folders of ReplicateDir

There are two projects in the solution ReplicatePlaywright_SJT_COM (Replicate) DemoCallReplicatePlaywright_SJT_COM (Demo)

If Replicate is run directly, Playwright will initialize successfully and would work (see note below before running!) If Demo is run (click test button), simply creates an instance of Replicate - you’ll eventually see the System.Text.JSON error when it spins up Playwright.

Note: I needed to copy a .playwright folder into …ReplicateDir\DemoCallReplicatePlaywright_SJT_COM\bin

see comment in the code with the error thrown if that folder does not exist. I have seen a feature request about being able specify the .playwright folder location somehow, which would be a nice addition.

Again thank you very much for looking at this!

BasketGolfer avatar Mar 23 '22 21:03 BasketGolfer

Didn't see the zip file attach - adding ReplicateDir.zip .

BasketGolfer avatar Mar 24 '22 11:03 BasketGolfer

Sorry for the late response, I was out of office.

I tried to reproduce it, but was not able to. I followed the steps in the repro which included cleaning the project (remove bin and obj folder), restoring the nuget dependencies, building the project and running. I did not have to manually copy the .playwright folder over, rebuilding seems to work for me.

Platform: Windows 11, VS 2022. Since I didn't modify anything, I guess net47 was used like in your use-case.

mxschmitt avatar Apr 05 '22 12:04 mxschmitt

Hum... I just tried the same thing you described, extracting the code onto a different machine. If I run ReplicatePlaywright_SJT_COM works perfectly. If I run DemoCallReplicatePlaywright_SJT_COM I get the same JSON error.

BasketGolfer avatar Apr 07 '22 14:04 BasketGolfer

Still trying to resolve this problem. For those following this there are two projects, one that calls an assembly (ReplicatePlaywright_SJT_COM) that does Playwright stuff. Calling the assembly from a different project DemoCallReplicatePlaywright_SJT_COM fails. Calling the class from a form within ReplicatePlaywright_SJT_COM works.

Some more data. I uninstalled the playwright Nuget package in ReplicatePlaywright_SJT_COM. Then added System.Json.Text v 6.0.0 since that's the version the error references. Added Nuget package back got Warning NU1605 Detected package downgrade: System.Text.Json from 6.0.1 to 6.0.0. When I ran, DemoCallReplicatePlaywright_SJT_COM The error changed now its: Could not load file or assembly 'System.ComponentModel.Annotations, Version=4.2.0.0

I think the root cause is that the Microsoft.Playwright Nuget package does not have a "framework" folder (e.g. 4.61) in the lib directory. and this cascades problems trying to get the relevant dll's necessary for this to work. I've tried manually getting the "matching" dll's from each package's lib folder. But there are many different mismatchs -each of the depending packages each have different folders under lib. I can see why this is difficult to get to work its a real mess. I wish there were just specific downloads of all the needed DLL's vs using Nuget.

BasketGolfer avatar Apr 27 '22 13:04 BasketGolfer

Hey I was developing a WPF application in .NET 5 and was having a similar error and finally just fixed it. I ended up clearing my NuGet cache, downgrading to .NET 3.1, restarting my computer, and that worked. I know it's not super specific, but I thought maybe it might help someone.

Julian-C10 avatar May 26 '22 01:05 Julian-C10

I was trying something similar to what Julian-C10 did. After clearing everything and re-addin Playwright nuget I see this in the output - when I have "fixed" the System.Text.JSON 6.0 error - then I get errors referencing System.Runtime.CompilerServices.Unsafe as described above.

The question I have is why is this mismatching of versions happening at all? Is there a way to simplify what Playwright needs dependency wise?

Installed System.Runtime.CompilerServices.Unsafe 6.0.0 from https://api.nuget.org/v3/index.json with content hash /iUeP3tq1S0XdNNoMz5C9twLSrM/TH+qElHkXWaPvuNOt+99G75NrV0OS2EqHx5wMN7popYjpc8oTjC1y16DLg==. Installed System.Runtime.CompilerServices.Unsafe 4.5.3 from https://api.nuget.org/v3/index.json with content hash 3TIsJhD1EiiT0w2CcDMN/iSSwnNnsrnbzeVHSKkaEgV85txMprmuO+Yq2AdSbeVGcg28pdNDTPK87tJhX7VFHw==.

BasketGolfer avatar May 26 '22 09:05 BasketGolfer

I've created a work around by putting a .config file with the below binding redirects into the directory of the application that is calling my playwright enabled assembly. This one fixes the System.Text.JSON missmatch - its shipping the 6.0.0.1 dll but the package information is somehow saying 6.0.0.0 assemblyIdentity name="System.Text.Json" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.1"

But one you address that issue, you then need this: assemblyIdentity name="System.ComponentModel.Annotations" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" bindingRedirect oldVersion="0.0.0.0-4.2.1.0" newVersion="4.2.1.0"

BasketGolfer avatar May 26 '22 17:05 BasketGolfer

Closing since it seems working with the workaround above. We also bumped the JSON dependency etc. so it should not yield the same error anymore.

For further issues please file a new repro.

mxschmitt avatar Jul 27 '22 11:07 mxschmitt

Hi I am facing the same error, what context info should I provide?

BrennanOwYong avatar Aug 12 '23 18:08 BrennanOwYong

Same problem started after installing sdk 6.0.428. After uninstalling it the problem was fixed.

elmy33 avatar Apr 11 '25 20:04 elmy33