Open sandboxFocusImprove this doc

LA0233: Binding Ignored in This Kind of Advice

Severity Warning
Reported by PostSharp
Message The parameter "<Parameter>" of the advice "<Advice>" is bound by <Binding>, which is not supported in this advice: the value of the parameter is ignored or not defined. <Binding> is supported only in these advices: <SupportedAdvices>.

Cause

Each binding attribute of an advice parameter is accepted only in some kinds of advice. Most bindings cause the error LA0151 in an advice that does not accept them. Two bindings cause this warning instead, because the woven code still builds:

<Binding> is the binding attribute, such as [FlowBehavior], and <SupportedAdvices> lists the advice attributes that accept it.

In the following example, the [IgnoreExceptions] aspect sets the flow behavior to FlowBehavior.Return in an exit advice, so the exception of Save is not swallowed.

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

namespace Errors.LA0233_Error;

[PSerializable]
public class IgnoreExceptionsAttribute : MethodLevelAspect
{
    [OnMethodExitAdvice]
    public static void OnExit( [DeclarationName] string methodName,
                               // An exit advice does not read the flow behavior, so the exception is not swallowed.
                               [FlowBehavior] out FlowBehavior flowBehavior )
    {
        Console.WriteLine( $"Leaving {methodName}." );
        flowBehavior = FlowBehavior.Return;
    }
}

public class SettingsService
{
    [IgnoreExceptions]
    public void Save()
    {
        throw new IOException( "The disk is full." );
    }
}

public static class Program
{
    public static void Main()
    {
        try
        {
            new SettingsService().Save();
            Console.WriteLine( "The exception was ignored." );
        }
        catch ( IOException e )
        {
            Console.WriteLine( $"The exception was not ignored: {e.Message}" );
        }
    }
}
warning LA0233: LA0233_Error.cs(15,64): The parameter "flowBehavior" of the advice "Errors.LA0233_Error.IgnoreExceptionsAttribute.OnExit(System.String, PostSharp.Aspects.FlowBehavior@)" is bound by [FlowBehavior], which is not supported in this advice: the value of the parameter is ignored or not defined. [FlowBehavior] is supported only in these advices: OnMethodEntryAdvice, OnMethodExceptionAdvice, OnMethodResumeAdvice.
Leaving Save.
The exception was not ignored: The disk is full.

Resolution

  • Move the parameter to an advice that accepts it, as <SupportedAdvices> lists. To swallow an exception, set the flow behavior in an OnMethodExceptionAdvice.
  • When the advice does not need the value, remove the parameter.

In the following example, the aspect sets the flow behavior in an exception advice, which swallows the exception.

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

namespace Errors.LA0233_Fixed;

[PSerializable]
public class IgnoreExceptionsAttribute : MethodLevelAspect
{
    // An exception advice reads the flow behavior, so FlowBehavior.Return swallows the exception.
    [OnMethodExceptionAdvice]
    public static void OnException( [DeclarationName] string methodName,
                                    [FlowBehavior] out FlowBehavior flowBehavior )
    {
        Console.WriteLine( $"Leaving {methodName}." );
        flowBehavior = FlowBehavior.Return;
    }
}

public class SettingsService
{
    [IgnoreExceptions]
    public void Save()
    {
        throw new IOException( "The disk is full." );
    }
}

public static class Program
{
    public static void Main()
    {
        try
        {
            new SettingsService().Save();
            Console.WriteLine( "The exception was ignored." );
        }
        catch ( IOException e )
        {
            Console.WriteLine( $"The exception was not ignored: {e.Message}" );
        }
    }
}
Leaving Save.
The exception was ignored.

See Also

Reference

FlowBehaviorAttribute
ArgumentsAttribute
OnMethodExceptionAdvice

Other Resources

Advice Parameters