PostSharp在Visual Studio 2015上无法工作

3
我有一个Visual Studio 2015的解决方案,其中我复制并粘贴了一些PostSharp验证属性,这些属性之前在Visual Studio 2013项目中使用成功。该项目编译和运行都成功。然而,用于测试我的验证属性的单元测试失败了。通过调试,我发现我的所有属性都没有被执行。我已确保在项目引用以及.xproj文件中正确引用了PostSharp包。我的验证属性代码如下:
using System;
using PostSharp.Aspects;
using PostSharp.Reflection;

namespace Foo.Domain.Model.ValidationAttributes
{
/// <summary>
/// Validates the parameter to strictly not be null (empty and whitespace is valid). Throws a System.ArgumentNullException if assigned a null value.
/// </summary>
[Serializable]
public sealed class NotNullAttribute : LocationInterceptionAspect, ILocationValidationAspect<object>
{
    public NotNullAttribute()
    {
    }

    public override void OnSetValue(LocationInterceptionArgs args)
    {
        Exception ex = ValidateValue(args.Value, args.LocationName, args.Location.LocationKind);

        if (ex != null)
        {
            throw ex;
        }

        args.ProceedSetValue();
    }

    public Exception ValidateValue(object value, string locationName, LocationKind locationKind)
    {
        return value == null ? new ArgumentNullException(locationName, string.Format("The parameter '{0}' may not be null.", locationName)) : null;
    }
}
}

我的project.json显示了添加的依赖项:

{
    "version": "1.0.0-*",
    "description": "MyProject Domain Class Library",
    "tags": [ "MyProject" ],
    "projectUrl": "",
    "licenseUrl": "",

    "dependencies": {
        "PostSharp": "4.1.21",
        "PostSharp.Patterns.Common": "4.1.21",
        "PostSharp.Patterns.Model": "4.1.21"
    },

    "frameworks": {
        "net451": { }
    }
}

我的.xproj文件显示了添加的PostSharp目标(我已检查它存在于路径中):

<Import Project="..\packages\PostSharp\4.1.21\tools\PostSharp.targets" />

从PostSharp文档中可以看出,VS2015得到了充分支持,但我似乎无法使这些方面工作。需要做些什么才能在Visual Studio 2015上运行PostSharp?

1个回答

2

目前,PostSharp不支持ASP.NET 5引入的新项目构建系统(这在兼容性页面中有提到)。当编译在内存中运行时,通常的MSBuild扩展点不会被使用,也不会调用PostSharp。您可以尝试在VS中完全构建解决方案,看看是否以此方式应用了方面。

PostSharp团队计划在即将推出的版本中提供对新ASP.NET构建系统的支持。

更新

在GitHub上发布了PostSharp的ASP.NET 5连接器预览版(https://github.com/postsharp/PostSharp.Dnx)。这将是一个“正在进行中”的工作,直到ASP.NET 5 RTM发布。


谢谢AlexD,这真是个糟糕的消息。你所说的“在VS中完全构建解决方案”是什么意思?你知道支持它的即将推出的版本有没有时间表吗? - 08Dc91wk
“完全构建”是指将项目构建并将二进制文件写入磁盘,但这似乎与此无关。无论如何,我们已经在GitHub上发布了ASP.NET 5连接器的PostSharp预览版(https://github.com/postsharp/PostSharp.Dnx)。 - AlexD

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