Open sandboxFocusImprove this doc

LA0086: Advice Named by the Master Property Not Found

Severity Error
Reported by PostSharp
Message The Master property of the custom attribute "<Advice>" on "<Method>" names "<Master>", but this name gives no group: no advice of the same family has this name, or the Master properties form a cycle. Set the Group property instead: Master is obsolete.

Cause

The advices of a family, for example the method boundary advices OnMethodEntryAdvice and OnMethodExitAdvice, are grouped so that PostSharp weaves them as one transformation. An advice selects its group with the Group property. The Master property, which selects the group of the advice method that it names, is obsolete, and the C# compiler reports its use with the warning CS0618. It keeps working, and Group is its replacement.

PostSharp reports LA0086 when the name given to Master leads to no group: no advice of the same family is applied to a method of this name, because the name has a typo, the method has no advice attribute, or its advice belongs to another family; or the Master properties of several advices name each other in a cycle. <Master> is the value of the property, <Advice> the type of the advice attribute that sets it, and <Method> the advice method that carries this attribute.

In the following example, the OnExit advice names OnEnter in its Master property, but the entry advice of the aspect is OnEntry. The #pragma directive disables the warning CS0618 about the obsolete property.

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

// The Master property is obsolete (CS0618).
#pragma warning disable CS0618

namespace Errors.LA0086_Error;

[PSerializable]
public class TraceMethodsAttribute : TypeLevelAspect
{
    [OnMethodEntryAdvice, MulticastPointcut( Targets = MulticastTargets.Method )]
    public void OnEntry( [DeclarationName] string methodName )
    {
        Console.WriteLine( $"Entering {methodName}." );
    }

    // The aspect has no advice method named OnEnter.
    [OnMethodExitAdvice( Master = "OnEnter" )]
    public void OnExit( [DeclarationName] string methodName )
    {
        Console.WriteLine( $"Leaving {methodName}." );
    }
}

[TraceMethods]
public class OrderService
{
    public void PlaceOrder( string product ) => Console.WriteLine( $"Order placed: {product}" );
}

public static class Program
{
    public static void Main()
    {
        new OrderService().PlaceOrder( "Book" );
    }
}
error LA0086: LA0086_Error.cs(23,17): The Master property of the custom attribute "PostSharp.Aspects.Advices.OnMethodExitAdvice" on "Errors.LA0086_Error.TraceMethodsAttribute.OnExit(System.String)" names "OnEnter", but this name gives no group: no advice of the same family has this name, or the Master properties form a cycle. Set the Group property instead: Master is obsolete.

Resolution

  • Remove Master, when the class of the advice has only one group that has no advice of its kind: PostSharp then puts the advice in this group.
  • Otherwise, replace Master with Group, and set the same name on all the advices of the group.

In the following example, the OnExit advice has no Master property any more. The group of OnEntry is the only group of the class, so OnExit joins it, and the pointcut of OnEntry applies both advices to the methods of OrderService.

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

namespace Errors.LA0086_Fixed;

[PSerializable]
public class TraceMethodsAttribute : TypeLevelAspect
{
    [OnMethodEntryAdvice, MulticastPointcut( Targets = MulticastTargets.Method )]
    public void OnEntry( [DeclarationName] string methodName )
    {
        Console.WriteLine( $"Entering {methodName}." );
    }

    // Without the obsolete Master property, the advice joins the only group of the class that has no exit advice.
    [OnMethodExitAdvice]
    public void OnExit( [DeclarationName] string methodName )
    {
        Console.WriteLine( $"Leaving {methodName}." );
    }
}

[TraceMethods]
public class OrderService
{
    public void PlaceOrder( string product ) => Console.WriteLine( $"Order placed: {product}" );
}

public static class Program
{
    public static void Main()
    {
        new OrderService().PlaceOrder( "Book" );
    }
}
Entering PlaceOrder.
Order placed: Book
Leaving PlaceOrder.

See Also

Reference

Group
Master
OnMethodEntryAdvice
OnMethodExitAdvice

Other Resources

Selecting Members with Pointcuts
Advice Methods