| Severity | Error |
| Reported by | PostSharp |
| Message | Cannot create an instance of the service type '<TypeName>, <AssemblyFile>': <Reason> |
Cause
The Service element of a PostSharp configuration file, such as postsharp.config, registers a build-time service. When it initializes the project, PostSharp loads the assembly that the AssemblyFile attribute names or, when the attribute is omitted, the assembly being built, finds the class that TypeName names, and creates an instance of it. PostSharp reports PS0157 when any of these steps throws an exception. <Reason> is the message of the exception, and <AssemblyFile> is empty when the element has no AssemblyFile attribute.
The common causes are:
TypeNameis not the full name of a class of the assembly, for example because it lacks the namespace. The reason is thenValue cannot be null. (Parameter 'type').- The file of
AssemblyFilecannot be found or loaded. - The class has no parameterless constructor, or its constructor throws an exception.
Because the services are created when the project is initialized, PostSharp also reports PS0084 and stops the build.
In the following example, TypeName gives the name of the class without its namespace.
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.postsharp.org/1.0/configuration">
<!-- The type name lacks the namespace, so PostSharp cannot find the type. -->
<Service TypeName="OrderAuditService" />
</Project>
using System;
using PostSharp.Aspects;
using PostSharp.Extensibility;
using PostSharp.Serialization;
namespace Errors.PS0157_Error;
// A build-time service that postsharp.config registers.
public class OrderAuditService : IService
{
}
[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 PS0157: postsharp.config(4,4): Cannot create an instance of the service type 'OrderAuditService, ': Value cannot be null. (Parameter 'type').
Resolution
- Set
TypeNameto the full name of the class, with its namespace. For a nested class, separate it from its declaring class with+. - When the class is in another assembly, set
AssemblyFileto the path of this assembly. A relative path is resolved from the directory of the configuration file. - Give the class a public parameterless constructor that does not throw.
- When the service is not needed, remove the
Serviceelement.
In the following example, TypeName is the full name of the class, and the project builds.
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.postsharp.org/1.0/configuration">
<!-- The type name is the full name of the service class. -->
<Service TypeName="Errors.PS0157_Fixed.OrderAuditService" />
</Project>
using System;
using PostSharp.Aspects;
using PostSharp.Extensibility;
using PostSharp.Serialization;
namespace Errors.PS0157_Fixed;
// A build-time service that postsharp.config registers.
public class OrderAuditService : IService
{
}
[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
Reference
Other Resources
Configuration File Schema Reference
Configuring Projects Using postsharp.config