Open sandboxFocusImprove this doc

PS0003: Configuration File Cannot Be Read

Severity Error
Reported by PostSharp
Message Invalid configuration file: <Reason>

Cause

PostSharp reads every postsharp.config file from the directory of the project up to the root of the drive, and every configuration file that a Using element imports. See Configuring Projects Using postsharp.config. PostSharp reports PS0003, and stops building the project, when it cannot read one of these files:

  • The file is not well-formed XML: an element is not closed, an attribute value has no quotes, or a character is not escaped. <Reason> is the message of the XML parser, with the line and the position of the problem.
  • The root element of the file is not a Project element in the namespace http://schemas.postsharp.org/1.0/configuration. <Reason> gives the name and the namespace of the root element that PostSharp found. When the root element has no namespace, the namespace is empty in the message.

The location of the message is the configuration file, not a line of your code.

In the following example, the Project element of postsharp.config has no xmlns attribute.

<?xml version="1.0" encoding="utf-8"?>
<!-- The Project element has no namespace, so PostSharp does not recognize it. -->
<Project>
  <Property Name="LoggingEnabled" Value="True" />
</Project>
using System;
using PostSharp.Aspects;
using PostSharp.Serialization;

namespace Errors.PS0003_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 PS0003: Invalid configuration file: Unexpected root node: Project in namespace .

Resolution

  • Make sure that the root element of the file is <Project xmlns="http://schemas.postsharp.org/1.0/configuration">. See Configuration File Schema Reference for the elements that it can contain.
  • When the message reports an XML error, open the file at the line and the position that the message gives, and correct the XML. An XML editor, such as the one of Visual Studio, shows the error as you type.
  • When the file is not a PostSharp configuration file, rename it or move it: PostSharp reads every file named postsharp.config in the directory of the project and in its parent directories.

In the following example, the Project element is in the namespace of the PostSharp configuration files, and the project builds.

<?xml version="1.0" encoding="utf-8"?>
<!-- The Project element is in the namespace of the PostSharp configuration files. -->
<Project xmlns="http://schemas.postsharp.org/1.0/configuration">
  <Property Name="LoggingEnabled" Value="True" />
</Project>
using System;
using PostSharp.Aspects;
using PostSharp.Serialization;

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

Configuring Projects Using postsharp.config
Configuration File Schema Reference