Skip to content

Workarounds in loading assembly with different versions #11571

@ritchxu

Description

@ritchxu

In our particular case, we have a custom shell around ConsoleShell that upon start up loads Microsoft.WindowsAzure.Storage.dll into the current app domain. Once the shell is up, we couldn't import for example Az.Accounts module because it depends on a different version of Microsoft.WindowsAzure.Storage.dll. Since this is a more general problem with PowerShell Core, I posted the issue here instead of over at Azure PowerShell. Effectively, I'm looking for a workaround for issue #2083.

Attempt 1:

custom assembly resolve

using Microsoft.PowerShell;
using System;
using System.Diagnostics;
using System.Reflection;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve;

            ConsoleShell.Start("Hello", "", new string[0] { });
        }

        private static Assembly OnAssemblyResolve(object sender, ResolveEventArgs args)
        {
            Debugger.Launch();
            return null;
        }
    }
}
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.PowerShell.SDK" Version="7.0.0-rc.1" />
  </ItemGroup>

  <ItemGroup>
    <Reference Include="Microsoft.PowerShell.ConsoleHost">
      <HintPath>$(NuGetPackageRoot)\microsoft.powershell.consolehost\7.0.0-rc.1\runtimes\win\lib\netcoreapp3.1\Microsoft.PowerShell.ConsoleHost.dll</HintPath>
    </Reference>
  </ItemGroup>

</Project>

Problem:

When run two import (Import-Module C:\version1\assembly.dll; Import-Module C:\version2\assembly.dll) with different versions of the assembly, I hit error of Import-Module: Assembly with same name is already loaded but the AssemblyResolve event was not raised.

Attempt 2:

binding redirect through config file

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="Microsoft.WindowsAzure.Storage" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-9.3.3.0" newVersion="9.3.3.0" />
  </dependentAssembly>
</assemblyBinding>

Problem:

Run into the same error above

Attempt 3:

run any external commands with Invoke-Command -Session, this basically out-sourced the command on a different app domain and avoid the version collision.

Problem:

This seems to be the only workaround I can come up with, but it's really inconvenient to jump back and forth PS sessions.

Question:

Any better workaround?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-AnsweredThe question is answered.WG-Enginecore PowerShell engine, interpreter, and runtime

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions