Open sandboxFocusImprove this doc

PS0279: Assembly Cannot Be Loaded into the PostSharp Process

Severity Fatal
Reported by PostSharp
Message Cannot load the assembly "<Assembly>" into the PostSharp process. The file may be a reference assembly, a native library or a corrupted file, or it may target another processor architecture than the PostSharp process. Enable the AssemblyBinder trace category with the PostSharpTrace MSBuild property for details.

Cause

PostSharp does not only read the assemblies that the project references: it also loads them for execution, because it runs the build-time code of aspects, such as their CompileTimeValidate and CompileTimeInitialize methods. PostSharp locates the file of each assembly that it must execute, then loads it into its own process.

PostSharp reports PS0279 when the runtime refuses to load the file with a BadImageFormatException. <Assembly> is the name of the assembly. The usual causes are:

  • The file is a reference assembly, which contains no code and cannot be executed, for example because a reference points to the ref folder of a NuGet package instead of its lib folder.
  • The file is not a .NET assembly, but a native library with the same name.
  • The assembly is built for a single processor architecture, for example x86, which differs from the architecture of the PostSharp process.
  • The file is truncated or corrupted, for example by an interrupted copy.

The error is fatal: PostSharp stops processing the project.

This message depends on the files of the build and on the PostSharp process, so no example can show it.

Resolution

  1. Find the file that PostSharp tried to load. Build with the AssemblyBinder trace category, without the pipe server so that the trace reaches the build log, and search the log for the name of the assembly:

    dotnet build -p:PostSharpTrace=AssemblyBinder -p:PostSharpUsePipeServer=False --no-incremental -v:detailed > build.log
    
  2. Depending on what the file is:

    • For a reference assembly, reference the implementation assembly instead. Check the HintPath of the reference, or the package that provides it.
    • For a native library, make sure that it does not shadow the .NET assembly of the same name in the output directory or in the search path of PostSharp.
    • For an assembly built for one processor architecture, build it for AnyCPU, or make the PostSharp process use the same architecture. When the project targets .NET Framework, the PostSharpTargetProcessor MSBuild property selects the architecture of the PostSharp process (see Well-Known MSBuild Properties).
    • For a corrupted file, delete the bin and obj directories and the affected package from the NuGet cache, then restore and rebuild.

See Also

Other Resources

Well-Known MSBuild Properties
Resolution of Assembly Binding Redirections
Debugging Build-Time Logic