| Severity | Error |
| Reported by | PostSharp |
| Message | Unexpected <Binding> parameter in the advice "<Advice>". The <Binding> parameter can be used only in these advices: <EligibleAdvices>. |
Cause
Each binding attribute of an advice parameter gives a value that exists only in some kinds of advices. For instance, the exception thrown by the target method, which ExceptionAttribute binds, exists only in an OnMethodExceptionAdvice, and the awaited task of AwaitedTaskAttribute only in an OnMethodYieldAdvice. When a parameter has a binding attribute that the kind of its advice does not accept, and PostSharp cannot emit the value there, it reports LA0151 on the parameter.
<Binding> is the binding attribute as you write it, such as [Exception], and <EligibleAdvices> lists the advice attributes that accept it. The table in Bindings by advice kind shows the binding attributes that each advice accepts. Some combinations report another message instead: LA0224 in an OnInstanceConstructedAdvice, and the warning LA0233 for [Arguments] and [FlowBehavior], which build but whose value is ignored.
In the following example, the exit advice of the [LogFailure] aspect has an [Exception] parameter.
using System;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Serialization;
namespace Errors.LA0151_Error;
[PSerializable]
public class LogFailureAttribute : MethodLevelAspect
{
[OnMethodExitAdvice]
// [Exception] is accepted only in an exception advice, not in an exit advice.
public void OnExit( [DeclarationName] string methodName, [Exception] Exception exception )
{
Console.WriteLine( $"{methodName} failed: {exception.Message}" );
}
}
public class OrderService
{
[LogFailure]
public void Ship( int orderId )
{
throw new InvalidOperationException( $"Order {orderId} has no address." );
}
}
public static class Program
{
public static void Main()
{
try
{
new OrderService().Ship( 42 );
}
catch ( InvalidOperationException )
{
}
}
}
error LA0151: LA0151_Error.cs(13,84): Unexpected [Exception] parameter in the advice "Errors.LA0151_Error.LogFailureAttribute.OnExit(System.String, System.Exception)". The [Exception] parameter can be used only in these advices: OnMethodExceptionAdvice.
Resolution
- Move the parameter to an advice of a kind listed in the message. For the exception, use an OnMethodExceptionAdvice, which runs only when the target method throws.
- Otherwise, remove the parameter.
In the following example, the [LogFailure] aspect receives the exception in an exception advice.
using System;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Serialization;
namespace Errors.LA0151_Fixed;
[PSerializable]
public class LogFailureAttribute : MethodLevelAspect
{
[OnMethodExceptionAdvice]
// An exception advice runs only when the method throws, and receives the exception.
public void OnException( [DeclarationName] string methodName, [Exception] Exception exception )
{
Console.WriteLine( $"{methodName} failed: {exception.Message}" );
}
}
public class OrderService
{
[LogFailure]
public void Ship( int orderId )
{
throw new InvalidOperationException( $"Order {orderId} has no address." );
}
}
public static class Program
{
public static void Main()
{
try
{
new OrderService().Ship( 42 );
}
catch ( InvalidOperationException )
{
}
}
}
Ship failed: Order 42 has no address.
See Also
Reference
AdviceParameterAttribute
ExceptionAttribute
OnMethodExceptionAdvice
Other Resources