Today we’re excited to announce the public availability of PostSharp 3.1 Preview. You can download the new Visual Studio tooling from our website, our update your packages using NuGet Package Manager. In the latter case, make sure to enable the “Include Prereleases” option.

PostSharp 3.1 includes the following features:

  • Support for state machines (async and iterators)
  • Improved build performance
  • Resolution of file and line of error messages
  • Solution-level policies (and a smarter configuration system)
  • Indentation in logging

PostSharp 3.1 is a free upgrade for all customers with a valid support subscription. The license key of PostSharp 3.0 will work.

Support for state machines

When you applied an OnMethodBoundaryAspect to a method that was compiled into a state machine, whether an iterator or an async method, the code generated by PostSharp would not be very useful:  the aspect would just be applied to the method that implements the state machine. An OnException advise had no chance to get ever fired.

PostSharp 3.1 gets much smarter. OnMethodBoundaryAspect now understands that is being applied to a state machine, and works as you would expect. To enable the new behavior, you need to set the OnMethodBoundaryAspect.ApplyToStateMachine property to true. It is false by default for backward compatibility.

But there is more: the aspects define two advises: OnYield and OnResume. For the sake of backward compatibility, we could not add them to the IOnMethodBoundaryAspect interface, so we defined a new interface IOnStateMachineBoundaryAspect with these two methods.

More blogging about this later.

To discover more on your own, try to apply the following aspect to an async method or an iterator:

public class MyAspect : OnMethodBoundaryAspect, IOnStateMachineBoundaryAspect
    public static StringBuilder Trace = new StringBuilder();

    public override void OnEntry(MethodExecutionArgs args)

    public void OnResume(MethodExecutionArgs args)

    public void OnYield(MethodExecutionArgs args)

    public override void OnSuccess(MethodExecutionArgs args)

    public override void OnExit(MethodExecutionArgs args)

Read more about this feature.

Improved build performance

The first time you compile a project using a specific build of PostSharp, you will be proposed to install itself into GAC and create native images.

Doing will decrease build time of a fraction of a second for each project. A potentially substantial gain if you have a lot of projects. You can uninstall these images at any time from PostSharp options in Visual Studio.

Under the cover, PostSharp will just install itself in GAC using gacutil and will generate native images using ngen.  The feature does not affect build servers or cloud builds, so only build time on developers’ workstations will be improved.

Keep in mind that your end-users will not have PostSharp installed in GAC, so you still need to distribute PostSharp.dll.

Resolution of file and line of error messages

When previous versions of PostSharp had to report an error or a warning, it would include the name of the type and/or method causing the message, but was unable to determine the file and line number.

You can now double-click on an error message in Visual Studio and you’ll get to the relevant location for the error message.

I know, it seems like an obvious feature. But it was actually quite complex to implement. PostSharp works as MSIL level and the file that’s supposed to MSIL back to source – the PDB file – won’t tell you where a type, field, or abstract method is implemented. Indeed, it only contains “sequence points”, mapping instructions to lines of code. That is, only the inside of method bodies are mapped to source code.  So, we had to rely on a source code parser to make the missing like ourselves.

This feature is available for the C# language only.

Note that this feature is not yet fully stable. There are still many situations where locations cannot be resolved.

Solution-Level Policies

We made it easier to add a policy to a whole solution. Say you want to add logging to the whole solution. Previously, you had to add the aspect to every project of the solution. Now, you can right-click on the solution and just add it to the solution.

This is not just a UI tweak. To make this scenario possible, we had to do significant work on our configuration subsystem:

  • Support for solution-level configuration files (SolutionName.pssln), additionally to project-level files (ProjectName.psproj).
  • Support for conditional configuration elements.
  • Support for XPath in expressions (instead of only property references as previously).

Thanks to this improvements, you can add not only our own ready-made aspects to the whole solution, but also your own aspects.

The configuration subsystem is currently largely undocumented and there was not a good use case for it until PostSharp 3.0. I’ll blog further about this feature and we’ll update the documentation.

Read more about support for solution-level policies.

Indentation in logging

PostSharp Diagnostics Pattern Library was good at adding a lot of logging, but the log quickly became unreadable because the output was not indented. We fixed that. If the logging back-end supports indentation, we’ll call its Indent or Unindent method. Otherwise, we’ll create indentation using spaces.

Our implementation is thread-safe and still does not require you to add a reference to any PostSharp library at runtime (the reference is required at build time and will be removed).

Improvements in PostSharp 3.0

Note that while working on PostSharp 3.1, we still added some features to PostSharp 3.0. The most important ones are support for Windows 8.1 and Visual Studio 2013. Keeping in pace with changes of development environments is a challenge in itself, and we’re glad this we handled it smoothly, without forcing customers to wait for a new minor version.

Please report any issue on our support forum. We’d love to hear about your feedback.

Happy PostSharping – faster than ever.


We’ve got great news for those of you who have been waiting patiently for a PostSharp online training course to become a reality:

Pluralsight, the online training provider for hardcore developers, announced this week the availability of the PostSharp Fundamentals course authored by senior software developer, trainer, and PostSharp MVP – Donald Belcham.

The course is an introduction to using PostSharp and covers all the fundamentals needed to effectively use it to create basic aspects in your applications. The course includes:

  • Introduction (Why and How to use Aspect-Oriented Programming)
  • Installation (EXE & MSI, NuGet, Source Control, License Management)
  • Out of the Box Aspects (OnMethodBoundaryAspect, OnExceptionAspect, EventInterceptionAspect, etc.)
  • Aspect Lifetime (Compile Time, Serialization & Deserialization, Run Time)
  • Existing Codebases (Approach, Attaching Aspects, Side Effects, Low Hanging Fruit)
  • Deployment (Deployables, Signing Assemblies, Obfuscation)
  • Developer Experience (Debugging, IDE Enhancements)

Many thanks to Aaron Skonnard and his team at Pluralsight for continuing to deliver quality online training from some of today’s leading .NET developers. Also a big congratulations and thanks to our good friend Donald Belcham for his continuing efforts to spread the word about the benefits of using PostSharp for AOP.

Watch Donald’s PostSharp Fundamentals course on Pluralsight >>

During compilation, PostSharp takes great care in making sure that everything works correctly. When something goes wrong, PostSharp will report it as an error or a warning. Until now, however, whenever an error or a warning occurred, the developer had to manually navigate to that place in code.

We are excited to announce that with PostSharp 2.1 we’ve enhanced the errors and warnings with the exact location information, allowing you to simply double-click on the message and you’ll be taken to the error line!

To enable this feature, go to Tools – Options – PostSharp, and under Experimental, set Resolve Message Location to True:

Then, simply rebuild your solution, and if there are any warnings or errors, you’ll be able to see exactly where they are:

This is accomplished by specifying the member (in this case, the method) that is responsible for the message, in the aspect’s CompieTimeValidate method:

// Validate the attribute usage.
public override bool CompileTimeValidate( MethodBase method )
    // Don't apply to constructors.
    if ( method is ConstructorInfo )
        Message.Write( method, SeverityType.Error, "CX0001", 
                       "Cannot cache constructors." );
        return false;

    MethodInfo methodInfo = (MethodInfo) method;

    // Don't apply to void methods.
    if ( methodInfo.ReturnType.Name == "Void" )
        Message.Write( method, SeverityType.Error, "CX0002", 
                       "Cannot cache void methods." );
        return false;

    // Does not support out parameters.
    ParameterInfo[] parameters = method.GetParameters();
    for ( int i = 0; i < parameters.Length; i++ )
        if ( parameters[i].IsOut )
            Message.Write( method, SeverityType.Error, "CX0003", 
                           "Cannot cache methods with return values." );
            return false;

    return true;

Aspect developers are encouraged to include the member in error/warning messages. For more information, please refer to the documentation on Working with Errors, Warnings and Messages.

Please note that this is not enabled by default as it is still experimental and might have an impact on performance. Please let us know how it works out for you!

Happy PostSharping!