如何将 postsharp 方面应用于网站项目?

How can I apply postsharp aspect to a website project?

我使用一个简单的 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");
}

还有我的后锐方面:

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 is (currently) integrated via MSBuild (as also mentioned by John Saunders), which is not used by website projects.

While in it's core PostSharp is a command-line tool, it gets so much information from MSBuild that it's quite hard to make it work separately (and neither advised nor documented nor supported in the first place). Daniel Balas

对于 4.3 之前的版本,我没有找到关于该主题的任何更新。对于 4.3 及更早版本,根据先前的回答和 this documentation (p. 45) 网站项目不受支持。

编辑:在询问先前引用的作者关于他们回答的当前有效性时,他们在评论中回复 :

@Amon For project-less websites (which IIRC are not creatable from the UI in VS 2017 and later) it is still true. However, all web projects that have csproj/vbproj (.NET or .NET Core) are working as expected (this also didn't change since then). – Daniel Balas