Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could not load file or assembly or one of its dependencies in PowerShell Module project target to netstandard 2.0 #113006

Closed
hanqiang2 opened this issue Feb 28, 2025 · 7 comments
Labels
area-Meta needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners question Answer questions and provide assistance, not an issue with source code or documentation.

Comments

@hanqiang2
Copy link

Description

PowerShell Module project target to netstandard doesnot work.
Image

Image

Image

It report the error below

Could not load file or assembly 'System.Threading.AccessControl, Version=9.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified

Environment

PS C:\Users\USER> dotnet --info
.NET SDK:
 Version:           8.0.404
 Commit:            7b190310f2
 Workload version:  8.0.400-manifests.c6df56b6
 MSBuild version:   17.11.9+a69bbaaf5

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.20348
 OS Platform: Windows
 RID:         win-x64
 Base Path:   C:\Program Files\dotnet\sdk\8.0.404\

.NET workloads installed:
Configured to use loose manifests when installing new manifests.
There are no installed workloads to display.

Host:
  Version:      8.0.11
  Architecture: x64
  Commit:       9cb3b725e3

.NET SDKs installed:
  6.0.418 [C:\Program Files\dotnet\sdk]
  7.0.203 [C:\Program Files\dotnet\sdk]
  8.0.404 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 6.0.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.26 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 7.0.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 8.0.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 6.0.15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.26 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 7.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 6.0.26 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 7.0.5 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 8.0.11 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
  x86   [C:\Program Files (x86)\dotnet]
    registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables:
  Not set

global.json file:
  Not found

Learn more:
  https://aka.ms/dotnet/info

Download .NET:
  https://aka.ms/dotnet/download

Reproduction Steps

  1. Create a empty PowerShell Standard Module project.
  2. Then I add a reference of System.Threading.AccessControl 9.0.2, and call its functions.
  3. Build and import the module, and run it.

Besides, if a PowerShell Module Project target to net462 refer to a netstandard with the dependency System.Threading.AccessControl, it report the same error.

Expected behavior

It reports error

Test-SampleCmdlet : Could not load file or assembly 'System.Threading.AccessControl, Version=9.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
At line:1 char:103
+ ... 1\Project1\bin\Debug\netstandard2.0\Project1.dll;Test-SampleCmdlet 1;
+                                                      ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Test-SampleCmdlet], FileNotFoundException
    + FullyQualifiedErrorId : System.IO.FileNotFoundException,Project1.TestSampleCmdletCommand
  • The reference in the project is version 9.0.2, but it was trying to load version 9.0.0.0.
  • There is not dependencies output in the bin folder.
  • If I put the dll version 9.0.2 into bin folder, it report the same error. If I put 9.0.0, it works.

Actual behavior

It should work in dotnet installed environment. And it should look for 9.0.2, not 9.0.0.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Feb 28, 2025
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Feb 28, 2025
@huoyaoyuan
Copy link
Member

The tutorial at PowerShell is incomplete. Dependencies aren't copied to output folder on .NET Standard or .NET Core. To get dependencies copied to output folder, you need to use dotnet publish command and load from the publish folder.

Note that this won't work for System.Threading.AccessControl. There's no single file for System.Threading.AccessControl that works on both .NET Framework and .NET Core. You must multi-target your project with net462;net8.0-windows. The -windows suffix is mandatory to pick the working file.

@hanqiang2
Copy link
Author

Hi @huoyaoyuan . Thanks a lot. Nice information! Make sense to me.
Then I update the project like below which is the really structure in our product. And the DLL was output to the bin folder.

Image

Image
Also, I tried publish here.
Image

It worked as a net462 before. This year we migrate part of the project to net8 and some of the parts remain net462. So we made one of the common lib to netstandard to compile this common lib only once.
For your comments, I have tried that it works when I compile the common lib to both netstandard and net462. But I have no idea if I must compile the common lib to multiple framework.

@hanqiang2
Copy link
Author

I checked System.Threading.AccessControl again. It seems has a NETStandard implement.
Image

@huoyaoyuan
Copy link
Member

It seems has a NETStandard implement.

No. It only has reference support for .NET Standard.

You can inspect the content of the NuGet package. lib\net462 has support assembly for .NET Framework. runtimes\win\lib has support assembly for .NET 8/9/10 on Windows. All other assembly files are throwing PlatformNotSupportException.

The support assembly for .NET Framework and .NET 8~10 are not interchangeable.

@jeffschwMSFT jeffschwMSFT added question Answer questions and provide assistance, not an issue with source code or documentation. area-Meta labels Mar 3, 2025
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-meta
See info in area-owners.md if you want to be subscribed.

@hanqiang2
Copy link
Author

As I thought twice, I finally realized that the issue here is that I should not import such a DLL into a netstandard project which DLL has no netstandard implementation.
Do not know if my understanding is right.

@huoyaoyuan
Copy link
Member

I'm not sure what happens exactly with transitive dependencies, but you end goal is to get the correct binary loaded into correct environment. You can check the identity of files get copied to better achieve this.

@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Mar 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-Meta needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners question Answer questions and provide assistance, not an issue with source code or documentation.
Projects
None yet
Development

No branches or pull requests

3 participants