【问题标题】:How can I apply postsharp aspect to a website project?如何将 postsharp 方面应用于网站项目?
【发布时间】: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 版本?它是什么项目类型?

标签: c# postsharp


【解决方案1】:

根据 PostSharp Technologies 开发人员的old answer

PostSharp(当前)通过 MSBuild 集成(也由 John Saunders),网站项目不使用它。

虽然 PostSharp 的核心是一个命令行工具,但它的功能非常多 来自 MSBuild 的信息表明很难使其工作 单独(并且既不建议也不记录或支持在 第一名)。丹尼尔·巴拉斯

我没有找到任何关于 4.3 之前版本的更新。对于 4.3 及更早版本,根据上一个答案和 this documentation(第 45 页)网站项目不受支持。

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

@Amon 用于无项目网站(IIRC 不能从 VS 2017 及更高版本中的 UI)仍然是正确的。但是,所有网络项目 具有 csproj/vbproj(.NET 或 .NET Core)的设备按预期工作 (从那时起这也没有改变)。 ——丹尼尔·巴拉斯

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-15
    • 1970-01-01
    • 2015-02-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多