How to ensure return value is not null in PostSharp 5

dave.parson's Avatar


07 Nov, 2017 08:45 PM

While updating from PostSharp 4 to 5 I've run into a breaking change where [return: PostSharp.Patterns.Contracts.NotNullAttribute] is no longer supported on awaitable types. Is there another approach we can use for this situation?

Error LA0178: Advice "PostSharp.Patterns.Contracts.NotNullAttribute.ValidateValueDynamic(System.Object, System.String, PostSharp.Reflection.LocationKind, PostSharp.Aspects.LocationValidationContext)" cannot be applied to the return value of method "Sample.GetAsync(System.Int32, Sample.DataSourceType)": cannot validate a return value of type Task or of another awaitable type. (40, 39)
  1. Support Staff 1 Posted by PostSharp Techn... on 08 Nov, 2017 11:59 PM

    PostSharp Technologies's Avatar


    Code contracts never worked correctly with the return values of async methods, i.e. the returned task instance was validated instead of the task result. In PostSharp 5 we emit a build error to prevent the unexpected behavior. And we treat async tasks and tasks returned by non-async methods the same way - we assume that the user wants to validate the result of the task by default, which currently is not supported by code contracts.

    The support for validation of async return values is planned for PostSharp 5.1. In the meantime you can implement a custom OnMethodBoundaryAspect as a workaround. In PostSharp 5 OnMethodBoundaryAspect is applied semantically to async methods and tasks by default. For more information about semantic advising please see

    Below is the example of the aspects that validates method return values and handles task results as well.

    public sealed class NotNullReturnValueAttribute : OnMethodBoundaryAspect
        public override void OnExit(MethodExecutionArgs args)
            if (args.ReturnValue == null)
                throw new Exception("Method returned null.");


  2. 2 Posted by dave.parson on 09 Nov, 2017 12:49 AM

    dave.parson's Avatar

    That's what our resident PostSharp expert came up with as well, thanks Alex.

  3. dave.parson closed this discussion on 09 Nov, 2017 12:49 AM.

  4. dave.parson re-opened this discussion on 09 Nov, 2017 12:52 AM

  5. 3 Posted by dave.parson on 09 Nov, 2017 12:56 AM

    dave.parson's Avatar

    One other question, is there an ETA for PostSharp 5.1?

  6. Support Staff 4 Posted by PostSharp Techn... on 09 Nov, 2017 10:47 AM

    PostSharp Technologies's Avatar


    According to our policy we do not disclose ETA for our releases. The information I can share is that the work on 5.1 has started recently and I wouldn't expect the RTM release earlier than in 3 months.


  7. 5 Posted by dave.parson on 09 Nov, 2017 10:17 PM

    dave.parson's Avatar

    Sounds good, thank you.

  8. dave.parson closed this discussion on 09 Nov, 2017 10:17 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac