Search code examples
c#postsharp

How can I apply postsharp aspect to a website project?


I use a simple postsharp.config file :

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.postsharp.org/1.0/configuration">
  <Multicast xmlns:my="clr-namespace:ExceptionAutoSerializer.Aspects;assembly:ExceptionAutoSerializer">
    <my:MethodBoundaryAspect  AttributeTargetTypes="MyTopLevelNamespace.*" />
    <my:MethodBoundaryAspect  AttributeTargetMembers="*ctor*" AttributeExclude="true"/>
    <my:MethodBoundaryAspect  AttributeTargetMembers="get_*" AttributeExclude="true"/>
    <my:MethodBoundaryAspect  AttributeTargetMembers="set_*" AttributeExclude="true"/>
  </Multicast>
</Project>

All my project in my solution are under the namespace MyTopLevelNamespace. And every single project in the solution has the aspect applied to it correctly except for my website project. I'm not familiar with the solution as I just got in the dev team.

All I know is that I would like to apply aspect to classes within this project and that postsharp seems to ignore that particular project. The config file is located in the src/ folder and should be applied to all project.

I've made sure the types I'm applying my aspect to are under the namespace specified in the config file and that it doesn't match any of the excluding patterns.

Did I provide enough information ? I'm not sure it is due to the project beeing a website project but I can't see anything else.

Edit: I've made sure I added the nuget package to the project. I also tried to manually add the aspect with an attribute to a specific method of this project and the aspect doesn't trigger.

Edit2: this is the method that I use to test:

[MethodBoundaryAspect]
public bool Foo(string bar1, string bar2)
{
            // at runtime test contains indeed one attribute MethodBoundaryAspect
            var test = this.GetType().GetMethod("ValidateUser").GetCustomAttributes(false);
            //here the exception is caught higher up but the "onException" of my attribute doesn't trigger
            throw new Exception("test exception");
}

and my postsharp aspect :

namespace ExceptionAutoSerializer.Aspects
{
    [Serializable]
    public class MethodBoundaryAspect : OnMethodBoundaryAspect
    {

        //[...]

        public override void OnEntry(MethodExecutionArgs args)
        {
            //[...]
        }

        public override void OnSuccess(MethodExecutionArgs args)
        {
            //[...]
        }

        public override void OnException(MethodExecutionArgs args)
        {
            //[...]
        }
    }
}

Solution

  • According to an old answer of a developper at PostSharp Technologies :

    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). Daniel Balas

    I didn't find any update on that subject for version older than 4.3. For 4.3 and earlier according to the previous answer and this documentation (p. 45) website project are not supported.

    Edit: when questioning the previously quoted author about the current validity of they answer they responded this in comment :

    @Amon For project-less websites (which IIRC are not creatable from the UI in VS 2017 and later) it is still true. However, all web projects that have csproj/vbproj (.NET or .NET Core) are working as expected (this also didn't change since then). – Daniel Balas