Open sandboxFocusImprove this doc

PS0075: Compile Task Signs the Assembly Without a Private Key

Severity Error
Reported by PostSharp
Message Compile task: one of the properties 'PrivateKeyLocation' or 'PrivateKeyContainer' should be set when the property 'SignAssembly' is true.

Cause

The last step of the PostSharp compiler is the Compile task, which writes the woven assembly. PostSharp configures this task itself. A configuration file, such as postsharp.config or a .psproj file, can replace this configuration with a Compile element in a Tasks section, a practice of old PostSharp projects.

When the SignAssembly attribute of this element is true, PostSharp must sign the woven assembly, so it needs a private key: a key file in the PrivateKeyLocation attribute, or the name of a key container in the PrivateKeyContainer attribute. PostSharp reports PS0075 when both are missing or empty.

PostSharp then cannot set up the project and also reports PS0084.

In the following example, the postsharp.config file next to the program sets SignAssembly without a private key.

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.postsharp.org/1.0/configuration">
  <Tasks>
    <!-- SignAssembly is true, but no private key is given. -->
    <Compile TargetFile="{$Output}"
             IntermediateDirectory="{$IntermediateDirectory}"
             SignAssembly="True" />
  </Tasks>
</Project>
using System;
using PostSharp.Aspects;
using PostSharp.Serialization;

namespace Errors.PS0075_Error;

[PSerializable]
public class LogAttribute : OnMethodBoundaryAspect
{
    public override void OnEntry( MethodExecutionArgs args )
    {
        Console.WriteLine( $"Entering {args.Method.Name}." );
    }
}

public class OrderService
{
    [Log]
    public void PlaceOrder( string product )
    {
        Console.WriteLine( $"Order placed for {product}." );
    }
}

public static class Program
{
    public static void Main()
    {
        new OrderService().PlaceOrder( "Coffee" );
    }
}
error PS0075: Compile task: one of the properties 'PrivateKeyLocation' or 'PrivateKeyContainer' should be set when the property 'SignAssembly' is true.
error PS0084: The project could not be initialized because of the previous errors.

Resolution

  • Remove the Compile element, and the Tasks section, from the configuration file. PostSharp then configures the Compile task itself and signs the woven assembly when the project is signed, with the SignAssembly and AssemblyOriginatorKeyFile MSBuild properties.
  • When you keep the element, remove the SignAssembly attribute, or give the private key in the PrivateKeyLocation or PrivateKeyContainer attribute.

In the following example, the Compile element no longer sets SignAssembly.

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.postsharp.org/1.0/configuration">
  <Tasks>
    <!-- SignAssembly is no longer set without a private key. -->
    <Compile TargetFile="{$Output}"
             IntermediateDirectory="{$IntermediateDirectory}" />
  </Tasks>
</Project>
using System;
using PostSharp.Aspects;
using PostSharp.Serialization;

namespace Errors.PS0075_Fixed;

[PSerializable]
public class LogAttribute : OnMethodBoundaryAspect
{
    public override void OnEntry( MethodExecutionArgs args )
    {
        Console.WriteLine( $"Entering {args.Method.Name}." );
    }
}

public class OrderService
{
    [Log]
    public void PlaceOrder( string product )
    {
        Console.WriteLine( $"Order placed for {product}." );
    }
}

public static class Program
{
    public static void Main()
    {
        new OrderService().PlaceOrder( "Coffee" );
    }
}
Entering PlaceOrder.
Order placed for Coffee.

See Also

Other Resources

Configuring Projects Using postsharp.config
Configuration File Schema Reference
Well-Known MSBuild Properties