#2510·Nancy

Error binding to model with C# 6 auto-property initializer

Author: RPM1984Created Jul 17, 2016Updated Feb 10, 2020

Prerequisites

  • I have written a descriptive issue title
  • I have verified that I am running the latest version of Nancy
  • I have verified if the problem exist in both DEBUG and RELEASE mode
  • I have searched open and closed issues to ensure it has not already been reported

Description

Hey guys, i'm getting a weird error with Nancy model binding, relating to C#6 auto props.

  • Unable to bind to type xxx
  • Inner exception: "Non-static method requires a target."

The model inherits from this base class:

public abstract class Operation
{
   public UserData UserData { get; set; } = new UserData();
   public bool IsAuthenticated => UserData?.LoggedInMember != null && UserData.LoggedInMember.Id > 0;
}

It doesn't like the IsAuthenticated prop. If i take it out, it binds fine.

I've tried ignoring with:

var query = nancyModule.Bind<MyModel>(new BindingConfig(), x => x.IsAuthenticated);

But the same error occurs.

If i change the property to a method, it works fine, e.g:

public bool IsAuthenticated()
{
   return UserData?.LoggedInMember != null && 
      UserData.LoggedInMember.Id > 0;
}

Steps to Reproduce

  1. Create model which contains an auto-property initializer.
  2. Try and bind to it using .Bind<T>()

System Configuration

  • Nancy version: 1.4.3
  • Nancy host
    • ASP.NET
    • OWIN
    • Self-Hosted
    • Other:
  • Other Nancy packages and versions: Nancy.Owin 1.4.1
  • Environment (Operating system, version and so on): Windows 10
  • .NET Framework version: 4.6.1