Skip to content

[BUG] Error with colliding assemblies when using Start-ThreadJob  #3207

@AndersRask

Description

@AndersRask

Reporting an Issue or Missing Feature

Issue

Expected behavior

According to the note in the start of this article: https://pnp.github.io/powershell/articles/azurefunctions.html
image
We should use Start-ThreadJob as a way to mitigate the assembly load issue that especially now with .NET 6 has been an issue when using PnP.PowerShell with other modules, like Microsoft.Identity.Client in Az.Account

When I try the examples in the above article with Start-Job in PowerShell 5.1 it works fine, but as soon as I use PowerShell 7 it fails with the dreaded "Assembly with same name is already loaded"

I believe this is because the way AppDomains work in .NET Framework vs .NET Core

On a related note:
_I know you did a lot to prevent these issues when upgrading to 2.1.1 but did you consider ALC bridging?
ALC is the "new" way of loading assemblies in PowerShell 7, and should have better ways of handling multiple versions of the same assembly. There are a few samples in the code below that show how to avoid this breaking code into two modules and using the IModuleAssemblyInitializer to lazy load and undload assemblies into ALC.

https://learn.microsoft.com/en-us/powershell/scripting/dev-cross-plat/resolving-dependency-conflicts?view=powershell-7.3#more-robust-solutions

Would love to hear your thoughts? :)_

Actual behavior

When using Start-ThreadJob in PowerShell 7 we were expecting to be able to use Az along with PnP.PowerShell

Steps to reproduce behavior

$script = {
      Connect-PnPOnline tenantname.sharepoint.com/sites/test -Interactive
      $web = Get-PnPWeb
      $web.Title}
$webtitle = Start-ThreadJob -ScriptBlock $script | Wait-Job | Receive-Job
# webtitle returns title:
$webtitle
PS> test123

Also a observe that loaded modules show that modules are indeed not loaded, but checking the appdomain for assemblies reveal that pnp powershell are indeed loaded witch will cause trouble later:

[AppDomain]::CurrentDomain.GetAssemblies() | ? FullName -like *pnp.powershell* | select fullname
PS> PnP.PowerShell, Version=2.1.1.0, Culture=neutral, PublicKeyToken=null
PS> PnP.PowerShell.ALC, Version=2.1.1.0, Culture=neutral, PublicKeyToken=null

Now if we try to run an Az command like Get-AzADUser in a seperate job look what happens:

$getUsers = {
    Connect-AzAccount -Tenantid $tenantId -SubscriptionId $subid | out-null
   Get-AzAdUser
}
Start-ThreadJob -ScriptBlock $getUsers | Wait-Job | Receive-Job

The 'Get-AzADUser' command was found in the module 'Az.Resources', but the module could not be loaded due to the following error: [Assembly with same name is already loaded]
For more information, run 'Import-Module Az.Resources'.

What is the version of the Cmdlet module you are running?

PowerShell 7.3.4
PnP.PowerShell 2.1.1
Az.Resources 3.3.0

Which operating system/environment are you running PnP PowerShell on?

  • [x ] Windows
  • Linux
  • MacOS
  • Azure Cloud Shell
  • Azure Functions
  • Other : please specify

Metadata

Metadata

Assignees

No one assigned

    Labels

    azure-automationIncludes Azure Functions or Azure RunbooksbugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions