Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies.

flagrant99 21 Reputation points
2023-06-09T13:52:16.9066667+00:00

Can someone tell me why the following powershell code no longer works with newer MSBuild 17 (It works fine with MSBuild 15)

$retVal = Add-Type -Path "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\Microsoft.Build.dll" #load DLL

$ProjectCollection = [Microsoft.Build.Evaluation.ProjectCollection]::GlobalProjectCollection;

$ProjectCollection.UnloadAllProjects();

I always get the following error

Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

Why does MSBuild 17 want to use such an OLD version 4.0.4.1? This version is not installed anywhere.

Why would it need to be for new MSBuild?

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,648 questions
Not Monitored
Not Monitored
Tag not monitored by Microsoft.
36,286 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,103 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Tianyu Sun-MSFT 27,546 Reputation points Microsoft Vendor
    2023-06-12T08:49:25.0166667+00:00

    Hello @flagrant99 ,

    Welcome to Microsoft Q&A forum.

    The error message does mention that "could not load file or assembly … The system cannot find the file specified". If you try to install the System.Runtime.CompilerServices.Unsafe.dll assembly(version 4.5.3 corresponding to assembly version 4.0.4.1, you can run ildasm in Developer Command Prompt for Visual Studio and use ildasm tool to check the assembly version of the dll file) and register it into GAC, the error should disappear.

    Please try to download the Nuget package from here: System.Runtime.CompilerServices.Unsafe. After downloading, you will get a .nupkg file, move it to a clean folder. Rename the file extension to .zip, open it, open the lib folder, open the net461 folder and you should see the dll file, copy the folder path, let’s say the path is C:\XXXX\XXXX.

    Run Developer Command Prompt for Visual Studio as administrator, and run following commands:

    cd C:\XXXX\XXXX

    gacutil /i System.Runtime.CompilerServices.Unsafe.dll

    User's image

    User's image

    User's image

    User's image

    Sincerely,

    Tianyu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    3 people found this answer helpful.

  2. Rich Matheisen 45,096 Reputation points
    2023-06-09T15:40:45.2733333+00:00

    That error (with varying version numbers) has been encountered several times. You might start here: https://github.com/dotnet/msbuild/issues/5192

    It would probably also be a good idea to add one of the "Visual Studio" tags to your question.

    I don't think the problem is with PowerShell, though.


  3. flagrant99 21 Reputation points
    2023-06-12T14:31:49.6+00:00

    It runs fine from Powershell 7.3.4.

    I guess Powershell 5.1 does not work with newer MSBuild.

    I think the problem is the GAC. The GAC breaks things. Powershell 7.x does not load from the GAC at all and it works fine.

    See here:

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