Announcing PostSharp 3.1 Preview

by Gael Fraiteur on 31 Oct 2013

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:

[Serializable]
public class MyAspect : OnMethodBoundaryAspect, IOnStateMachineBoundaryAspect
{
    public static StringBuilder Trace = new StringBuilder();

    public override void OnEntry(MethodExecutionArgs args)
    {
        Console.WriteLine("OnEntry");
    }

    public void OnResume(MethodExecutionArgs args)
    {
        Console.WriteLine("OnResume");
    }

    public void OnYield(MethodExecutionArgs args)
    {
        Console.WriteLine("OnYield");
    }

    public override void OnSuccess(MethodExecutionArgs args)
    {
        Console.WriteLine("OnSuccess");
    }

    public override void OnExit(MethodExecutionArgs args)
    {
        Console.WriteLine("OnExit");
    }
}

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.

-gael