Open sandboxFocusImprove this doc

LA0259: Two Pointcuts in One Group

Severity Error
Reported by PostSharp
Message The advices <Advices> are in one group and each has a pointcut, but a group has one pointcut, which applies to all its advices. Keep the pointcut on one advice of the group, or give the advices different groups.

Cause

A pointcut, such as MulticastPointcut or MethodPointcut, selects the declarations to which a group of advices applies. The pointcut is a property of the whole group, so a group has one pointcut, and you can put it on any advice of the group.

An advice with a pointcut starts a group. Two advices with a pointcut are therefore in two groups, unless they select one group with the Group property. PostSharp reports LA0259 on each advice of the group when two advices of one group have a pointcut. <Advices> lists these advices.

In the following example, the entry advice and the exit advice are in the Trace group, and each has a pointcut.

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

namespace Errors.LA0259_Error;

[PSerializable]
public class TraceAttribute : TypeLevelAspect
{
    // Both advices are in the Trace group, and each has a pointcut.
    [OnMethodEntryAdvice( Group = "Trace" )]
    [MulticastPointcut( Targets = MulticastTargets.Method, Attributes = MulticastAttributes.Public )]
    public void OnEntry( MethodExecutionArgs args ) => Console.WriteLine( $"Entering {args.Method.Name}" );

    [OnMethodExitAdvice( Group = "Trace" )]
    [MulticastPointcut( Targets = MulticastTargets.Method, Attributes = MulticastAttributes.Public )]
    public void OnExit( MethodExecutionArgs args ) => Console.WriteLine( $"Leaving {args.Method.Name}" );
}

[Trace]
public class OrderService
{
    public void PlaceOrder() => Console.WriteLine( "Placing the order" );
}

public static class Program
{
    public static void Main()
    {
        new OrderService().PlaceOrder();
    }
}
error LA0259: LA0259_Error.cs(15,17): The advices "Errors.LA0259_Error.TraceAttribute.OnEntry(PostSharp.Aspects.MethodExecutionArgs)", "Errors.LA0259_Error.TraceAttribute.OnExit(PostSharp.Aspects.MethodExecutionArgs)" are in one group and each has a pointcut, but a group has one pointcut, which applies to all its advices. Keep the pointcut on one advice of the group, or give the advices different groups.
error LA0259: LA0259_Error.cs(19,17): The advices "Errors.LA0259_Error.TraceAttribute.OnEntry(PostSharp.Aspects.MethodExecutionArgs)", "Errors.LA0259_Error.TraceAttribute.OnExit(PostSharp.Aspects.MethodExecutionArgs)" are in one group and each has a pointcut, but a group has one pointcut, which applies to all its advices. Keep the pointcut on one advice of the group, or give the advices different groups.

Resolution

Keep the pointcut on one advice of the group. It applies to all the advices of the group. When the advices must apply to different declarations, remove the Group property, or give the advices different group names: each pointcut then starts a group of its own. When the class has only one group, the Group property is not needed: PostSharp puts each advice without pointcut in the group that has no advice of its kind.

In the following example, only the entry advice has a pointcut. The exit advice joins its group, and applies to the same methods.

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

namespace Errors.LA0259_Fixed;

[PSerializable]
public class TraceAttribute : TypeLevelAspect
{
    // The pointcut is on one advice, and applies to the whole group.
    [OnMethodEntryAdvice]
    [MulticastPointcut( Targets = MulticastTargets.Method, Attributes = MulticastAttributes.Public )]
    public void OnEntry( MethodExecutionArgs args ) => Console.WriteLine( $"Entering {args.Method.Name}" );

    // The advice joins the only group of the class that has no exit advice.
    [OnMethodExitAdvice]
    public void OnExit( MethodExecutionArgs args ) => Console.WriteLine( $"Leaving {args.Method.Name}" );
}

[Trace]
public class OrderService
{
    public void PlaceOrder() => Console.WriteLine( "Placing the order" );
}

public static class Program
{
    public static void Main()
    {
        new OrderService().PlaceOrder();
    }
}
Entering PlaceOrder
Placing the order
Leaving PlaceOrder

See Also

Reference

Group
MulticastPointcut
MethodPointcut

Other Resources

LA0231
Selecting Members with Pointcuts
Advice Methods