### Example Runtime Errors for MSBL001 Source: https://github.com/microsoft/msbuildlocator/blob/main/docs/diagnostics/MSBL001.md These examples show common runtime errors like FileNotFoundException and InvalidOperationException that can occur due to incorrect MSBuild package references. ```text System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Build, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. ``` ```text System.InvalidOperationException: SDK 'Microsoft.NET.Sdk' could not be resolved. The SDK resolver "Microsoft.DotNet.MSBuildSdkResolver" returned null. ``` -------------------------------- ### Register Specific MSBuild Instance Source: https://github.com/microsoft/msbuildlocator/blob/main/src/MSBuildLocator/README.md Queries available Visual Studio instances and registers a specific one, typically the latest version, for use with MSBuild APIs. ```csharp using Microsoft.Build.Locator; // Query available MSBuild instances var instances = MSBuildLocator.QueryVisualStudioInstances().ToArray(); // Register a specific instance var instance = instances.OrderByDescending(i => i.Version).First(); MSBuildLocator.RegisterInstance(instance); ``` -------------------------------- ### Correct MSBuild PackageReference Configuration Source: https://github.com/microsoft/msbuildlocator/blob/main/docs/diagnostics/MSBL001.md This snippet shows the recommended way to configure MSBuild PackageReferences by adding ExcludeAssets="runtime" and PrivateAssets="all" to prevent runtime issues. ```xml ... ``` -------------------------------- ### Register Default MSBuild Assemblies Source: https://github.com/microsoft/msbuildlocator/blob/main/src/MSBuildLocator/README.md Registers the default MSBuild assemblies. This should be done before any MSBuild types are used in the application to ensure correct assembly loading. ```csharp using Microsoft.Build.Locator; using Microsoft.Build.Evaluation; // Register defaults before using any MSBuild types MSBuildLocator.RegisterDefaults(); // Now you can safely use MSBuild APIs. // NOTE: due the the way that the CLR loads assemblies, you MUST // register MSBuild through Locator before any types from // the MSBuild assemblies are used in your application. // The safest way to ensure this is to put any MSBuild API // access into a separate method. LoadProject(); void LoadProject() { var project = new Project("MyProject.csproj"); ... } ``` -------------------------------- ### Disable MSBL001 Check Source: https://github.com/microsoft/msbuildlocator/blob/main/docs/diagnostics/MSBL001.md This alternative configuration disables the MSBL001 check by setting the DisableMSBuildAssemblyCopyCheck property. This is not recommended as it requires distributing MSBuild assemblies manually. ```xml true ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.