Open sandboxFocusImprove this doc

PS0158: Required Attribute Missing in a Configuration Element

Severity Error
Reported by PostSharp
Message The <Element> element does not have a <Attribute> attribute.

Cause

Two elements of a PostSharp configuration file, such as postsharp.config, require an attribute:

  • A Property element requires a Name attribute.
  • A Service element requires a TypeName attribute. PostSharp checks it after it evaluates the expressions of the attribute, so an expression that evaluates to an empty string is also reported. For a Service element, the message gives the configuration file without a line number.

PostSharp reports PS0158 when the attribute is missing or empty. Attribute names are case-sensitive, so name is not the Name attribute. PostSharp then reports PS0084 and stops the build, because it cannot initialize the project.

In the following example, the Property element has a name attribute instead of Name.

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.postsharp.org/1.0/configuration">
  <!-- XML is case-sensitive: "name" is not the Name attribute. -->
  <Property name="OrderCategory" Value="Orders" />
</Project>
using System;
using PostSharp.Aspects;
using PostSharp.Serialization;

namespace Errors.PS0158_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 PS0084: The project could not be initialized because of the previous errors.
error PS0158: postsharp.config(4,4): The Property element does not have a Name attribute.

Resolution

  • Add the Name attribute to the Property element, or the TypeName attribute to the Service element, with the exact case.
  • When a TypeName attribute is an expression, check that the properties it uses are defined.

In the following example, the Property element has a Name attribute, and the project builds.

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.postsharp.org/1.0/configuration">
  <!-- The property has a Name attribute. -->
  <Property Name="OrderCategory" Value="Orders" />
</Project>
using System;
using PostSharp.Aspects;
using PostSharp.Serialization;

namespace Errors.PS0158_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

Configuration File Schema Reference
Configuring Projects Using postsharp.config