我使用一个简单的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
。除我的网站项目外,解决方案中的每个项目均已正确应用该方面。我刚刚进入开发团队,对解决方案并不熟悉。
我所知道的是,我想将方面应用于该项目中的类,并且后期处理似乎忽略了该特定项目。配置文件位于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");
}
和我的后期处理方面:
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 Technologies开发人员的旧答案:
(目前)PostSharp是通过MSBuild集成的(John Saunders也提到过),而网站项目并未使用。
虽然PostSharp是其核心工具,但它是一个命令行工具,但它从MSBuild获得了太多信息,因此很难使其单独工作(并且一开始既未提供建议,也未提供文档或支持)。丹尼尔·巴拉斯(Daniel Balas)
对于4.3之前的版本,我没有找到关于该主题的任何更新。对于4.3和更早版本,根据先前的答案和此文档(第45页),不支持网站项目。
编辑:质疑关于当前有效性先前引述笔者时,他们回答他们回答这个在评论:
@Amon对于无项目网站(无法在VS 2017及更高版本中从UI创建IIRC),这仍然适用。但是,所有具有csproj / vbproj(.NET或.NET Core)的Web项目都按预期工作(此后此后也没有发生变化)。–丹尼尔·巴拉斯(Daniel Balas)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句