Implement custom MSBuild tasks and distribute them via NuGet

January 31, 2017, (updated on May 15, 2018), 6 comments, Software Development

In this article describes how to implement MSBuild tasks with inline C# or in an external .NET assembly. After doing so, we will bundle these tasks in a NuGet package, so that they can easily be distributed and updated via NuGet and executed as part of the MSBuild compilation process. This is a powerfull technique to share and manage MSBuild tasks (e.g. build scripts, code generators, etc.) in bigger environments (enterprises, open-source libraries, common/shared tasks).

Write simple task with inline C

First we implement an MSBuild task with inline C# code. For the purpose of this article, a simple “Hello World” is sufficient:


<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="">
    <UsingTask TaskName="MyInlineTask" 
            <MyParameter ParameterType="System.String" Required="true" />
            <Using Namespace="System"/>
            <Using Namespace="System.Collections.Generic"/>
            <Using Namespace="System.IO"/>
            <Using Namespace="System.Linq"/>
            <Using Namespace="System.Text"/>
            <Using Namespace="System.Text.RegularExpressions"/>            
            <Code Type="Fragment" Language="cs">
                    var myParameter = this.MyParameter;             
                    Log.LogMessage(MessageImportance.High, "Hello from MyInlineTask: " + myParameter);

You can now run the implemented task in your .csproj file by importing the .props file and executing the MyInlineTask task:

<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="">
    <Import Project="MyTasks.props" />

    <Target Name="MyTasks" AfterTargets="AfterBuild">
        <MyInlineTask MyParameter="Foobar"></MyInlineTask>

Write task in external assembly

If you implement tasks with more source code, it is recommended to implement them in a separate .NET project. In a new project, reference the GAC assembly Microsoft.Build.Utilities.Core and create a new class which inherits from Microsoft.Build.Utilities.Task:

public class MyExternalTask : Microsoft.Build.Utilities.Task
    public string MyParameter { get; set; }

    public override bool Execute()
        Console.WriteLine("Hello from the MyAssembly: " + MyParameter);
        return true; 

The task can now be used by importing the assembly with its file path and defining the class with the full qualified name:


<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="">
    <UsingTask TaskName="MyExternalTask" TaskFactory="MyAssembly.MyExternalTask" AssemblyFile="MyAssembly.dll"/>

As before, the task can be called in your .csproj file by importing the .props file and executing the task:

<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="">
    <Import Project="MyTasks.props" />

    <Target Name="MyTasks" AfterTargets="AfterBuild">
        <MyExternalTask MyParameter="Foobar"></MyExternalTask>

Distribution via NuGet

Now that we have implemented the two tasks, we can distribute them with a NuGet package and automatically run both tasks after the AfterBuild target.

First, we create the MyPackage.props file (the file name must match the package ID, previously called MyProps.props) which contains the custom tasks and which will be added to the project where the NuGet package is installed:

<Project xmlns="">
    <UsingTask ... />
    <UsingTask ... />
    <Target Name="MyTasks" AfterTargets="AfterBuild">
        <MyInlineTask MyParameter="Foo" />
        <MyExternalTask MyParameter="Bar" />

Next, we create the MyPackage.nuspec NuSpec file which references the MyPackage.props file and the MyAssembly.dll assembly file with the external task:

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="">
        <copyright>Copyright © Company 2014</copyright>
        <dependencies />
        <references />
        <file src="MyPackage.props" target="build" />
        <file src="MyAssembly.dll" target="build" />

It is important to set developmentDependency to true so that the package is installed and marked as development dependency in the project’s packages.config. This way the package is only installed in the project but not marked as a runtime dependency of the project.

Now we can generate the NuGet package:

nuget pack MyPackage.nuspec

After installing the package in another project, the project’s .csproj file should import the .props file from the NuGet package:

<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.props" />
<Import Project="..\packages\MyPackage.\build\MyPackage.props" Condition="Exists('..\packages\MyPackage.\build\MyPackage.props')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">

Now the tasks should be executed when the MSBuild project is built. I used NuGet packaged MSBuild tasks in the NSwag project so that the code generators can be run as part of the MSBuild compilation.

Tags: , , ,

6 responses to “Implement custom MSBuild tasks and distribute them via NuGet”

  1. Peter Moore says:

    Trying the external version and in the main assembly I keep getting the error, “The task factory “MyAssembly.MyExternalTask” could not be found in the assembly …”

    • Rico Suter says:

      Strange, it seems that it does not expect a Task type… but maybe a type which inherits from ITaskFactory?

      • Jack Taylor says:

        It worked for me after omitting the TaskFactory attribute:

        <UsingTask TaskName="MyNamespace.MyExternalTask" AssemblyFile="MyAssembly.dll" />

        Replace “MyNamespace” with whatever you chose for your namespace, and “MyAssembly.dll” with whatever your assembly is called.

  2. Patrick D'Souza says:

    Great article! Had a question, In the line below

    “Next, we create the MyPackage.nuspec NuSpec file which references the MyPackage.props file (the file name must match the package ID) and the MyAssembly.dll assembly file with the”

    Should it be “MyTasks.props” instead of “MyPackage.props”.

  3. Rahul says:

    Thank you for sharing this valuable information! It helped me create a MSBuild Task nuget.

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    Markdown is turned off in code blocks:
     [This is not a link](

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see

This site uses Akismet to reduce spam. Learn how your comment data is processed.