Open sandboxFocusImprove this doc

PS0021: Unsupported Attribute on a Task Element

Severity Warning
Reported by PostSharp
Message The attribute "<Attribute>" is not supported on the task "<Task>".

Cause

PostSharp processes an assembly with a set of tasks. A postsharp.config file or a PostSharp project file (.psproj) can add a task or change its settings with an element under its Tasks element: the name of the element is the name of the task, <Task> in the message, and its attributes set the properties of the task.

PostSharp reports PS0021 for each attribute of a task element that is neither Enabled nor a property that the task can receive from the configuration. The names of these properties are not case-sensitive, but Enabled is. PostSharp ignores the attribute, and an attribute with an empty value is ignored without a warning. Every task accepts the Enabled and Disabled attributes, which disable the task when they are false and true respectively.

In the following example, the CountLinesOfCode task has a Disable attribute instead of Disabled, so the task is not disabled.

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.postsharp.org/1.0/configuration">
  <Tasks>
    <!-- The tasks have no Disable attribute, so the task still runs. -->
    <CountLinesOfCode Disable="true" />
  </Tasks>
</Project>
using System;
using PostSharp.Aspects;
using PostSharp.Serialization;

namespace Errors.PS0021_Error;

[PSerializable]
public class LogAttribute : OnMethodBoundaryAspect
{
    public override void OnEntry( MethodExecutionArgs args )
    {
        Console.WriteLine( $"Entering {args.Method.Name}." );
    }
}

public class OrderService
{
    [Log]
    public void PlaceOrder( string product )
    {
        Console.WriteLine( $"Order placed for {product}." );
    }
}

public static class Program
{
    public static void Main()
    {
        new OrderService().PlaceOrder( "Coffee" );
    }
}
warning PS0021: The attribute "Disable" is not supported on the task "CountLinesOfCode".

Resolution

  • When the attribute name is misspelled, correct it. The attributes of a task are the names of its configurable properties: see the documentation of the plug-in that defines the task.
  • When the attribute belongs to another task, or to a former version of the task, remove it.

In the following example, the Disabled attribute disables the task, and the project builds without a warning.

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.postsharp.org/1.0/configuration">
  <Tasks>
    <!-- The Disabled attribute, or Enabled="false", disables the task. -->
    <CountLinesOfCode Disabled="true" />
  </Tasks>
</Project>
using System;
using PostSharp.Aspects;
using PostSharp.Serialization;

namespace Errors.PS0021_Fixed;

[PSerializable]
public class LogAttribute : OnMethodBoundaryAspect
{
    public override void OnEntry( MethodExecutionArgs args )
    {
        Console.WriteLine( $"Entering {args.Method.Name}." );
    }
}

public class OrderService
{
    [Log]
    public void PlaceOrder( string product )
    {
        Console.WriteLine( $"Order placed for {product}." );
    }
}

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

See Also

Other Resources

Configuring Projects Using postsharp.config
Configuration File Schema Reference
Well-Known MSBuild Properties