| Severity | Error |
| Reported by | PostSharp |
| Message | Expression "<Expression>" cannot evaluate to an empty value. |
Cause
Many attributes of a postsharp.config file accept expressions, such as {$ExtraPlugIn}, which PostSharp evaluates when it loads the file. See Using Expressions in Configuration Files. A property that is not defined evaluates to an empty string, without an error.
Some attributes cannot be empty, because PostSharp cannot do anything with an empty value. PostSharp reports PS0138 when one of them evaluates to an empty string, or to a string of white space:
- The
Fileattribute of aUsingelement, which names the plug-in or the configuration file to import. - The
TypeNameattribute of aServiceelement. - The PostSharp properties that give the output file of the build and the file that lists the installed frameworks. The MSBuild targets of PostSharp set them, so an empty value means that the build was customized.
<Expression> is the expression as written, before evaluation. The location of the message is the element of the configuration file. Because the configuration of the project has an error, PostSharp then stops with PS0084.
In the following example, postsharp.config imports a plug-in whose path is given by the ExtraPlugIn property, but the build does not set this property.
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.postsharp.org/1.0/configuration">
<!-- The ExtraPlugIn property is not set, so the File attribute evaluates to an empty string. -->
<Using File="{$ExtraPlugIn}" />
</Project>
using System;
using PostSharp.Aspects;
using PostSharp.Serialization;
namespace Errors.PS0138_Error;
// The postsharp.config file of this folder imports a plug-in from a property that is not set.
[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 PS0084: The project could not be initialized because of the previous errors.
error PS0138: postsharp.config(4,4): Expression "{$ExtraPlugIn}" cannot evaluate to an empty value.
Resolution
- Set the property that the expression refers to, in the
postsharp.configfile with aPropertyelement, or from MSBuild with thePostSharpPropertiesproperty. See Well-Known PostSharp Properties. - When the element is needed only when the property is set, add a
Conditionattribute to it, as inCondition="{$ExtraPlugIn != ''}". PostSharp evaluates the condition first and skips the element when it is false. - Check the spelling of the property name: a misspelled property evaluates to an empty string.
In the following example, the Using element is skipped when the ExtraPlugIn property is not set.
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.postsharp.org/1.0/configuration">
<!-- The plug-in is imported only when the ExtraPlugIn property is set. -->
<Using File="{$ExtraPlugIn}" Condition="{$ExtraPlugIn != ''}" />
</Project>
using System;
using PostSharp.Aspects;
using PostSharp.Serialization;
namespace Errors.PS0138_Fixed;
// The postsharp.config file of this folder imports the plug-in only when the property is set.
[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
Using Expressions in Configuration Files
Configuration File Schema Reference
Well-Known PostSharp Properties
Configuring Projects Using postsharp.config