Open sandboxFocusImprove this doc

LA0210: Custom Binding Attribute Not Supported

Severity Error
Reported by PostSharp
Message The parameter "<Parameter>" of the advice "<Advice>" has the advice parameter attribute "<Attribute>", which is not supported. Only the advice parameter attributes defined by PostSharp can be used.

Cause

The binding attributes of advice parameters derive from AdviceParameterAttribute. PostSharp generates the code that computes the value of each binding attribute that it defines, such as ArgumentAttribute or DeclarationNameAttribute. It cannot compute a value for an attribute that your application derives from AdviceParameterAttribute, so it reports LA0210 on the parameter that has such an attribute. <Attribute> is the type of this attribute.

In the following example, the [Audit] aspect tries to receive the current user through a [CurrentUser] attribute of the application.

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

namespace Errors.LA0210_Error;

public static class UserContext
{
    public static string CurrentUser { get; set; } = "alice";
}

// A binding attribute of the application, which PostSharp cannot bind.
public sealed class CurrentUserAttribute : AdviceParameterAttribute
{
}

[PSerializable]
public class AuditAttribute : MethodLevelAspect
{
    [OnMethodEntryAdvice]
    // Only the binding attributes of PostSharp can select the value of a parameter.
    public void OnEntry( [DeclarationName] string methodName, [CurrentUser] string user )
    {
        Console.WriteLine( $"{user} calls {methodName}." );
    }
}

public class OrderService
{
    [Audit]
    public void Cancel( int orderId )
    {
        Console.WriteLine( $"Order {orderId} is canceled." );
    }
}

public static class Program
{
    public static void Main()
    {
        new OrderService().Cancel( 42 );
    }
}
error LA0210: LA0210_Error.cs(23,84): The parameter "user" of the advice "Errors.LA0210_Error.AuditAttribute.OnEntry(System.String, System.String)" has the advice parameter attribute "Errors.LA0210_Error.CurrentUserAttribute", which is not supported. Only the advice parameter attributes defined by PostSharp can be used.

Resolution

  • Remove the parameter, and get the value in the body of the advice, for instance from a service or a static property of your application.
  • When the value is one that PostSharp binds, use the binding attribute of PostSharp instead. The table in Binding attributes lists them.

In the following example, the advice reads the current user from the UserContext class of the application.

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

namespace Errors.LA0210_Fixed;

public static class UserContext
{
    public static string CurrentUser { get; set; } = "alice";
}

[PSerializable]
public class AuditAttribute : MethodLevelAspect
{
    [OnMethodEntryAdvice]
    public void OnEntry( [DeclarationName] string methodName )
    {
        // The advice reads the value from the application instead of a parameter.
        string user = UserContext.CurrentUser;

        Console.WriteLine( $"{user} calls {methodName}." );
    }
}

public class OrderService
{
    [Audit]
    public void Cancel( int orderId )
    {
        Console.WriteLine( $"Order {orderId} is canceled." );
    }
}

public static class Program
{
    public static void Main()
    {
        new OrderService().Cancel( 42 );
    }
}
alice calls Cancel.
Order 42 is canceled.

See Also

Reference

AdviceParameterAttribute

Other Resources

Advice Parameters
Advice Methods