0
votes

I am using PostSharp - OnMethodBoundaryAspect in OperationContract. The aspect doesnt seem to fire though. I have the necessary reference to postsharp dll. Pseudo code below.

  • Aspect

    [Serializable]
    [MulticastAttributeUsage(MulticastTargets.Method, Inheritance =  MulticastInheritance.Multicast)]
    public class LoggingAspect : OnMethodBoundaryAspect
    {
      //Implementation for entry and exit
    }
    
  • Usage

    [ServiceContract]
    public interface ITest
    {
     [OperationContract]
     [LoggingAspect(.....)]
      void Test();
    }
    

Any help is deeply appreciated

1
Adding reference to PostSharp.dll is not enough, PostSharp needs to install itself into a build process. Do you also have import of PostSharp.targets in your *.csproj file? Is there a message in the build log indicating that PostSharp has been invoked? Finally, what happens if you apply the aspect in the implementation class instead of interface? - AlexD
I dont have a PostSharp.targets in .csproj. I had installed though nuget in a test project. In my main project; I am just referencing the postsharp dll. What should be done to include the targets? - Sudeep Syamnath
If you have added the PostSharp package to the project, it should add targets automatically. Note that you also need to have this package added to projects that contain implementation of this interface (so that PostSharp can process those projects). - Daniel Balas
Yes I was not adding it as reference to the containing projects as well. The targets were missing. But now I have rectified it. Thanks Alex and Daniel - Sudeep Syamnath

1 Answers

0
votes

We need to do the following

  1. Install Postsharp through NuGet on the project which you want to write your custom aspect.
  2. This will ensure that the targets are present in the .csproj files thereby enabling injection during compile time.
  3. Install PostSharp though NuGet in all projects where you want to use the aspect which you wrote as mentioned in #1

Thanks AlexD and Daniel Balas for the inputs