The PostSharp team is excited to announce today the first public release of PostSharp 3, available for download on the Visual Studio Gallery and NuGet (make sure to allow prereleases).

This release marks an important turning point in the way we talk about PostSharp. During the last 8 years, we have successfully positioned PostSharp as the leading framework for aspect-oriented programming in Microsoft .NET. We provided an abstract construction kit and told customers they could build “whatever they wanted” with it. However, we became increasingly aware that the vast majority of our customers were all re-implementing the same aspects.

In addition to just providing a wonderful framework, we now offer more value to our customers by delivering ready-made implementations of the most common design patterns that hang over application development today:

  • Logging for System.Diagnostics, NLog, Log4Net (Enterprise Library coming soon);
  • Design patterns for safe multithreading;
  • INotifyPropertyChanged beyond the obvious;
  • Architectural validation;
  • Change tracking and undo/redo (coming soon);
  • Value validation: not null, regular expression, range, custom rules, … (coming soon);

Long-time PostSharpers know that we already started building on this vision nearly a year ago with our PostSharp Toolkits, published as free add-ons to PostSharp 2.1 Pro Edition. Although the toolkits have always been an important part of our vision for PostSharp 3, we wanted to nurture these projects jointly with our community and release them continuously. The incubation period is now over, and we are happy to announce that PostSharp toolkits will join our product line as first-class members.

With PostSharp 3, not only do we provide ready-made solutions to top problems, but we also make these solutions much more accessible than ever. With the new content-sensitive smart tags and wizards, you will be able to implement design patterns in a matter of minutes, without need to read extensive documentation.

So, beyond the new orientation, what’s new in PostSharp 3?

Smart tags and wizards in Visual Studio

The single most striking feature of PostSharp 3 is its deeper integration with Visual Studio.

For instance, move the caret to the name of a class or a method. Visual Studio will display a smart tag proposing actions that make sense in the current context: apply a threading model, add logging, implement INotifyPropertyChanged, …

A wizard-like user interface then collects all relevant settings and performs the required operations, such as installing a NuGet package and adding a custom attribute to your code.

Support for Visual Studio 2012

Visual Studio 2012 is now fully supported. Windows Store projects are supported too (see below). Visual Studio 2010 is still supported at an equal level of features, but support for Visual Studio 2008 has been discontinued.

First-Class Support for Windows Store Apps, Silverlight, and Windows Phone

Silverlight and Windows Phone (previously .NET Compact Framework) have been supported since PostSharp 1.5, but only a small subset of features was actually available for these platforms. This limitation was due to the inability of previous versions of PostSharp to execute, at build-time, code that was linked to exogenous platforms. Concretely, this affected features such as CompileTimeValidate, CompileTimeInitialize, IAspectProvider and MethodPointcut.

Thanks to some significant engineering effort, this limitation is now removed and all PostSharp features are now equally available on all supported platforms.

Support for Portable Class Libraries

Exogenous platforms are now supported through a single Portable Class Library that can target .NET 4.0, Silverlight 4, Windows Phone 7, and Windows Store 8. Therefore, it is now possible to create portable aspect libraries.

Note that a separate version of PostSharp.dll is still available for .NET 2.0. Unlike the portable version, this one supports serialization of aspects through the BinaryFormatter and provides backward compatibility not only with previous versions of .NET, but also with previous versions of PostSharp.

Because the BinaryFormatter is not portable, we had to develop our own portable serializer. Unlike other formatters readily available with the portable class library, and just as the BinaryFormatter, our implementation serializes the internal object structure (i.e. fields, and not public properties) and supports cyclic object graphs. To use the portable serializer, just use [PSerializable] instead of [Serialiable] and [PNonSerialized] instead of [NonSerialized]. This alone is already a pretty piece of software!

In order to provide support for portable class libraries, we had to do some breaking changes in PostSharp.dll. Specifically, the interface _Assembly is now replaced everywhere by the class Assembly. We had to use the interop interface _Assembly in previous versions of PostSharp because the class Assembly used to be sealed. Since _Assembly is not portable and Assembly is now portable and abstract, we decided to make this breaking change.

Practically, it means that PostSharp 3 won’t compile aspects linked to a previous version.

Unified Deployment Experience

As industry has evolved since PostSharp’s debuts in 2004, we decide to embrace Microsoft’s new vision of development tools deployment. PostSharp distribution is now clearly split into two parts: the compiler is now only distributed as a NuGet package and published on the NuGet Gallery, and the user interface is shipped as a VSIX package and published on the Visual Studio Gallery.

Feature Scavenging

It’s sometimes necessary to make difficult choices and discontinue support for scenarios that seem no longer central. That’s what we did with the following features:

  • Support for Mono as a build platform,
  • Support for Visual Studio 2005 and .NET 3.5 as a build platform (.NET 2.0 is still supported as a runtime platform),
  • Support for .NET Compact Framework (Windows Phone 7 is supported through the Portable Class Library),
  • Support for Silverlight 3 (Silverlight 4 is supported through the Portable Class Library),
  • Diagnostic build,
  • Diagnostic console,
  • Windows Installer distribution,
  • Zip package distribution.

Customers who rely on these features have the possibility to keep using PostSharp 2.1, which is still maintained. Also, note that the user interface of PostSharp 3 is compatible with the compiler of PostSharp 2.1, so side-by-side usage is possible.

Commercial customers who are severely affected by these deprecations are invited to contact us directly.

Continuous Delivery and Subscription-Based Maintenance

As of PostSharp 2.1 we officially switched to a continuous delivery process. We abandoned the distinction between “milestone releases” and “hotfixes” to assure that our customers will always have the latest build from the download page on our website, as well as on NuGet.

All paid PostSharp 3 licenses will come with subscription-based maintenance including 1 year of free updates, web-based technical support, phone support, remote assistance and issue escalation. Maintenance subscriptions are renewed annually with multi-year subscriptions available. Pricing for all PostSharp 3 editions will be announced shortly.

Conditions for Free Upgrade

Those who recently purchased PostSharp 2.1 without support subscription will be eligible for a free upgrade to PostSharp 3, with 6 months of free maintenance from the original date of purchase. This offer is valid only for customers who purchase PostSharp 2.1 within 6 months of the official PostSharp 3 release, scheduled for Q1 2013.

Current PostSharp 2.1 customers with support subscription can upgrade to PostSharp 3 and will receive an additional 6 months of maintenance to their subscription – at no cost.


Note that PostSharp is currently in pre-release quality. It comes with several minor issues and the following limitations:

  • All new APIs are still subject to change.
  • Support for obfuscation is not yet available for portable class library.


Starting today, you will hear much less about aspect-oriented programming from us and much more about patterns. Engineers need patterns because they have been proven to reduce complexity and PostSharp provides ready-made implementations of some of the most common patterns found in .NET applications. Furthermore, PostSharp helps you to build custom design patterns of your own, implement them automatically, and validate that they have been implemented properly. AOP remains at the heart of PostSharp as one of the enabling technologies, but now PostSharp adds even more value to software engineering.

Happy PostSharping!


We’ve all experienced, with great frustration, desktop application freeze: the user interface becomes unresponsive and, after a while, the message “Not Responding” is displayed in the title bar and the only escape is to kill the process. Typically, application freezes are the result of a deadlock where the foreground thread, instead of processing the message loop, waits for some resource to be released by a background thread, which in turn waits for the foreground thread to release some other resource.

Deadlocks Defined

A deadlock is a situation in which two or more competing actions are waiting for each other to finish, and thus neither ever does. Whenever you’re using locks there is a risk of deadlocks.

There are four main conditions necessary for a deadlock to occur:

a) A limited number of instances of a particular resource. In the case of a monitor in C# (what you use when you use the lock keyword), this limited number is one, since a monitor is a mutual-exclusion lock.

b) The ability to hold one resource and request another. In C#, this can be done by locking on one object and then locking on another before releasing the first lock, for example:


c) No preemption capability. In C#, this means that one thread can't force another thread to release a lock.

d) A circular wait condition. In C#, this means that thread 1 is waiting for thread 2, thread 2 for 3 and the last one is waiting for thread 1. This makes a cycle that results in deadlock.

If any one of these conditions is not met, deadlock is not possible.

Avoiding Deadlocks

So simple solution to deadlock problem would be to ensure that at least one of above condition is not met at any time in your application. Unfortunately all above conditions are met in any large-scale application using C#.

In theory, deadlocks could be defeated by aborting one of the threads involved in the circular relationship. However, interrupting a thread is a dangerous operation unless its work can be fully and safely rolled back as is the case with database stored procedures.

Some advanced techniques such as lock leveling (link) have been proposed to prevent the apparition of deadlocks. With lock leveling, all locks are assigned numeric value and a thread can acquire only locks with number greater than those it already holds. As you can imagine, this would stop you from using pretty much anything from the .NET Framework, so it is not a practical solution.

Detecting Deadlocks

Since we can’t prevent deadlocks, we can try to detect them and, if we find one, we can eliminate it. Since application code is typically not transactional, the only safe action we can take to eliminate a deadlock is to terminate the application after having written appropriate diagnostic information that will help developers understanding and resolving the deadlock. The rationale here is that it is better to terminate an application properly than to let it remain in a frozen state. This rationale is true for both client and server applications.

In order to detect deadlocks we have to track all blocking instructions used in user code and build threads dependency graph from them. When deadlock is suspected all we have to do is check if there is a cycle in the graph.

It sounds simpler than it really is. Tracking all locking instructions using hand-written C# code would be very tedious. Normally one would write a wrapper for all synchronization primitives such as Monitor and use these wrappers instead of standard sync objects. This generates a couple of problems: a need to change existing code, introduction of boilerplate code and clutter to your codebase.

Moreover, there are some synchronization primitives, such as semaphore or barrier, which are hard to track because they can be signaled from any thread.

Detecting Deadlocks using the PostSharp Threading Toolkit

PostSharp Threading Toolkit features a drop-in deadlock detection policy that tracks use of thread synchronization primitives in transparent way without requiring any change in your code. You can use locks, monitors, mutexes and most common primitives in the usual way and PostSharp Threading Toolkit will track all resources for you.

When a thread waits for a lock for more than 200ms, the toolkit will run a deadlock detection routine. If it detects a deadlock, it will throw DeadlockException in all threads that are part of the deadlock. The exception gives a detailed report of all threads and all locks involved in the deadlock, so you can analyze the issue and fix it.

In order to use deadlock detection mechanism, all you have to do is:

  1. Add the PostSharp-Threading-Toolkit package to your project using NuGet.

  2. Add the following line somewhere in your code (typically in AssemblyInfo.cs):

[assembly: PostSharp.Toolkit.Threading.DeadlockDetectionPolicy]


In order to be effective, deadlock detection should be enabled in all projects of your application.

Supported Threading Primitives

Here’s the list of synchronization methods supported by DeadlockDetectionPolicy:

  • Mutex: WaitOne, WaitAll, Release
  • Monitor: Enter, Exit, TryEnter, TryExit (including c# lock keyword; Pulse and Wait methods are not supported)
  • ReaderWriterLock: AcquireReaderLock, AcquireWriterLock, ReleaseReaderLock, ReleaseWriterLock, UpgradeToWriterLock, DowngradeToReaderLock (ReleaseLock, RestoreLock not supported)
  • ReaderWriterLockSlim: EnterReadLock, TryEnterReadLock, EnterUpgradeableReadLock, TryEnterUpgradeableReadLock, EnterWriteLock, TryEnterWriteLock, ExitReadLock, ExitUpgradeableReadLock, ExitWriteLock,
  • Thread: Join

When using deadlock detection you can use all above methods in normal manner and threading toolkit will do the rest. In the current version of the Threading Toolkit we assume that waits with timeout don’t cause deadlocks (eventual deadlock will be broken when timeout expires). Due to performance issues deadlock detection is enabled only when DEBUG or DEBUG_THREADING debugging symbols are defined.


Although PostSharp Threading Toolkit detects a large class of deadlocks, we cannot detect deadlocks involving any of the following elements:

  • Livelocks, i.e. situation when threads alternate acquiring resources not advancing in execution (A real-world example of livelock occurs when two people meet in a narrow corridor, and each tries to be polite by moving aside to let the other pass, but they end up swaying from side to side without making any progress because they both repeatedly move the same way at the same time);
  • Asymmetric synchronization primitives, i.e. primitives such as ManualResetEvent, AutoResetEvent, Semaphore and Barrier, where it is not clear which thread is responsible for “signaling” or “releasing” the synchronization resource. There are some sophisticated algorithms that can detect deadlocks involving semaphores and other asymmetric synchronization mechanisms but these require advanced static code analysis, have enormous computation cost and some even argue that they cannot be implemented in languages with delegates and virtual methods at all.

Even in the case of supported threading primitives some methods make deadlock detection hard or impossible:

  • Getting a SafeWaitHandle of a Mutex is dangerous, because there is a risk that it gets exposed to unmanaged code, which is not tracked by the toolkit.
  • Methods ReleaseLock/RestoreLock of ReaderWriterLock are not supported.
  • Methods Pulse/Wait of Monitor are not supported.

What we need to avoid at any cost, is any kind of false positive, i.e. reporting a deadlock when there is none. In order to avoid such cases we construct a list of ignored resources which are not considered during deadlock detection routine. Additionally, if ignored resources list expands too much, deadlock detection becomes inefficient, so when ignored list contains more than 50 objects we disable deadlock detection mechanism and issue a debug trace informing about it.


If you’ve ever experienced a deadlock in production, you know how hard it is to diagnose. Not anymore. By adding the PostSharp-Threading-Toolkit NuGet package to your project and adding a single line of code to your codebase, you can get nice exception messages whenever a deadlock occurs.

In case you’re interested how this works under the hood, simply have a look at the source code in the Github repository (

Happy PostSharping!

With all the good reasons mentioned by Ayende, we decided to officially switch PostSharp 2.1 to a continuous deployment process. We are abandoning the distinction between “milestone releases” and “hotfixes”. Since today, you will know always download the latest revision from the principal download page, as well as on NuGet.

Ayende’s article came at a time when we were frustrated with our release cycle. Although we released dozens of hotfixes, the front-page download was still the initial 2.1 RTM release because we constantly deferred the SP1. Clearly, the majority of people were not downloading the best-quality release, and were therefore hitting bugs that have already been solved for a long time. Not the best user experience!

Technically, the difference is quite small. PostSharp’s build and test processes have always been automated. It takes about 50 minutes to build and test all components. Uploading to our download manager is very easy too – copy the build output to a network share and execute a script that synchronizes with Amazon S3. Therefore, the time-to-market of a bug fix has always been very short – a couple of hours, typically. The fact that deployment is now integrated with the build script does not really change this fact.

Thus, the most significant difference is that we will actually publish hotfixes as front-page downloads.

Unlike “hotfixes”, “milestone releases” got additional manual testing on virtual machines, where the later got only automated testing. Installation tests are still not automated, so there’s a slight chance to get a regression in a front-page release. We bet that these defects will be infrequent and quickly detected by the community.

Bottom line: we bet that the overall quality will be higher by switching to continuous development.

Happy PostSharping, now with a fresh version!