Open sandboxFocusImprove this doc

PS0048: Task Required by a Library Not Found

Severity Error
Reported by PostSharp
Message Cannot find the task "<TaskName>".

Cause

PostSharp builds a project by running tasks. PostSharp and its plug-ins define these tasks with TaskType elements in their configuration files. A library can require a task in the projects that use it with RequirePostSharpAttribute: when a project uses an attribute of the library that carries a RequirePostSharpAttribute, PostSharp loads the plug-in that PlugIn names, if any, and adds the task that Task names.

PostSharp reports PS0048, and stops building the project, when no task of the name <TaskName> is defined, neither by PostSharp nor by the plug-ins that it has loaded. The usual causes are:

  • The name of the task is misspelled in the RequirePostSharpAttribute of the library.
  • The task belongs to a version of the plug-in other than the one that the project uses, which does not define it, or defines it under another name.
  • The RequirePostSharpAttribute names the task, but not the plug-in that defines it.

The message does not name the declaration that requires the task. When you do not know it, search the referenced libraries for RequirePostSharpAttribute.

In the following example, the [Audited] attribute of a library requires the ValidateAnnotation task, but the task of PostSharp is named ValidateAnnotations.

// @DisabledMessages(PS0131)

using System;
using PostSharp.Extensibility;

namespace Errors.PS0048_Error.Auditing;

// The name of the task is misspelled: the task of PostSharp is ValidateAnnotations.
[RequirePostSharp( null, "ValidateAnnotation" )]
[AttributeUsage( AttributeTargets.Method )]
public sealed class AuditedAttribute : Attribute
{
}
using System;
using Errors.PS0048_Error.Auditing;

namespace Errors.PS0048_Error;

public class OrderService
{
    // The Audited attribute of the library requires a task that does not exist.
    [Audited]
    public void PlaceOrder( string product )
    {
        Console.WriteLine( $"Order placed for {product}." );
    }
}

public static class Program
{
    public static void Main()
    {
        new OrderService().PlaceOrder( "Coffee" );
    }
}
error PS0048: Cannot find the task "ValidateAnnotation".

Resolution

  • When you develop the library, give the exact name of the task, as the Name attribute of its TaskType element defines it, and the name of the plug-in that defines it when the task is not part of PostSharp.
  • When the library comes from a NuGet package, check that the packages of the library and of its plug-ins have the same version, and update them together.

In the following example, the library names the ValidateAnnotations task, and the project builds.

// @DisabledMessages(PS0131)

using System;
using PostSharp.Extensibility;

namespace Errors.PS0048_Fixed.Auditing;

// The library names a task that PostSharp defines.
[RequirePostSharp( null, "ValidateAnnotations" )]
[AttributeUsage( AttributeTargets.Method )]
public sealed class AuditedAttribute : Attribute
{
}
using System;
using Errors.PS0048_Fixed.Auditing;

namespace Errors.PS0048_Fixed;

public class OrderService
{
    // The Audited attribute of the library now requires an existing task.
    [Audited]
    public void PlaceOrder( string product )
    {
        Console.WriteLine( $"Order placed for {product}." );
    }
}

public static class Program
{
    public static void Main()
    {
        new OrderService().PlaceOrder( "Coffee" );
    }
}
Order placed for Coffee.

See Also

Reference

RequirePostSharpAttribute

Other Resources

Configuration File Schema Reference