PowerShell / PowerShell

PowerShell for every system!
https://microsoft.com/PowerShell
MIT License
43.56k stars 7.06k forks source link

"Could not load file or assembly 'System.ServiceModel.Primitives, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'." #21527

Closed prajasek-netapp closed 2 weeks ago

prajasek-netapp commented 3 weeks ago

Prerequisites

Steps to reproduce

I am not able to load the module, which has System.ServiceModel.Primitives dependency.

Simply load the module in the powershell 7. Example:-

[System.Reflection.Assembly]::LoadFrom('< path to the folder contains System.ServiceModel.Primitives.dll>')

Add-Type -Path "< path to the folder contains System.ServiceModel.Primitives.dll>"

Both the commands results in Could not load file or assembly 'System.ServiceModel.Primitives, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

Expected behavior

PS C:\Users\Administrator> [System.Reflection.Assembly]::LoadFrom('C:\_RajRepo\Master\orion\cliffhanger\Bin\Debug\net8.0\publish\win-x64\System.ServiceModel.Primitives.dll')

PS C:\Users\Administrator> Add-Type -Path "C:\_RajRepo\Master\orion\cliffhanger\Bin\Debug\net8.0\publish\win-x64\System.ServiceModel.Primitives.dll"

Expectation - should load the dll

Actual behavior

PS C:\Users\Administrator> [System.Reflection.Assembly]::LoadFrom('C:\_RajRepo\Master\orion\cliffhanger\Bin\Debug\net8.0\publish\win-x64\System.ServiceModel.Primitives.dll')
MethodInvocationException: Exception calling "LoadFrom" with "1" argument(s): "Could not load file or assembly 'System.ServiceModel.Primitives, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'."
PS C:\Users\Administrator> Add-Type -Path "C:\_RajRepo\Master\orion\cliffhanger\Bin\Debug\net8.0\publish\win-x64\System.ServiceModel.Primitives.dll"
Add-Type: Could not load file or assembly 'System.ServiceModel.Primitives, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

Error details

Could not load file or assembly 'System.ServiceModel.Primitives, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

Environment data

PS C:\Users\Administrator> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.4.1
PSEdition                      Core
GitCommitId                    7.4.1
OS                             Microsoft Windows 10.0.20348
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

rhubarb-geek-nz commented 3 weeks ago

Hi, I suggest it is a conflict with PowerShell's version of the library. It own's the process and is likely to have loaded that first.

PS C:\Program Files\PowerShell\7> (Get-Item -LiteralPath System.ServiceModel.Primitives.dll).VersionInfo

ProductVersion   FileVersion      FileName
--------------   -----------      --------
3.4.3            4.1000.323.51101 C:\Program Files\PowerShell\7\System.ServiceModel.Primitives.dll

If you are just writing a script then you have a major challenge, but if you are writing a C #cmdlet then you can set up an Assembly Load Context.

The problem is a single load context can only load one version of a named shared assembly. So you can create a child load context and load your conflicting assemblies into that.

I have just just gone through this process with one of my modules, where I load the Postgres Database Connection into the child Assembly Load Context but can share objects created within that load context through my cmdlet, the Alc module is the bridging assembly that allows objects to be shared across the divide.

The assemblies within the lib directory run in the child Alc.

microsoft-github-policy-service[bot] commented 2 weeks ago

📣 Hey @prajasek-netapp, how did we do? We would love to hear your feedback with the link below! 🗣️

🔗 https://aka.ms/PSRepoFeedback

Microsoft Forms
prajasek-netapp commented 2 weeks ago

Thanks, it worked.