1
votes

I have a solution with 3 projects. I made it as a test:

1) The WinFormsProject (setted as Main project): It is just a form that throws an exception when clicking a button. Before throwing the exception, it makes some assignations with an object of the class Person (who is in another assembly).

2) The ConsoleProject: It's another assembly with the same idea: it only plays a little with an object of the class Person and then throws an Exception.

3) The AspectTest: it's an assembly which has defined the class Person and an aspect (called LogBoundary) (inherited from OnMethodBoundaryAspect) who logs OnEntry(), OnExit(), OnSuccess() and OnException().

The three assemblies are configured through an "AspectInfo.cs" class to target every method but the "CompileGenerated" ones. So:

[assembly: LogBoundary()]
[assembly: LogBoundary(
    AttributeExclude = true,
    AttributePriority = 0,
    AttributeTargetMemberAttributes = MulticastAttributes.CompilerGenerated)]

The problem is that all methods in the assembly AspectTest are being logged, but the ones in WinFormsProject not. I have no idea why.

Some things to consider:

  • Every assembly has a reference to postsharp. So every aspect is being correctly recognized by the compiler.
  • If I set the ConsoleProject as the Main project, it works correctly. The problems comes only with WinFormsProject.
  • WinFormsProject references AspectTests (of course!).
  • ConsoleProject references AspectTests (of course!).
  • There is no dependency between WinFormsProject and ConsoleProject.

Any help would be great, and if you still need some info about this, please ask me (I might forget to tell something).

Thanks!

2
Please verify whether PostSharp runs when you rebuild the project, the similar line should be in your build output: : message : PostSharp 4.1 [4.1.21.0, postsharp.srv.4.0-x86.exe, CLR 4.0.30319.379893, Release] complete -- 0 errors, 0 warnings, processed in 1013 ms. Also the *.csproj file should have this line: <Import Project="..\packages\PostSharp.4.1.21\tools\PostSharp.targets" ... - AlexD
PostSharp integrates into the build process when you install the NuGet package. If PostSharp doesn't run during build, then you can try to reinstall the package. - AlexD

2 Answers

1
votes

To introduce the aspects into your code, PostSharp needs to execute during build time after the main compilation step. This means that adding a reference to PostSharp.dll in your project is not enough - the build sequence of the project needs to be modified as well.

PostSharp automatically integrates into the build process when you install the NuGet package. If PostSharp doesn't run during build, then you can try to reinstall the package.

0
votes

Well, I am a bit ashamed of the reason, but it might pass to anyone, so here's the problem and how I solved it.

The problem was: as I added the postsharp.dll as a reference manually and not with NuGet (because I had no internet on that moment) the references were ok, all compiled as expected, but as said, aspects didn't worked on the Winforms project. Maybe I missed to do something else.

The solution, therefore was so easy as to add postsharp through Nuget. Just that.

Now everything works. If someone has a better idea of the description of the problem, it would be good to know it. thanks, AlexD.