Open sandboxFocusImprove this doc

LA0179: Method Boundary Aspect Applied to a Constructor of Another Assembly

Severity Error
Reported by PostSharp
Message Cannot apply the aspect "<Aspect>" to the instance constructor of type "<Type>": applying OnMethodBoundaryAspect or OnExceptionAspect to an instance constructor of another assembly is not supported. Restrict the aspect to methods.

Cause

An aspect can enhance the declarations of a referenced assembly when you multicast it with the AttributeTargetAssemblies property. PostSharp does not modify the other assembly: it replaces the calls that the current assembly makes to the target method with calls to a generated method that runs the aspect. PostSharp does not support this for constructors, and reports LA0179 when an OnMethodBoundaryAspect or an OnExceptionAspect reaches an instance constructor of another assembly.

This usually happens by default: the AttributeTargetElements of these aspects include the instance and static constructors, so an aspect multicast to a type of another assembly also reaches the constructors that the current assembly calls. <Type> is the type that declares the constructor. The message has no location in the source code.

In the following example, a shipping library defines RateTable:

namespace Errors.LA0179_Error.Shipping;

public class RateTable
{
    private readonly decimal ratePerKg;

    public RateTable( decimal ratePerKg )
    {
        this.ratePerKg = ratePerKg;
    }

    public decimal GetPrice( decimal weightKg ) => weightKg * this.ratePerKg;
}

The application multicasts a [Log] aspect to RateTable, whose constructor it calls:

// @SkipPostSharpOnDependency

using System;
using Errors.LA0179_Error.Shipping;
using PostSharp.Aspects;
using PostSharp.Extensibility;
using PostSharp.Serialization;

// The aspect is multicast to all methods of RateTable, including its constructor, which is in another assembly.
[assembly: Errors.LA0179_Error.Log(
    AttributeTargetAssemblies = "LA0179_Error.Dependency",
    AttributeTargetTypes = "Errors.LA0179_Error.Shipping.RateTable" )]

namespace Errors.LA0179_Error;

[PSerializable]
public class LogAttribute : OnMethodBoundaryAspect
{
    public override void OnEntry( MethodExecutionArgs args )
    {
        Console.WriteLine( $"Calling {args.Method.DeclaringType!.Name}.{args.Method.Name}" );
    }
}

public static class Program
{
    public static void Main()
    {
        RateTable rates = new( 2.5m );

        Console.WriteLine( $"Price: {rates.GetPrice( 4m ):0.00}" );
    }
}
error LA0179: Cannot apply the aspect "Errors.LA0179_Error.LogAttribute" to the instance constructor of type "Errors.LA0179_Error.Shipping.RateTable": applying OnMethodBoundaryAspect or OnExceptionAspect to an instance constructor of another assembly is not supported. Restrict the aspect to methods.

Resolution

  • Restrict the aspect to methods: set AttributeTargetElements to Method.
  • When you own the library, apply the aspect in the library itself, where PostSharp can enhance the constructor.

In the following example, the aspect is applied to RateTable.GetPrice only.

// @SkipPostSharpOnDependency

using System;
using Errors.LA0179_Fixed.Shipping;
using PostSharp.Aspects;
using PostSharp.Extensibility;
using PostSharp.Serialization;

// The aspect is multicast to the methods of RateTable only, not to its constructor.
[assembly: Errors.LA0179_Fixed.Log(
    AttributeTargetAssemblies = "LA0179_Fixed.Dependency",
    AttributeTargetTypes = "Errors.LA0179_Fixed.Shipping.RateTable",
    AttributeTargetElements = MulticastTargets.Method )]

namespace Errors.LA0179_Fixed;

[PSerializable]
public class LogAttribute : OnMethodBoundaryAspect
{
    public override void OnEntry( MethodExecutionArgs args )
    {
        Console.WriteLine( $"Calling {args.Method.DeclaringType!.Name}.{args.Method.Name}" );
    }
}

public static class Program
{
    public static void Main()
    {
        RateTable rates = new( 2.5m );

        Console.WriteLine( $"Price: {rates.GetPrice( 4m ):0.00}" );
    }
}
Calling RateTable.GetPrice
Price: 10.00

See Also

Reference

AttributeTargetAssemblies
AttributeTargetElements
OnMethodBoundaryAspect
OnExceptionAspect

Other Resources

Understanding Attribute Multicasting