| Severity | Error |
| Reported by | PostSharp |
| Message | Cannot parse the XML namespace '<XmlNamespace>': the expected format is 'clr-namespace:MyNamespace;assembly:MyAssembly'. |
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: an aspect, an attribute or a logging option. PostSharp finds the type of the object from the XML namespace of the element, which must have the form clr-namespace:MyNamespace;assembly:MyAssembly: pairs separated by a semicolon, each with a key and a value separated by a colon, with exactly the keys clr-namespace and assembly.
PostSharp reports PS0161 when <XmlNamespace> does not have this form, and ignores the element. The common causes are:
- The assembly is introduced with
assembly=, as in XAML, instead ofassembly:. - The
assemblypart is missing. - The element has no prefix and inherits the default namespace of the file,
http://schemas.postsharp.org/1.0/configuration.
In the following example, the Multicast section uses the XAML syntax.
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.postsharp.org/1.0/configuration">
<!-- The XML namespace separates the assembly name with "=", as in XAML, instead of ":". -->
<Multicast xmlns:app="clr-namespace:Errors.PS0161_Error;assembly=PS0161_Error">
<app:LogAttribute AttributeTargetTypes="Errors.PS0161_Error.OrderService" AttributeTargetMembers="PlaceOrder" Category="Orders" />
</Multicast>
</Project>
using System;
using PostSharp.Aspects;
using PostSharp.Serialization;
namespace Errors.PS0161_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 PS0161: postsharp.config(5,6): Cannot parse the XML namespace 'clr-namespace:Errors.PS0161_Error;assembly=PS0161_Error': the expected format is 'clr-namespace:MyNamespace;assembly:MyAssembly'.
Resolution
Write the XML namespace in the form clr-namespace:MyNamespace;assembly:MyAssembly, where MyNamespace is the namespace of the type and MyAssembly is the name of the assembly that declares it, without the .dll extension. Give each element that describes an object the prefix of this namespace.
In the following example, the XML namespace separates the assembly name with a colon, and the aspect is applied.
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.postsharp.org/1.0/configuration">
<!-- The XML namespace separates the assembly name with ":". -->
<Multicast xmlns:app="clr-namespace:Errors.PS0161_Fixed;assembly:PS0161_Fixed">
<app:LogAttribute AttributeTargetTypes="Errors.PS0161_Fixed.OrderService" AttributeTargetMembers="PlaceOrder" Category="Orders" />
</Multicast>
</Project>
using System;
using PostSharp.Aspects;
using PostSharp.Serialization;
namespace Errors.PS0161_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
Configuration File Schema Reference