| Severity | Warning or Error |
| Reported by | PostSharp |
| Message | <Reason> |
Cause
Before it builds a project, PostSharp reads every license key that the project and the machine supply: the PostSharpLicense MSBuild property or environment variable, the License elements of the postsharp.config files, and the license keys registered on the machine. PostSharp reports PS0301 for each license key that it rejects, with the reason in the message. <Reason> is a complete sentence that names where the key was set, for example the path of a postsharp.config file or the PostSharpLicense property, rather than quoting the key, which is a secret on a build server.
PostSharp rejects a license key in the following cases:
- The key cannot be read: it is not a license key, it was truncated when it was copied, its signature does not verify, it is not valid yet, or it contains fields that this version of PostSharp does not understand.
- The key is limited to a namespace, and neither the name of the assembly nor the name of the Git repository of the project is in this namespace.
- The key licenses another family of products.
- The license string contains an instruction that PostSharp no longer accepts, or that is reserved for the builds of the source code of PostSharp, or an entry that names a license source without a key.
For a rejected license key, PS0301 is usually a warning: PostSharp ignores the key and builds the project with the other license keys, and reports PS0242 when none of them covers the project. For an entry of a license string that PostSharp cannot interpret, such as a retired instruction, PS0301 is an error.
In the following example, the license key in postsharp.config was truncated when it was pasted into the file.
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.postsharp.org/1.0/configuration">
<!-- The license key was truncated when it was pasted into the file. -->
<License Value="1234-ZQZQZQZQAQZQZQZQZQZQZQZQ" />
</Project>
using System;
using PostSharp.Aspects;
using PostSharp.Serialization;
namespace Errors.PS0301_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" );
}
}
PostSharp rejects the key and builds the project with the other license keys:
warning PS0301: The license key set in postsharp.config is invalid. Unable to read beyond the end of the stream.
Resolution
- When the key is malformed or truncated, copy the license key again, in full. See Deploying License Keys for the places where you can set it.
- When the key is limited to a namespace, make sure that the name of the assembly, or the name of the Git repository of the project as it appears in the URL of a remote, starts with the licensed namespace. See License Troubleshooting.
- When the key licenses another product, or was generated for a later version of PostSharp, use a key that licenses this version of PostSharp, or upgrade PostSharp.
- When the license string contains an instruction that is no longer supported, remove it.
- When the key is not needed, because another key licenses the project, remove it from the property, the file or the registration where the message says it is set.
To see which keys PostSharp considers and why it rejects them, capture the licensing trace with the PostSharpTrace MSBuild property, as License Troubleshooting describes.
In the following example, the truncated key is removed from postsharp.config, and the project builds without a warning.
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.postsharp.org/1.0/configuration">
<!-- The License element with the truncated key is removed: the build uses the other license keys of the project and of the machine. -->
</Project>
using System;
using PostSharp.Aspects;
using PostSharp.Serialization;
namespace Errors.PS0301_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
Deploying License Keys
License Troubleshooting
Configuration File Schema Reference
Well-Known MSBuild Properties