08Dc91wk
08Dc91wk

Reputation: 4318

PostSharp not working on Visual Studio 2015

I have a Visual Studio 2015 solution, into which I have copied-and-pasted a number of PostSharp validation attributes that I had been successfully using on a Visual Studio 2013 project.

The project all compiles and runs successfully. Unfortunately though, unit tests designed to test my validation attributes are failing. From debugging I have found that none of my attributes are ever being run. I have ensured that the PostSharp package is referenced correctly in the project references as well as in the .xproj file.

My Validation Attribute code is like:

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;
    }
}
}

My project.json shows the added dependencies:

{
    "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": { }
    }
}

And my .xproj file shows the added PostSharp target (I've checked it exists at the path):

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

From the PostSharp documentation it appears that VS2015 is fully supported, but I can't seem to get these aspects to work. Is there anything I need to do to get PostSharp up and running on Visual Studio 2015?

Upvotes: 3

Views: 2159

Answers (1)

AlexD
AlexD

Reputation: 5101

PostSharp does not currently support the new project build system that has been introduced with ASP.NET 5 (this is mentioned on the Compatibility page). When compilation runs "in-memory", then the usual MSBuild extension points are not used and PostSharp is not invoked. You can try to completely build your solution in VS and see if the aspects are applied this way.

PostSharp team plans to provide support for the new ASP.NET build system in one of the upcoming versions.

UPDATE

A preview of ASP.NET 5 connector for PostSharp has been release on GitHub (https://github.com/postsharp/PostSharp.Dnx). This is going to be a "work-in-progress" until ASP.NET 5 RTM is released.

Upvotes: 2

Related Questions