Open sandboxFocusImprove this doc

LA0065: Unsupported Aspect Dependency Custom Attribute

Severity Error
Reported by PostSharp
Message Cannot find a parser for the aspect dependency custom attribute "<AttributeType>" used on "<Declaration>". Use a dependency custom attribute of PostSharp, such as [AspectTypeDependency] or [AspectRoleDependency], instead of a class derived from AspectDependencyAttribute.

Cause

Aspect dependencies order aspects and advices, and declare requirements and conflicts between them. You express them with custom attributes derived from AspectDependencyAttribute. Although this class is public and abstract, PostSharp processes only the derived classes that it knows: AspectTypeDependencyAttribute, AspectRoleDependencyAttribute, AspectEffectDependencyAttribute, AdviceDependencyAttribute and ProvideAspectRoleAttribute. When a dependency custom attribute of another class is applied to an aspect class, an advice method or the assembly, or is added to the configuration of an aspect instance, PostSharp cannot read it and reports LA0065.

<AttributeType> is the class of the custom attribute, and <Declaration> is the aspect class or the advice method that carries it.

In the following example, the [Log] aspect carries a [RunsAfterValidation] custom attribute, defined in the program by deriving from AspectDependencyAttribute.

using System;
using PostSharp.Aspects;
using PostSharp.Aspects.Dependencies;
using PostSharp.Serialization;

namespace Errors.LA0065_Error;

public sealed class RunsAfterValidationAttribute : AspectDependencyAttribute
{
    public RunsAfterValidationAttribute() : base( AspectDependencyAction.Order, AspectDependencyPosition.After )
    {
    }
}

// PostSharp cannot process a dependency custom attribute that it does not define.
[PSerializable, RunsAfterValidation]
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" );
    }
}
error LA0065: LA0065_Error.cs(17,14): Cannot find a parser for the aspect dependency custom attribute "Errors.LA0065_Error.RunsAfterValidationAttribute" used on "Errors.LA0065_Error.LogAttribute". Use a dependency custom attribute of PostSharp, such as [AspectTypeDependency] or [AspectRoleDependency], instead of a class derived from AspectDependencyAttribute.

Resolution

Express the dependency with a custom attribute of PostSharp:

These classes are sealed, so you cannot derive a custom attribute of your own from them.

In the following example, an AspectRoleDependencyAttribute places the [Log] aspect after the validation aspects.

using System;
using PostSharp.Aspects;
using PostSharp.Aspects.Dependencies;
using PostSharp.Serialization;

namespace Errors.LA0065_Fixed;

// A dependency custom attribute of PostSharp expresses the same dependency.
[PSerializable, AspectRoleDependency( AspectDependencyAction.Order, AspectDependencyPosition.After, StandardRoles.Validation )]
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

Reference

AspectDependencyAttribute
AspectRoleDependencyAttribute
AspectTypeDependencyAttribute

Other Resources

Coping with Several Aspects on the Same Target
Ordering Advices