Open sandboxFocusImprove this doc

PS0167: Attribute with a Namespace in a Configuration File

Severity Error
Reported by PostSharp
Message The attribute '<Attribute>' of the element '<Element>' cannot have the namespace '<XmlNamespace>'. Remove the namespace prefix of the attribute.

Cause

In some sections of a PostSharp configuration file, such as the Multicast section and the logging profiles, each XML element describes a CLR object, and each XML attribute of the element sets a field or a property of this object. These attributes must have no namespace: only the element has the prefix of its clr-namespace.

PostSharp reports PS0167 when an attribute has a namespace prefix, and ignores the element. Namespace declarations (xmlns) and the Condition attribute are not concerned, and an attribute in the PostSharp namespace is reported as PS0206. <Element> is the expanded name of the element, which starts with its XML namespace in braces.

In the following example, the Category attribute has the prefix of the element.

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.postsharp.org/1.0/configuration">
  <Multicast xmlns:app="clr-namespace:Errors.PS0167_Error;assembly:PS0167_Error">
    <!-- The Category attribute has the namespace prefix of the element. -->
    <app:LogAttribute AttributeTargetTypes="Errors.PS0167_Error.OrderService" AttributeTargetMembers="PlaceOrder" app:Category="Orders" />
  </Multicast>
</Project>
using System;
using PostSharp.Aspects;
using PostSharp.Serialization;

namespace Errors.PS0167_Error;

[PSerializable]
public class LogAttribute : OnMethodBoundaryAspect
{
    public string Category { get; set; } = "General";

    public override void OnEntry( MethodExecutionArgs args )
    {
        Console.WriteLine( $"[{this.Category}] Entering {args.Method.Name}." );
    }
}

public class OrderService
{
    // The Multicast element of postsharp.config applies [Log] to this method.
    public void PlaceOrder( string product )
    {
        Console.WriteLine( $"Order placed for {product}." );
    }
}

public static class Program
{
    public static void Main()
    {
        new OrderService().PlaceOrder( "Coffee" );
    }
}
error PS0167: postsharp.config(5,115): The attribute 'Category' of the element '{clr-namespace:Errors.PS0167_Error;assembly:PS0167_Error}LogAttribute' cannot have the namespace 'clr-namespace:Errors.PS0167_Error;assembly:PS0167_Error'. Remove the namespace prefix of the attribute.

Resolution

Remove the namespace prefix of the attribute: write Category="Orders", not app:Category="Orders".

In the following example, the attribute has no prefix, and the aspect receives its value.

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.postsharp.org/1.0/configuration">
  <Multicast xmlns:app="clr-namespace:Errors.PS0167_Fixed;assembly:PS0167_Fixed">
    <!-- The Category attribute has no namespace prefix. -->
    <app:LogAttribute AttributeTargetTypes="Errors.PS0167_Fixed.OrderService" AttributeTargetMembers="PlaceOrder" Category="Orders" />
  </Multicast>
</Project>
using System;
using PostSharp.Aspects;
using PostSharp.Serialization;

namespace Errors.PS0167_Fixed;

[PSerializable]
public class LogAttribute : OnMethodBoundaryAspect
{
    public string Category { get; set; } = "General";

    public override void OnEntry( MethodExecutionArgs args )
    {
        Console.WriteLine( $"[{this.Category}] Entering {args.Method.Name}." );
    }
}

public class OrderService
{
    // The Multicast element of postsharp.config applies [Log] to this method.
    public void PlaceOrder( string product )
    {
        Console.WriteLine( $"Order placed for {product}." );
    }
}

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

See Also

Other Resources

Including CLR Objects in Configuration
Adding Aspects Using XML