You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/msbuild/msbuild-inline-tasks.md
+70-30Lines changed: 70 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,8 @@
1
1
---
2
2
title: Create MSBuild inline tasks
3
3
description: Create MSBuild inline tasks by compiling a class that implements the Microsoft.Build.Framework.ITask interface in Visual Studio.
4
-
ms.date: 10/31/2023
5
-
ms.topic: how-to
4
+
ms.date: 7/14/2025
5
+
ms.topic: concept-article
6
6
helpviewer_keywords:
7
7
- MSBuild, tasks
8
8
author: ghogen
@@ -14,20 +14,21 @@ ms.subservice: msbuild
14
14
15
15
MSBuild tasks are typically created by compiling a class that implements the <xref:Microsoft.Build.Framework.ITask> interface. For more information, see [Tasks](../msbuild/msbuild-tasks.md).
16
16
17
-
Starting in .NET Framework version 4, you can create tasks inline in the project file. You do not have to create a separate assembly to host the task. This makes it easier to keep track of source code and easier to deploy the task. The source code is integrated into the script.
17
+
When you want to avoid the overhead of creating a compiled task, you can create a task inline in the project file or in an imported file. You don't have to create a separate assembly to host the task. Using an inline task makes it easier to keep track of source code and easier to deploy the task. The source code is integrated into the MSBuild project file or imported file, typically a `.targets` file.
18
+
19
+
You create an inline task by using a *code task factory*. For current development, be sure to use [RoslynCodeTaskFactory](../msbuild/msbuild-roslyncodetaskfactory.md), not `CodeTaskFactory`. `CodeTaskFactory` only supports C# versions up to 4.0.
20
+
21
+
Inline tasks are intended as a convenience for small tasks that don't require complicated dependencies. Debugging support for inline tasks is limited. It's recommended to create a compiled task instead of inline task when you want to write more complex code, reference a NuGet package, run external tools, or perform operations that could produce error conditions. Also, inline tasks are compiled every time you build, so there can be a noticeable impact on build performance.
18
22
19
-
In MSBuild 15.8, the [RoslynCodeTaskFactory](../msbuild/msbuild-roslyncodetaskfactory.md) was added. For current development, be sure to use the RoslynCodeTaskFactory, not CodeTaskFactory. CodeTaskFactory only supports C# versions up to 4.0.
20
-
21
23
## The structure of an inline task
22
24
23
-
An inline task is contained by a [UsingTask](../msbuild/usingtask-element-msbuild.md) element. The inline task and the `UsingTask` element that contains it are typically included in a *.targets* file and imported into other project files as required. Here is a basic inline task. Notice that it does nothing.
25
+
An inline task is contained by a [UsingTask](../msbuild/usingtask-element-msbuild.md) element. The inline task and the `UsingTask` element that contains it are typically included in a `.targets` file and imported into other project files as required. Here's a basic inline taskthat does nothing, but illustrates the syntax:
@@ -37,8 +38,7 @@ MSBuild tasks are typically created by compiling a class that implements the <xr
37
38
</Code>
38
39
</Task>
39
40
</UsingTask>
40
-
</Project>
41
-
```
41
+
```
42
42
43
43
The `UsingTask` element in the example has three attributes that describe the task and the inline task factory that compiles it.
44
44
@@ -48,17 +48,17 @@ MSBuild tasks are typically created by compiling a class that implements the <xr
48
48
49
49
- The `AssemblyFile` attribute gives the location of the inline task factory. Alternatively, you can use the `AssemblyName` attribute to specify the fully qualified name of the inline task factory class, which is typically located in `$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll`.
50
50
51
-
The remaining elements of the `DoNothing` task are empty and are provided to illustrate the order and structure of an inline task. A more robust example is presented later in this topic.
51
+
The remaining elements of the `DoNothing` task are empty and are provided to illustrate the order and structure of an inline task. A complete example is presented later in this article.
52
52
53
-
- The `ParameterGroup` element is optional. When specified, it declares the parameters for the task. For more information about input and output parameters, see [Input and output parameters](#input-and-output-parameters) later in this topic.
53
+
- The `ParameterGroup` element is optional. When specified, it declares the parameters for the task. For more information about input and output parameters, see [Input and output parameters](#input-and-output-parameters) later in this article.
54
54
55
55
- The `Task` element describes and contains the task source code.
56
56
57
-
- The `Reference` element specifies references to the .NET assemblies that you are using in your code. This is equivalent to adding a reference to a project in Visual Studio. The `Include` attribute specifies the path of the referenced assembly.
57
+
- The `Reference` element specifies references to the .NET assemblies that you are using in your code. Using this element is equivalent to adding a reference to a project in Visual Studio. The `Include` attribute specifies the path of the referenced assembly. Assemblies in mscorlib, .NET Standard, [Microsoft.Build.Framework](https://www.nuget.org/packages/Microsoft.Build.Framework/), and [Microsoft.Build.Utilities.Core](https://www.nuget.org/packages/Microsoft.Build.Utilities.Core), as well as some assemblies that are transitively referenced as dependencies, are available without a `Reference`.
58
58
59
-
- The `Using` element lists the namespaces that you want to access. This resembles the `Using` statement in Visual C#. The `Namespace` attribute specifies the namespace to include.
59
+
- The `Using` element lists the namespaces that you want to access. This element is equivalent to the `using` directive in C#. The `Namespace` attribute specifies the namespace to include. It doesn't work to put a `using` directive in the inline code, because that code is put into a method body, where `using` directives aren't allowed.
60
60
61
-
`Reference` and `Using` elements are language-agnostic. Inline tasks can be written in any one of the supported .NET CodeDom languages, for example, Visual Basic or Visual C#.
61
+
`Reference` and `Using` elements are language-agnostic. Inline tasks can be written in Visual Basic or C#.
62
62
63
63
> [!NOTE]
64
64
> Elements contained by the `Task` element are specific to the task factory, in this case, the code task factory.
@@ -77,27 +77,26 @@ The remaining elements of the `DoNothing` task are empty and are provided to ill
77
77
78
78
- If the value of `Type` is `Fragment`, then the code defines the contents of the `Execute` method, but not the signature or the `return` statement.
79
79
80
-
The code itself typically appears between a `<![CDATA[` marker and a `]]>` marker. Because the code is in a CDATA section, you do not have to worry about escaping reserved characters, for example, "\<" or ">".
80
+
The code itself typically appears between a `<![CDATA[` marker and a `]]>` marker. Because the code is in a CDATA section, you don't have to worry about escaping reserved characters, for example, "\<" or ">".
81
81
82
-
Alternatively, you can use the `Source` attribute of the `Code` element to specify the location of a file that contains the code for your task. The code in the source file must be of the type that is specified by the `Type` attribute. If the `Source` attribute is present, the default value of `Type` is `Class`. If `Source`is not present, the default value is `Fragment`.
82
+
Alternatively, you can use the `Source` attribute of the `Code` element to specify the location of a file that contains the code for your task. The code in the source file must be of the type that is specified by the `Type` attribute. If the `Source` attribute is present, the default value of `Type` is `Class`. If `Source`isn't present, the default value is `Fragment`.
83
83
84
84
> [!NOTE]
85
85
> When defining the task class in the source file, the class name must agree with the `TaskName` attribute of the corresponding [UsingTask](../msbuild/usingtask-element-msbuild.md) element.
86
86
87
87
## HelloWorld
88
88
89
-
Here is a more robust inline task. The HelloWorld task displays "Hello, world!" on the default error logging device, which is typically the system console or the Visual Studio **Output** window. The `Reference` element in the example is included just for illustration.
89
+
Here's an example of a simple inline task. The HelloWorld task displays "Hello, world!" on the default error logging device, which is typically the system console or the Visual Studio **Output** window.
Parameters may have one or more of these attributes:
136
135
137
136
-`Required` is an optional attribute that is `false` by default. If `true`, then the parameter is required and must be given a value before calling the task.
138
-
139
-
-`ParameterType` is an optional attribute that is `System.String` by default. It may be set to any fully qualified type that is either an item or a value that can be converted to and from a string by using System.Convert.ChangeType. (In other words, any type that can be passed to and from an external task.)
140
-
137
+
-`ParameterType` is an optional attribute that is `System.String` by default. It may be set to any fully qualified type that is either an item or a value that can be converted to and from a string by using <xref:System.Convert.ChangeType%2A>. (In other words, any type that can be passed to and from an external task.)
141
138
-`Output` is an optional attribute that is `false` by default. If `true`, then the parameter must be given a value before returning from the Execute method.
142
139
143
140
For example,
@@ -160,14 +157,18 @@ defines these three parameters:
160
157
161
158
If the `Code` element has the `Type` attribute of `Fragment` or `Method`, then properties are automatically created for every parameter. Otherwise, properties must be explicitly declared in the task source code, and must exactly match their parameter definitions.
162
159
163
-
## Example
160
+
## Debug an inline task
161
+
162
+
MSBuild generates a source file the inline task and writes the output to text file with a GUID filename in the temporary files folder, *AppData\Local\Temp\MSBuildTemp*. The output is normally deleted, but to preserve this output file, you can set the environment variable `MSBUILDLOGCODETASKFACTORYOUTPUT` to 1.
163
+
164
+
## Example 1
164
165
165
166
The following inline task replaces every occurrence of a token in the given file with the given value.
0 commit comments