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.
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'd like to invite you to join us for the FREE full-day training in London slated for November 7th, 2013. Learn how to automate design pattern implementation and validation using PostSharp with its creator, Gael Fraiteur.
Here's the agenda:
Morning: ready-made design patterns
09.00-10.00 Introduction to Design Pattern Automation with PostSharp
10.05-10.55 Threading patterns
11.30-12.30 Hands-On Exercises
Lunch: free catered lunch for all attendees!
Afternoon: custom patterns
13.35-14.00 Introduction to Aspect-Oriented Programming
14.00-14.45 Simple aspect types, aspect lifetime, applying aspects to code
14.45-15.15 Hands-On Exercises
15.30-16.00 Aspect composition, composite aspects
16.00-16.45 Code Validation and Analysis
16.50-17.30 Hands-On Exercises
Because space is limited, we will draw 15 attendees at random at the end of October and notify them via email with additional course details.
Sign up for your chance to attend the PostSharp Training today!
I’m excited to announce that I’ll participate as a speaker to the first NDC London. I’ll be delivering my talk pretentiously named You Thought You Understood Multithreading, a deep dive that starts from the lock keyword and CPU microarchitecture and the Windows kernel.
For those who don’t know the NDC concept: this is an awesome combination of top-notch content and undeniable fun. The serious side is taken care of by a wide board of speakers, ranging from recognized personalities such as Robert Martin and Scott Guthrie to less known innovators (like me!) and luminaries.
As for entertainment, the NDC team could not find a better partner than Carl Franklin and Richard Campbell from .NET Rocks. I can only talk of what I’ve seen the last time I was at NDC. This was in Oslo in June 2011, two months before the twins got born and the two-years self-inflicted house arrest that followed. Richard kicked off the party by the hilarious story of how the teenager he was acquired a 4MB hard drive (as huge as a washing machine) and filled it with… ASCII p0rn. Then Richard joined with guitars and they start playing, guess what… rock! They are not named .NET Rocks without a reason. The second half of the evening was guaranteed by a local rock band. I forgot the name, and just remember I was picked by the security team after having tied the laces of the lead singer’s shoes and taken him on my shoulders. Hem. I didn’t know I was so strong. I was happy to wake up fresh enough to give my talk in the first slot of the next morning.
Lesson taken: you don’t want to miss the NDC party, but as the speaker you don’t want the first slot after the party.
I’ll be in London between December 4th and December 6th. If you want to meet up, discuss how you love PostSharp or how it sucks, please contact me and we’ll set up a date. And make sure to attend my talk on Thursday.