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

Error if msbuild already loaded #14

Merged

Conversation

rainersigwald
Copy link
Member

The .NET assembly loader ensures that any assembly referenced by a
method is loaded before the method is called. This requires that
MSBuildLocator be called before any method that references MSBuild.

That's a confusing requirement, and easy to forget. But it's easy to
check for, and since we expect the locator to be called roughly once per
process, not too expensive to check for at RegisterInstance time.

Produces an error like

Unhandled Exception: System.InvalidOperationException: Microsoft.Build.Locator.MSBuildLocator.RegisterInstance was called, but MSBuild assemblies were already loaded.
Ensure that RegisterInstance is called before any method that directly references types in the Microsoft.Build namespace has been called.
Loaded MSBuild assemblies: Microsoft.Build, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
   at Microsoft.Build.Locator.MSBuildLocator.RegisterInstance(VisualStudioInstance instance)
   at Microsoft.Build.Locator.MSBuildLocator.RegisterDefaults()
   at MultiProcBuilderApplication.Program.Main(String[] args) in s:\work\MultiProcBuilderApplication\MultiProcBuilderApplication\Program.cs:line 13

The .NET assembly loader ensures that any assembly referenced by a
method is loaded before the method is called. This requires that
MSBuildLocator be called **before any method that references MSBuild**.

That's a confusing requirement, and easy to forget. But it's easy to
check for, and since we expect the locator to be called roughly once per
process, not too expensive to check for at `RegisterInstance` time.

Produces an error like

```
Unhandled Exception: System.InvalidOperationException: Microsoft.Build.Locator.MSBuildLocator.RegisterInstance was called, but MSBuild assemblies were already loaded.
Ensure that RegisterInstance is called before any method that directly references types in the Microsoft.Build namespace has been called.
Loaded MSBuild assemblies: Microsoft.Build, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
   at Microsoft.Build.Locator.MSBuildLocator.RegisterInstance(VisualStudioInstance instance)
   at Microsoft.Build.Locator.MSBuildLocator.RegisterDefaults()
   at MultiProcBuilderApplication.Program.Main(String[] args) in s:\work\MultiProcBuilderApplication\MultiProcBuilderApplication\Program.cs:line 13
```
@rainersigwald rainersigwald merged commit e60d679 into microsoft:master Feb 20, 2018
@rainersigwald rainersigwald deleted the error-if-msbuild-already-loaded branch February 20, 2018 23:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants