Open sandboxFocusImprove this doc

LA0234: FlowBehavior Parameter Overrides MethodExecutionArgs.FlowBehavior

Severity Warning
Reported by PostSharp
Message The advice "<Advice>" has both a MethodExecutionArgs parameter and the [FlowBehavior] parameter "<Parameter>". The value of the [FlowBehavior] parameter is used, and MethodExecutionArgs.FlowBehavior is ignored.

Cause

An OnMethodEntryAdvice, an OnMethodExceptionAdvice or an OnMethodResumeAdvice can change the control flow of the target method in two ways: through the FlowBehavior property of a MethodExecutionArgs parameter, or through a parameter annotated with FlowBehaviorAttribute. When the advice has both, the woven code reads only the [FlowBehavior] parameter, and PostSharp reports LA0234 on this parameter, whatever values the advice sets.

In the following example, the [SkipWhenOffline] aspect sets args.FlowBehavior to FlowBehavior.Return, but leaves the [FlowBehavior] parameter at its default value, so Synchronize still runs.

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

namespace Errors.LA0234_Error;

public static class Network
{
    public static bool IsOnline => false;
}

[PSerializable]
public class SkipWhenOfflineAttribute : MethodLevelAspect
{
    [OnMethodEntryAdvice]
    public static void OnEntry( MethodExecutionArgs args,
                                // The [FlowBehavior] parameter takes precedence, so args.FlowBehavior is ignored.
                                [FlowBehavior] out FlowBehavior flowBehavior )
    {
        flowBehavior = FlowBehavior.Default;

        if ( !Network.IsOnline )
        {
            Console.WriteLine( $"Skipping {args.Method.Name} because the network is offline." );
            args.FlowBehavior = FlowBehavior.Return;
        }
    }
}

public class SyncService
{
    [SkipWhenOffline]
    public void Synchronize()
    {
        Console.WriteLine( "Synchronizing." );
    }
}

public static class Program
{
    public static void Main()
    {
        new SyncService().Synchronize();
    }
}
warning LA0234: LA0234_Error.cs(19,65): The advice "Errors.LA0234_Error.SkipWhenOfflineAttribute.OnEntry(PostSharp.Aspects.MethodExecutionArgs, PostSharp.Aspects.FlowBehavior@)" has both a MethodExecutionArgs parameter and the [FlowBehavior] parameter "flowBehavior". The value of the [FlowBehavior] parameter is used, and MethodExecutionArgs.FlowBehavior is ignored.
Skipping Synchronize because the network is offline.
Synchronizing.

Resolution

Set the flow behavior in one way only:

In the following example, the advice keeps only its MethodExecutionArgs parameter, and Synchronize is skipped.

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

namespace Errors.LA0234_Fixed;

public static class Network
{
    public static bool IsOnline => false;
}

[PSerializable]
public class SkipWhenOfflineAttribute : MethodLevelAspect
{
    // Without a [FlowBehavior] parameter, the advice sets the flow behavior through MethodExecutionArgs.
    [OnMethodEntryAdvice]
    public static void OnEntry( MethodExecutionArgs args )
    {
        if ( !Network.IsOnline )
        {
            Console.WriteLine( $"Skipping {args.Method.Name} because the network is offline." );
            args.FlowBehavior = FlowBehavior.Return;
        }
    }
}

public class SyncService
{
    [SkipWhenOffline]
    public void Synchronize()
    {
        Console.WriteLine( "Synchronizing." );
    }
}

public static class Program
{
    public static void Main()
    {
        new SyncService().Synchronize();
    }
}
Skipping Synchronize because the network is offline.

See Also

Reference

FlowBehaviorAttribute
FlowBehavior

Other Resources

Advice Parameters