.netmsbuild.net-4.8postsharp

NET Multitarget assembly error: assembly has not been processed by PostSharp


I have a multitarget assembly Mully (only one net48 target for now) with MSBuild items

<Import Project="..\..\..\..\_Packages\PostSharp.2024.0.5\build\PostSharp.props" />
<!-- ... -->
<TargetFrameworks>net48</TargetFrameworks>
<!-- ... -->
<Reference Include="PostSharp, Version=2024.0.5.0, Culture=neutral, PublicKeyToken=b13fd38b8f9c99d7, processorArchitecture=MSIL">
   <HintPath>..\..\..\..\_Packages\PostSharp.Redist.2024.0.5\lib\net45\PostSharp.dll</HintPath>
</Reference>
<!-- ... -->
<UsePostSharp>true</UsePostSharp>
<!-- ... -->
<Import Project="..\..\..\..\_Packages\PostSharp.2024.0.5\build\PostSharp.targets" />

and with a PostSharp aspect

[PSerializable]
public class MyAspect : OnMethodBoundaryAspect

Which is project-referenced by a .NET Framework 4.8 project Franky.

Mully builds successfully, but Franky throws error

Error: The type MyNamespace.MyAspect has the [PSerializable] custom attribute but the assembly C:\obfuscated\outputdir\net48\Mully.dll has not been processed by PostSharp.

How do I solve this?

I have tried solution clean, deleting obj, bin, outputdir\, .vs, %appdata%\local\Microsoft\VisualStudio\xx\ComponentModelCache\ and restarting Visual Studio.


Solution

  • The only supported way of using PostSharp is through the NuGet package, especially in SDK-style project files, where it is not possible to use nuget.config.

    The legacy ZIP distribution is still provided but deprecated in the documentation. This, however, was intended for legacy-style project files and never worked with the newer SDK-style projects.

    The most likely reason for PostSharp not being executed is that you are importing PostSharp.targets before Sdk.targets. You should reverse the order. .targets from NuGet packages are imported after all system files.

    However, I suggest you rather use <PackageReference> to avoid getting into these issues.

    Regarding your remark about cleaning the VS component cache. All PostSharp build tooling is within the NuGet package and the VS extension itself provides UI support, but is not itself required nor does it do anything significant while building.