【发布时间】:2020-11-27 10:22:51
【问题描述】:
我使用一个简单的postsharp.config 文件:
<?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>
我的解决方案中的所有项目都在命名空间MyTopLevelNamespace 下。除了我的网站项目之外,解决方案中的每个项目都正确应用了方面。我刚加入开发团队,对解决方案不熟悉。
我所知道的是,我想将方面应用到这个项目中的类,而 postsharp 似乎忽略了那个特定的项目。配置文件位于src/ 文件夹中,应该应用于所有项目。
我已确保我应用方面的类型位于配置文件中指定的命名空间下,并且它不匹配任何排除模式。
我是否提供了足够的信息?我不确定这是因为该项目是一个网站项目,但我看不到其他任何内容。
编辑:我已确保将 nuget 包添加到项目中。我还尝试将具有属性的方面手动添加到该项目的特定方法中,并且方面没有触发。
Edit2:这是我用来测试的方法:
[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");
}
还有我的postsharp方面:
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)
{
//[...]
}
}
}
【问题讨论】:
-
网站项目中是否安装了 PostSharp NuGet 包?您是否在该项目的构建日志中看到来自 PostSharp 的任何消息?您能否尝试将您的方面作为属性添加到源代码中,看看是否有效?
-
@AlexD 即使手动添加 aspect 属性也不会触发 asspec。 Visual Studio 没有显示它已应用,并且在调试器中未达到方面。构建日志很大,所以如果有错误很难,但我认为没有。是的,postsharp 已安装在项目中。
-
您的目标是什么 .Net 版本?它是什么项目类型?