| Severity | Error |
| Reported by | PostSharp |
| Message | An unexpected exception occurred when executing user code: <Exception> |
Cause
PostSharp runs part of your code at build time: it constructs the aspects, and calls their build-time methods such as CompileTimeValidate, CompileTimeInitialize and ProvideAspects, the advice providers, the pointcut methods, and the ValidateCode methods of architecture constraints. When one of these throws an exception, PostSharp reports PS0125, a fatal error that stops the build.
<Exception> is the whole exception, with its type, its message and its stack trace: the first frames of the stack trace name your method, and the last ones are in PostSharp. The message is reported on the aspect or constraint class whose code threw the exception, or on the attribute.
A common case is an aspect that validates its target by throwing an exception, for example a caching aspect applied to a class that throws InvalidOperationException in CompileTimeInitialize when it meets a method that returns void.
The message contains the stack trace of the exception, whose frames in PostSharp change from one build of PostSharp to another, so no example can show it.
Resolution
- Read the first lines of
<Exception>to find which method of yours threw the exception, and why. To step through the build-time code, attach a debugger to the build as Debugging Build-Time Logic describes. - When the exception reports that the aspect cannot be applied to a target, report an error instead: in
CompileTimeValidate, write a message with Write and returnfalse, as Validating Aspect Usage describes. The build then fails with your message, on the target, without a stack trace. - When the aspect must simply skip some targets, return
falsefromCompileTimeValidatewithout writing a message, or restrict the targets with the multicasting properties of the aspect, such asAttributeTargetMemberAttributes. - When the exception comes from a dependency that the build-time code needs, such as a configuration file or an assembly that is not available at build time, move this work to the run time, for example to
RuntimeInitialize.
See Also
Reference
Other Resources
Validating Aspect Usage
Emitting Errors, Warnings, and Messages
Debugging Build-Time Logic