Skip to content

Explain need for Locator API call #4935

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

Merged
merged 2 commits into from
Apr 29, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion docs/msbuild/updating-an-existing-application.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,33 @@ Do not specify `ExcludeAssets=runtime` for the Microsoft.Build.Locator package.

### Register instance before calling MSBuild

Add a call to the Locator API before calling any method that uses MSBuild.
> [!IMPORTANT]
> You cannot reference any MSBuild types (from the `Microsoft.Build` namespace) in the method that calls MSBuildLocator. For example, you cannot do this:
>
> ```csharp
> void ThisWillFail()
> {
> MSBuildLocator.RegisterDefaults();
> Project p = new Project(SomePath); // Could be any MSBuild type
> // Code that uses the MSBuild type
> }
> ```
>
> Instead, you must do this:
>
> ```csharp
> void MethodThatDoesNotDirectlyCallMSBuild()
> {
> MSBuildLocator.RegisterDefaults();
> MethodThatCallsMSBuild();
> }
>
> void MethodThatCallsMSBuild()
> {
> Project p = new Project(SomePath);
> // Code that uses the MSBuild type
> }
> ```

The simplest way to add the call to the Locator API is to add a call to

Expand Down