Powershell: Could not load file or assembly error when loading a class library from a module in beta.3

Created on 6 Jul 2017  路  12Comments  路  Source: PowerShell/PowerShell

I have a C# Powershell module compiled with netcoreapp2.0 that uses a C# class also library compiled with netcoreapp2.0.

Since beta.3 when trying to load the class library from the module I am seeing a 'Could not load file or assembly' error. The same code works just fine in beta.2.

I have created an example case in the following repo with an example of the issue: https://github.com/barcexwiki/cannotloadassembly

In the repo, project 'psmodule' is a module that has a single cmdlet: Get-HelloWorld. This cmdlet calls an static method in a class in the 'classlibrary' project and outputs "Hello World from classlibrary!".

Steps to reproduce

D:\>git clone https://github.com/barcexwiki/cannotloadassembly.git
Cloning into 'cannotloadassembly'...
remote: Counting objects: 8, done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 8 (delta 0), reused 8 (delta 0), pack-reused 0
Unpacking objects: 100% (8/8), done.
Checking connectivity... done.

D:\>cd cannotloadassembly\psmodule

D:\cannotloadassembly\psmodule>dotnet restore
  Restoring packages for D:\cannotloadassembly\classlibrary\classlibrary.csproj...
  Restoring packages for D:\cannotloadassembly\psmodule\psmodule.csproj...
  Generating MSBuild file D:\cannotloadassembly\classlibrary\obj\classlibrary.csproj.nuget.g.props.
  Generating MSBuild file D:\cannotloadassembly\classlibrary\obj\classlibrary.csproj.nuget.g.targets.
  Restore completed in 296,18 ms for D:\cannotloadassembly\classlibrary\classlibrary.csproj.
  Generating MSBuild file D:\cannotloadassembly\psmodule\obj\psmodule.csproj.nuget.g.props.
  Generating MSBuild file D:\cannotloadassembly\psmodule\obj\psmodule.csproj.nuget.g.targets.
  Restore completed in 2,46 sec for D:\cannotloadassembly\psmodule\psmodule.csproj.

D:\cannotloadassembly\psmodule>dotnet build
Microsoft (R) Build Engine version 15.3.388.41745 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  classlibrary -> D:\cannotloadassembly\classlibrary\bin\Debug\netcoreapp2.0\classlibrary.dll
  psmodule -> D:\cannotloadassembly\psmodule\bin\Debug\netcoreapp2.0\psmodule.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:07.78


Expected behavior

Loading the module with beta.2 it loads the class library and the Get-HelloWorld cmdlet works.

D:\cannotloadassembly\psmodule>"c:\Program Files\PowerShell\6.0.0-beta.2\powershell.exe"
PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

PS D:\cannotloadassembly\psmodule> Import-Module D:\cannotloadassembly\psmodule\bin\Debug\netcoreapp2.0\psmodule.dll
PS D:\cannotloadassembly\psmodule> Get-HelloWorld
Hello World from classlibrary!

Actual behavior

Loading the module with beta.3 it loads the class library and the Get-HelloWorld cmdlet fails to load the class library.

D:\cannotloadassembly\psmodule>"c:\Program Files\PowerShell\6.0.0-beta.3\powershell.exe"
PowerShell v6.0.0-beta.3
Copyright (C) Microsoft Corporation. All rights reserved.

PS D:\cannotloadassembly\psmodule> Import-Module D:\cannotloadassembly\psmodule\bin\Debug\netcoreapp2.0\psmodule.dll
PS D:\cannotloadassembly\psmodule> Get-HelloWorld
Get-HelloWorld : Could not load file or assembly 'classlibrary, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null'. El sistema no puede encontrar el archivo especificado.
At line:1 char:1
+ Get-HelloWorld
+ ~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-HelloWorld], FileNotFoundException
    + FullyQualifiedErrorId : System.IO.FileNotFoundException,psmodule.GetHelloWorld

Environment data

for Powershell beta.2

> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      6.0.0-beta
PSEdition                      Core
BuildVersion                   3.0.0.0
CLRVersion
GitCommitId                    v6.0.0-beta.2
OS                             Microsoft Windows 10.0.15063
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

for Powershell beta.3

> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      6.0.0-beta
PSEdition                      Core
GitCommitId                    v6.0.0-beta.3
OS                             Microsoft Windows 10.0.15063
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

net version

```

dotnet --version
2.0.0-preview2-006497
````

Issue-Question Resolution-Answered

Most helpful comment

Verified that the repro works with the latest powershell:

PS:68> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      6.0.0-beta
PSEdition                      Core
GitCommitId                    v6.0.0-beta.3-30-gece27ff68798f508c755ccb84be5ae78b386ac82
OS                             Microsoft Windows 10.0.15063
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0


[F:\tmp\repro\cannotloadassembly\psmodule]
PS:69> Import-Module F:\tmp\repro\cannotloadassembly\psmodule\bin\Debug\netcoreapp2.0\psmodule.dll
[F:\tmp\repro\cannotloadassembly\psmodule]
PS:70> Get-HelloWorld
Hello World from classlibrary!

The beta.4 powershell will be released on 7/13.

All 12 comments

@daxian-dbw Could you please comment?

Hum! I'm having a similar issue as in Beta.2 can load the Microsoft.SqlServer.Smo assembly but not in Beta.3. And, is giving a similar error message stating the assembly is not found or not loaded.

Just wondering it is related!

:)

This is due to https://github.com/dotnet/corefx/issues/18791 (For an assembly loaded via 'Assembly.LoadFrom', the assembly it referenced cannot be loaded even if they are in the same folder). This has been addressed by moving PowerShell to .NET Core 2.0.0-preview3 (#4144).

I will double validate using the repro repo https://github.com/barcexwiki/cannotloadassembly.git and reply back.

Verified that the repro works with the latest powershell:

PS:68> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      6.0.0-beta
PSEdition                      Core
GitCommitId                    v6.0.0-beta.3-30-gece27ff68798f508c755ccb84be5ae78b386ac82
OS                             Microsoft Windows 10.0.15063
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0


[F:\tmp\repro\cannotloadassembly\psmodule]
PS:69> Import-Module F:\tmp\repro\cannotloadassembly\psmodule\bin\Debug\netcoreapp2.0\psmodule.dll
[F:\tmp\repro\cannotloadassembly\psmodule]
PS:70> Get-HelloWorld
Hello World from classlibrary!

The beta.4 powershell will be released on 7/13.

@daxian-dbw can we add a test for this?

@SteveL-MSFT Sure, will do.

As expected, it works in beta.4! Thanks!

@daxian-dbw can you post link to PR for added test here?

Thanks for bringing it up. It slipped off my radar ... The PR has been submitted.

@SteveL-MSFT @daxian-dbw

It still failing loading "Microsoft.SqlServer.Smo" assembly in Windows in Beta 4, but it works in Linux So, it's getting there!

@MaximoTrinidad why is it not working on Windows? Did you find the root cause? We would like to know if there is still a problem.

@daxian-dbw I'm getting the same error with smo. (I have SQL Server 2014 express with management studio installed)

I'm on windows 7 x 64 SP1, using the installed from the official page in github readme Windows 7 / Server 2008 R2 (x64)

See below:

7-version

8-error

Was this page helpful?
0 / 5 - 0 ratings

Related issues

andschwa picture andschwa  路  3Comments

rkeithhill picture rkeithhill  路  3Comments

MaximoTrinidad picture MaximoTrinidad  路  3Comments

alx9r picture alx9r  路  3Comments

aragula12 picture aragula12  路  3Comments