Open sandboxFocusImprove this doc

LA0036: Aspect Library Not Processed by PostSharp

Severity Warning
Reported by PostSharp
Message Missing optimization information on method '<Method>'. This information is computed automatically by PostSharp. Make sure that assembly <Assembly> is processed by PostSharp.

Cause

When PostSharp builds the assembly that defines an aspect, it analyzes each advice method, such as OnEntry, to find which parts of the advice arguments the method uses, and stores the result in a custom attribute on the method. When another project applies the aspect, PostSharp reads this attribute to generate only the code that the advice needs.

PostSharp reports LA0036 when an advice method of an aspect from a referenced assembly does not have this attribute, because the assembly was built without PostSharp: its project references the PostSharp.Redist package but not the PostSharp package, or it sets the SkipPostSharp MSBuild property to True. The warning is reported on the advice method. PostSharp still applies the aspect, but without the optimizations.

In the following example, [Trace] is defined in a library that is built without PostSharp.

using System;
using PostSharp.Aspects;
using PostSharp.Aspects.Configuration;
using PostSharp.Aspects.Serialization;

namespace Errors.LA0036_Error.Diagnostics;

// This library is built without PostSharp, so the advices of TraceAttribute are not analyzed.
[OnMethodBoundaryAspectConfiguration( SerializerType = typeof(MsilAspectSerializer) )]
public class TraceAttribute : OnMethodBoundaryAspect
{
    public override void OnEntry( MethodExecutionArgs args ) => Console.WriteLine( $"Entering {args.Method.Name}." );
}
// @SkipPostSharpOnDependency

using System;
using Errors.LA0036_Error.Diagnostics;

namespace Errors.LA0036_Error;

public class OrderService
{
    // The aspect comes from a library that PostSharp has not processed.
    [Trace]
    public void PlaceOrder( string product ) => Console.WriteLine( $"Order placed for {product}." );
}

public static class Program
{
    public static void Main()
    {
        new OrderService().PlaceOrder( "Book" );
    }
}
warning LA0036: LA0036_Error.Dependency.cs(12,26): Missing optimization information on method 'Errors.LA0036_Error.Diagnostics.TraceAttribute.OnEntry(PostSharp.Aspects.MethodExecutionArgs)'. This information is computed automatically by PostSharp. Make sure that assembly LA0036_Error.Dependency is processed by PostSharp.

Resolution

Build the project that defines the aspect with PostSharp: add a reference to the PostSharp package, and remove the SkipPostSharp property if the project sets it. See PostSharp Components and Well-Known MSBuild Properties.

In the following example, the library is built with PostSharp, and the program uses the aspect without warning.

using System;
using PostSharp.Aspects;
using PostSharp.Aspects.Configuration;
using PostSharp.Aspects.Serialization;

namespace Errors.LA0036_Fixed.Diagnostics;

// This library is built with PostSharp, which analyzes the advices of TraceAttribute.
[OnMethodBoundaryAspectConfiguration( SerializerType = typeof(MsilAspectSerializer) )]
public class TraceAttribute : OnMethodBoundaryAspect
{
    public override void OnEntry( MethodExecutionArgs args ) => Console.WriteLine( $"Entering {args.Method.Name}." );
}
using System;
using Errors.LA0036_Fixed.Diagnostics;

namespace Errors.LA0036_Fixed;

public class OrderService
{
    // The aspect comes from a library that PostSharp has processed.
    [Trace]
    public void PlaceOrder( string product ) => Console.WriteLine( $"Order placed for {product}." );
}

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

See Also

Reference

OnMethodBoundaryAspect

Other Resources

PostSharp Components
Well-Known MSBuild Properties