Search code examples
vb.netvisual-studio-2012aoppostsharpvs-web-site-project

Can postsharp aspects be used on website projects?


I'm trying to use a PostSharp aspect in a website project in VS2012. It seems to work fine when I set up a web application project, but when I apply the aspect attribute to a method on a page in the website project it compiles and runs fine, but my OnMethodBoundaryAspect never gets hit. I tried setting breakpoints and logging from the aspect methods.

Does PostSharp support website projects? if so, what am I missing?

Please no comments about why I want to use a website instead of a web app. Unfortunately it is a requirement (don't ask).

This is my aspect code (all in vb.net), but like I said, it works fine on a web app project:

Imports PostSharp.Aspects

Namespace TestAopLib

<Serializable>
Public Class AopTester
    Inherits OnMethodBoundaryAspect

    Public Overrides Sub OnEntry(args As MethodExecutionArgs)
        MyBase.OnEntry(args)
        Debug.WriteLine("In OnEntry")
    End Sub

    Public Overrides Sub OnExit(args As MethodExecutionArgs)
        MyBase.OnExit(args)
        Debug.WriteLine("In OnExit")
    End Sub

    Public Overrides Sub OnSuccess(args As MethodExecutionArgs)
        MyBase.OnSuccess(args)
        Debug.WriteLine("In OnSuccess")
    End Sub

    Public Overrides Sub OnException(args As MethodExecutionArgs)
        MyBase.OnException(args)
        Debug.WriteLine("In OnException")
    End Sub

End Class

End Namespace

Solution

  • There is a open-source project that achieves exactly this (as also mentioned by CodingSource). However, it was done for PostSharp 2.x and is neither recommended or supported (as stated on project's home page). It would most likely not work with PostSharp 3+ without major issues.

    PostSharp is (currently) integrated via MSBuild (as also mentioned by John Saunders), which is not used by website projects.

    While in it's core PostSharp is a command-line tool, it gets so much information from MSBuild that it's quite hard to make it work separately (and neither advised nor documented nor supported in the first place).

    P.S.: I currently work for PostSharp technologies.