Open sandboxFocusImprove this doc

LA0067: Type-Level Dependency on an Advice

Severity Error
Reported by PostSharp
Message Cannot define <Level>-level dependencies on the advice "<Advice>": a dependency custom attribute applied to an advice cannot set its Target property to AspectDependencyTarget.Type. Remove the Target property, or apply the dependency custom attribute to the aspect class.

Cause

The Target property of a dependency custom attribute chooses what the dependency orders. With Default, the dependency applies to the target of the aspect. With Type, it applies to the effects of a member-level aspect on the declaring type of its target, such as introduced members.

A dependency applied to an aspect class can use both targets. A dependency applied to an advice method orders this advice only, so it can only use Default. When it sets Type, PostSharp reports LA0067 on the advice. <Level> is always type.

In the following example, the entry advice of the [Audit] aspect has a dependency on the [Log] aspect with a type-level target.

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

namespace Errors.LA0067_Error;

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

[PSerializable]
public class AuditAttribute : MethodLevelAspect
{
    // A dependency on an advice cannot target the declaring type.
    [OnMethodEntryAdvice]
    [AspectTypeDependency( AspectDependencyAction.Order, AspectDependencyPosition.After, typeof(LogAttribute), Target = AspectDependencyTarget.Type )]
    public void OnEntry( MethodExecutionArgs args )
    {
        Console.WriteLine( $"Audit: entering {args.Method.Name}" );
    }
}

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

public static class Program
{
    public static void Main()
    {
        new OrderService().PlaceOrder( "Coffee" );
    }
}
error LA0067: LA0067_Error.cs(24,17): Cannot define type-level dependencies on the advice "Errors.LA0067_Error.AuditAttribute.OnEntry(PostSharp.Aspects.MethodExecutionArgs)": a dependency custom attribute applied to an advice cannot set its Target property to AspectDependencyTarget.Type. Remove the Target property, or apply the dependency custom attribute to the aspect class.

Resolution

  • Remove the Target property, so that the dependency orders the advice on its target.
  • Or, when the dependency must order the effects of the aspect on the declaring type, apply the dependency custom attribute to the aspect class instead of the advice method.

In the following example, the dependency of the entry advice has the default target, and the advice of [Audit] runs after [Log].

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

namespace Errors.LA0067_Fixed;

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

[PSerializable]
public class AuditAttribute : MethodLevelAspect
{
    // The dependency applies to the target of the advice.
    [OnMethodEntryAdvice]
    [AspectTypeDependency( AspectDependencyAction.Order, AspectDependencyPosition.After, typeof(LogAttribute) )]
    public void OnEntry( MethodExecutionArgs args )
    {
        Console.WriteLine( $"Audit: entering {args.Method.Name}" );
    }
}

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

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

See Also

Reference

Target
AspectDependencyTarget

Other Resources

Ordering Advices
Coping with Several Aspects on the Same Target