Aspects targeting WinForm project's methods are not working

时间:2015-07-31 20:04:58

标签: c# .net winforms .net-assembly postsharp

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 个答案:

答案 0 :(得分:1)

要将这些方面引入代码,PostSharp需要在主编译步骤之后的构建期间执行。这意味着在项目中添加对PostSharp.dll的引用是不够的 - 项目的构建顺序也需要修改。

当您安装NuGet包时,PostSharp会自动集成到构建过程中。如果PostSharp在构建期间未运行,则可以尝试重新安装该软件包。

答案 1 :(得分:0)

嗯,我有点惭愧的原因,但它可能会传给任何人,所以这就是问题以及我是如何解决的。

问题是:因为我手动添加了postsharp.dll作为参考,而不是NuGet(因为那时我没有互联网)引用是好的,所有编译都按预期进行,但如上所述,方面没有&#39 ; t在Winforms项目上工作。也许我错过了别的什么。

因此,解决方案非常容易通过Nuget添加postharp。就是这样。

现在一切正常。如果有人对问题的描述有了更好的了解,那么了解它会很好。 谢谢,AlexD。