1

On any project using the new VS2017 csproj format (.NET Core, Standard, or Framework), unless PostSharp is a direct NuGet dependency, aspects are not weaved into the code.

For example, if I create a .NET Core project that depends on PubComp.Caching.AopCaching, which has a dependency on PostSharp, it will allow me to use aspects that are defined in the AopCaching NuGet. The project will compile, but the PostSharp aspects are not actually weaved into the code. The only way to make it work is to install PostSharp as a direct NuGet dependency.

I assume it has something to do with the order in which msbuild resolves NuGet dependencies for the new csproj and when the IL code weaving of PostSharp takes place.

Does anyone know exactly why it happens? Is there any solution other than remembering to always install PostSharp as well?

doug144
  • 387
  • 3
  • 9

2 Answers2

2

The package in question uses the following reference to PostSharp in its .nuspec file:

<dependency id="PostSharp" version="6.0.28" exclude="Build,Analyzers" />

This specifically excludes PostSharp's build scripts, which are not injected into the build and thus PostSharp is not executed. This does not happen in non-SDK projects.

This is likely because the authors did not want their package to cause dependent packages to all execute PostSharp.

FYI; If you look at PostSharp's package structure, it has two packages for each library that contains aspects, e.g. PostSharp.Patterns.Common (build-time package), which references PostSharp.Patterns.Common.Redist (runtime package). The build time package contains build-time-only components and has a reference to PostSharp package which contains all build-time tooling. The runtime package contains the runtime DLL and references PostSharp.Redist package.

This allows dependent packages to choose whether they want the build time or runtime dependency graph.

Giacomo Pirinoli
  • 548
  • 6
  • 17
Daniel Balas
  • 1,805
  • 1
  • 15
  • 20
  • That sounds reasonable, but why did it work whenever I installed it on a .NET framework project with the old csproj format? – doug144 Sep 18 '19 at 08:01
  • @doug144 IIRC NuGet does not support these detailed dependency specifications in `packages.config` format, so it installs all dependencies fully. – Daniel Balas Sep 19 '19 at 09:51
0

based on this great answer by @Daniel Balas, i took it one step further and was able to get PostSharp to pre-compile even when imported on a single base project.

all i had to do was add build to the included assets when importing the PostSharp package in my csproj file

<PackageReference Include="PostSharp" Version="6.10.13">
    <IncludeAssets>build</IncludeAssets>
    <ExcludeAssets>none</ExcludeAssets>
    <PrivateAssets>analyzers</PrivateAssets>
</PackageReference>

so, when importing the PubComp.Caching.AopCaching package, import once the PostSharp package as well and include the build assets like above.

this should make explicit import of PostSharp redundant in referencing projects.