Skip to content

Commit f5d1b6e

Browse files
Update docs/msbuild/updating-an-existing-application.md
Co-Authored-By: Rainer Sigwald <[email protected]>
1 parent ca9dd12 commit f5d1b6e

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

docs/msbuild/updating-an-existing-application.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,33 @@ Do not specify `ExcludeAssets=runtime` for the Microsoft.Build.Locator package.
7979

8080
### Register instance before calling MSBuild
8181

82-
Add a call to the Locator API before calling any method that uses MSBuild. This is important because the just-in-time (JIT) compiler needs to understand the types of the objects it compiles, and when trying to build a project using a specific version of MSBuild, it has to inspect that version before it compiles the code in case those types have changed. Since the JIT compiler works at a method-level granularity, if the call to the Locator API is in the same method as the first use of an MSBuild type (even if it precedes it, and the code involving the type has not yet been executed), the JIT compiler will not be able to JIT the whole method, causing an error. Some types that require the call to the MSBuildLocator API to have already been called include ProjectRootElement, ProjectInstance, and BuildManager.
82+
> [!IMPORTANT]
83+
> You cannot reference any MSBuild types (from the `Microsoft.Build` namespace) in the method that calls MSBuildLocator. For example, you cannot do this:
84+
>
85+
> ```csharp
86+
> void ThisWillFail()
87+
> {
88+
> MSBuildLocator.RegisterDefaults();
89+
> Project p = new Project(SomePath); // Could be any MSBuild type
90+
> // Code that uses the MSBuild type
91+
> }
92+
> ```
93+
>
94+
> Instead, you must do this:
95+
>
96+
> ```csharp
97+
> void MethodThatDoesNotDirectlyCallMSBuild()
98+
> {
99+
> MSBuildLocator.RegisterDefaults();
100+
> MethodThatCallsMSBuild();
101+
> }
102+
>
103+
> void MethodThatCallsMSBuild()
104+
> {
105+
> Project p = new Project(SomePath);
106+
> // Code that uses the MSBuild type
107+
> }
108+
> ```
83109
84110
The simplest way to add the call to the Locator API is to add a call to
85111

0 commit comments

Comments
 (0)