我使用一个简单的
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包添加到项目中。我还尝试手动向此项目的特定方法添加带有属性的方面,但该方面未触发。
编辑2:这是我用来测试的方法:
[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)
{
//[...]
}
}
}