dotnet / dotnet/msbuild

Microsoft.Build API exception: Could not load file or assembly Microsoft.Build.Framework, Version=15.1.0.0

Open
#8,184 5 comments 0 reactions 0 assignees View on GitHub
Area: API Area: MSBuildLocator backlog Priority:2 triaged
Dominant language
C#
Stars
5.5k
Forks
1.5k
Avg merge
1d 8h
Merged PRs (30d)
141

Description

I am attempting to use the Microsoft.Build API to build projects and solution for use in an application within my company that is converting thousands of csproj files to the SDK format and verifying the build before and after. I am running into an exception with WPF projects that are in the Framework format. If I convert the project to SDK, the build succeeds. I do not have this issue building within Visual Studio 2022 or using MSBuild in 2022 Developer Command prompt. When I dig into the the exception I find that the root cause is an FileNotFoundException loading 'Microsoft.Build.Framework, Version=15.1.0.0,...'

I am not sure if there is a problem with my installation or something with my code/setup. I have attached the results of [EnumerateMSBuild.ps1](https://github.com/microsoft/msbuild/blob/2d82e1a861d890fce68c8e2d42b569e5bbaf5687/scripts/EnumerateMSBuild.ps1) and included as much detail as I can think of below. Please let me know if you require any more information and thank you in advance for any guidance.

[msbuild_versions.txt](https://github.com/dotnet/msbuild/files/10056181/msbuild_versions.txt)

The Error I get from the build is:
```
C:\windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.WinFx.targets(268,9): error MC1000: Unknown build error, 'Object reference not set to an instance of an object.'
0 Warning(s)
1 Error(s)
```

The diagnostic level log (abridged) has this information around the error:
```
Using "MarkupCompilePass1" task from assembly "PresentationBuildTasks, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35".
Task "MarkupCompilePass1"
...
Input: Assembly Reference file: 'C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.8\Facades\System.Xml.XPath.XDocument.dll'.
C:\windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.WinFx.targets(268,9): error MC1000: Unknown build error, 'Object reference not set to an instance of an object.'
Done executing task "MarkupCompilePass1" -- FAILED.
Done building target "MarkupCompilePass1" in project "G1.UI.WPF.csproj" -- FAILED.
```

When I attach a debugger and decompile symbols, I find the null ref is in the error handler on the "linePos" line, but the actual exception "e" is a file not found.
``` C#
\\MS.Internal.MarkupCompiler
internal void OnError(Exception e)
{
if (this.Error == null)
return;
int lineNum = e is XamlParseException xamlParseException ? xamlParseException.LineNumber : 0;
int linePos = xamlParseException != null ? xamlParseException.LinePosition : 0;
string originalFilePath = this.SourceFileInfo.OriginalFilePath;
this.Error((object) this, new MarkupErrorEventArgs(e, lineNum, linePos, originalFilePath));
}
```

Actual exception:
```
System.IO.FileNotFoundException
HResult=0x80070002
Message=Could not load file or assembly 'Microsoft.Build.Framework, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
Source=PresentationBuildTasks
StackTrace:
at MS.Internal.CompilerWrapper.OnSourceFileResolve(Object sender, SourceFileResolveEventArgs e)
at MS.Internal.MarkupCompiler.OnSourceFileResolve(FileUnit file)

Inner Exception 1:
FileNotFoundException: Could not load file or assembly 'Microsoft.Build.Framework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

Call Stack:
PresentationBuildTasks.dll!MS.Internal.MarkupCompiler.OnSourceFileResolve(MS.Internal.FileUnit file)
PresentationBuildTasks.dll!MS.Internal.MarkupCompiler.Initialize(MS.Internal.FileUnit sourceFile)
PresentationBuildTasks.dll!MS.Internal.MarkupCompiler.Compile(MS.Internal.CompilationUnit cu)
PresentationBuildTasks.dll!MS.Internal.CompilerWrapper.DoCompilation(string assemblyName, string language, string rootNamespace, MS.Internal.FileUnit[] fileList, bool isSecondPass)
[AppDomain (SlnToSdk.exe, #1) -> AppDomain (markupCompilationAppDomain, #2)]
```

Modules (abridged):
| Name | Version | AppDomain | Path |
|---|---|---|---|
|PresentationBuildTasks.dll | PresentationBuildTasks.dll 4.8.4084.0 built by: NET48REL1 | [2] markupCompilationAppDomain | C:\windows\Microsoft.Net\assembly\GAC_MSIL\PresentationBuildTasks\v4.0_4.0.0.0__31bf3856ad364e35\PresentationBuildTasks.dll |
| Microsoft.Build.Utilities.v4.0.dll | Microsoft.Build.Utilities.v4.0.dll 4.8.4084.0 built by: NET48REL1 | [2] markupCompilationAppDomain |C:\windows\Microsoft.Net\assembly\GAC_MSIL\Microsoft.Build.Utilities.v4.0\v4.0_4.0.0.0__b03f5f7f11d50a3a\Microsoft.Build.Utilities.v4.0.dll |
|Microsoft.Build.Locator.dll | Microsoft.Build.Locator.dll 1.05.5.21636 | [1] SlnToSdk.exe | C:\src\etdev\NET6Migration\SlnToSdk\bin\Debug\net48\Microsoft.Build.Locator.dll |
| Microsoft.Build.dll | Microsoft.Build.dll 17.02.1.25201 | [1] SlnToSdk.exe | C:\Program Files (x86)\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\Microsoft.Build.dll |
| Microsoft.Build.Framework.dll | Microsoft.Build.Framework.dll 17.02.1.25201 | [1] SlnToSdk.exe | C:\Program Files (x86)\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\Microsoft.Build.Framework.dll |
| SlnToSdk.exe | SlnToSdk.exe 1.00.0.0 | [1] SlnToSdk.exe | C:\src\etdev\NET6Migration\SlnToSdk\bin\Debug\net48\SlnToSdk.exe |

My code:
``` Xml


true








Exe
net48
enable
latest




```
``` C#
//Program
var latestInstance = MSBuildLocator.QueryVisualStudioInstances().MaxBy(x => x.Version);
MSBuildLocator.RegisterInstance(latestInstance);
Build(args[0]);

BuildResult Build(string solutionOrProjectFile)
{
var pc = new ProjectCollection();
var parameters = new BuildParameters(pc)
{
Loggers = new[]
{
new Microsoft.Build.Logging.ConsoleLogger(),
new Microsoft.Build.Logging.FileLogger()
{
Verbosity = LoggerVerbosity.Diagnostic
}
}
};

var request = new BuildRequestData(
projectFullPath: solutionOrProjectFile,
globalProperties: new Dictionary
{
{"Configuration", "Release"},
},
toolsVersion: null,
targetsToBuild: new string[]
{
"NukeAll",
"Restore",
"Build"
},
hostServices: null,
flags: BuildRequestDataFlags.ProvideProjectStateAfterBuild);

var buildResult = BuildManager.DefaultBuildManager.Build(parameters, request);
return buildResult;
}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.