我该如何将Postsharp方面应用于网站项目?

4
我使用一个简单的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)
        {
            //[...]
        }
    }
}

您在网站项目中安装了PostSharp NuGet包吗?您是否在该项目的构建日志中看到来自PostSharp的任何消息?您可以尝试将您的方面作为属性添加到源代码中,看看是否有效? - AlexD
@AlexD 即使手动添加 aspect 属性也无法触发该 aspect。Visual Studio 没有显示它被应用,调试器中也没有到达该 aspect。 构建日志非常大,因此很难确定是否存在错误,但我认为没有。是的,PostSharp 已安装在项目中。 - Amon
你的目标 .Net 版本是什么?这是哪种项目类型? - Peter Bons
1个回答

3
根据PostSharp Technologies的一位开发者旧答案所述:

目前,PostSharp是通过MSBuild集成的(正如John Saunders所提到的那样),但网站项目不使用它。

虽然在其核心中,PostSharp是一个命令行工具,但它从MSBuild获得了如此多的信息,以至于很难使其独立工作(也不建议、未记录、也没有支持)。 Daniel Balas

我没有找到任何关于早期版本的更新。根据先前的答案和这份文档(第45页),4.3及更早版本不支持网站项目。 编辑:当询问先前引用作者关于他们回答的当前有效性时,他们在评论中回复了这个
对于没有项目的网站(我记得在VS 2017及以后的版本中无法从UI中创建),这仍然是正确的。但是,所有具有csproj/vbproj(.NET或.NET Core)的Web项目都按预期工作(自那时起也没有改变)。 - Daniel Balas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接