PostSharp 5.0 preview with .NET Core

This article describes how to create, compile and run a .NET Core Project with PostSharp 5.0.12-preview. The same would work with a newer version. Just replace the version number in all places.

NuGet packages for .NET Core:

All PostSharp .NuGet packages except PostSharp.Patterns.Caching(.Redist) are ready to be used with .NET Core.

All packages have been split into tools and libraries (redistributables), so each former package has now its redistributable sibling with a suffix ".Redist" in its name. In projects for .NET Core and .NET Standard, it is possible to distinguish such references. To take advantage of it, place the .Redist packages into the section dependencies and the tooling into the section tools in project.json.

For the Diagnostics Pattern library, see http://support.sharpcrafters.com/kb/postsharp-50-preview/postsharp-...

1) Prepare The .NET Core Environment

See https://www.microsoft.com/net/download/core.

2) Create and set up a project

a) .NET Core CLI

1) Initialize the project

1) Create a .NET Core Console project:

mkdir HelloPostSharp
cd HelloPostSharp
dotnet new

2) In project.json, modify the sections buildOptions, dependencies and tools as shown below:

{
  "version": "1.0.0-*",
  "buildOptions": {
    "compilerName": "postsharp", // Introduce PostSharp into the build chain.
    "debugType": "full", // Portable PDB is not supported yet.
    "emitEntryPoint": true
  },
  "dependencies": {
    "PostSharp.Redist": "5.0.12-preview" // Run-time dependency for PostSharp.dll.
  },
  "tools": {
    "PostSharp": "5.0.12-preview" // Build-time dependency for PostSharp tooling.
  },
  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.1"
        }
      },
      "imports": "dnxcore50"
    }
  }
}

3) Add some code using PostSharp to the file Program.cs, e.g.:

using System;
using PostSharp.Aspects;
using PostSharp.Serialization;

namespace NetCoreCliConsoleAppWithPostSharp
{
    public class Program
    {
        [PSerializable]
        public sealed class SayHelloAttribute : OnMethodBoundaryAspect
        {
            public override void OnExit( MethodExecutionArgs args )
            {
                Console.WriteLine( "Hello {0}!", args.Method.Name );
            }
        }

        [SayHello]
        public static void Main(string[] args)
        {
            Console.WriteLine( "Hello PostSharp!" );
        }
    }
}

2) Run The Project

1) Restore packages from the local repository:

dotnet restore

2) Build and run:

dotnet run

NOTICE: If you don’t have a PostSharp license installed on your machine, the first build will not run correctly and you’ll be prompted to register the license on your machine.

b) Portable Class Library v5 (.NET Standard Class Library with .csproj/project.json)

1) Initialize the project in Visual Studio

1) Create a new C# project of type "Class Library (Portable)".

2) Select .NET Framework 4.6 and ASP.NET Core 1.0.

3) Switch to .NET Standard (Project Properties > Library > Targeting > Target .NET Platform Standard).

4) In project.json, modify the sections dependencies and tools as shown below:

{
  "dependencies": {
    "NETStandard.Library": "1.6.1",
    "PostSharp.Redist": "5.0.12-preview" // Run-time dependency for PostSharp.dll.
  },
  "tools": {
    "PostSharp": "5.0.12-preview" // Build-time dependency for PostSharp tooling.
  },
  "frameworks": {
    "netstandard1.3": {}
  }
}

5) In case the NuGet package PostSharp-5.0.12-preview is not in your NuGet packages cache,

call dotnet restore in the project folder.

6) Edit the .csproj file

...
<!-- PostSharp.targets needs to be imported after Microsoft.Portable.CSharp.targets -->
  <Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
  <Import Project="$(USERPROFILE)\.nuget\packages\PostSharp\5.0.12-preview\tools\PostSharp.targets" />
...

NOTICE: If you miss the step 5 (dotnet restore) and try to load this modified project in Visual Studio, you might be asked to install Visual Studio Update 1, which is an incorrect suggestion.

7) Add some code using PostSharp to the file Class1.cs, e.g.:

using System;
using PostSharp.Aspects;
using PostSharp.Serialization;

namespace NetStandardLibraryWithPostSharp
{
    public class Class1
    {
        [PSerializable]
        public sealed class SayHelloAttribute : OnMethodBoundaryAspect
        {
            public override void OnExit( MethodExecutionArgs args )
            {
                Console.WriteLine( "Hello {0}!", args.Method.Name );
            }
        }

        [SayHello]
        public static void Main( string[] args )
        {
            Console.WriteLine( "Hello PostSharp!" );
        }
    }
}

2) Build the project

The project will now build as usual using the MSBuild build chain.

NOTICE: If you don’t have a PostSharp license installed on your machine, the first build will not run correctly and you’ll be prompted to register the license on your machine.