Skip to content
This repository was archived by the owner on Nov 13, 2026. It is now read-only.
This repository was archived by the owner on Nov 13, 2026. It is now read-only.

.NET Core 2.1.0 WebApi Model Binding Issue #6142

Closed

Description

@mattvaughan

POSTing JSON in the body to bind to a simple POCO model doesn't work when compiled natively. Does this require some additions to the rd.xml file? POSTing to a URL without a model to bind to works just fine.

Now listening on: http://localhost:5000
Now listening on: https://localhost:5001
Application started. Press Ctrl+C to shut down.
fail: Microsoft.AspNetCore.Server.Kestrel[13]
      Connection id "0HLFJBDFV3MEM", Request id "0HLFJBDFV3MEM:00000001": An unhandled exception was thrown by the application.
System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.Extensions.Internal.PropertyHelper.MakeFastPropertyGetter(Type, MethodInfo, MethodInfo) + 0xb5
   at Microsoft.Extensions.Internal.PropertyHelper.MakeFastPropertyGetter(PropertyInfo, MethodInfo, MethodInfo) + 0xbd
   at Microsoft.AspNetCore.Mvc.ModelBinding.Metadata.DefaultModelMetadataProvider.CreatePropertyDetails(ModelMetadataIdentity) + 0x173
   at Microsoft.AspNetCore.Mvc.ModelBinding.Metadata.DefaultModelMetadataProvider.GetMetadataForProperties(Type) + 0xb2
   at Microsoft.AspNetCore.Mvc.ModelBinding.Metadata.DefaultModelMetadata.get_Properties() + 0x46
   at Microsoft.AspNetCore.Mvc.ModelBinding.Binders.ComplexTypeModelBinderProvider.GetBinder(ModelBinderProviderContext) + 0x67
   at Microsoft.AspNetCore.Mvc.ModelBinding.ModelBinderFactory.CreateBinderCoreUncached(ModelBinderFactory.DefaultModelBinderProviderContext, Object) + 0x111
   at Microsoft.AspNetCore.Mvc.ModelBinding.ModelBinderFactory.CreateBinder(ModelBinderFactoryContext) + 0x7d
   at Microsoft.AspNetCore.Mvc.Internal.ControllerBinderDelegateProvider.GetParameterBindingInfo(IModelBinderFactory, IModelMetadataProvider, ControllerActionDescriptor, MvcOptions) + 0x147
   at Microsoft.AspNetCore.Mvc.Internal.ControllerBinderDelegateProvider.CreateBinderDelegate(ParameterBinder, IModelBinderFactory, IModelMetadataProvider, ControllerActionDescriptor, MvcOptions) + 0x84
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvokerCache.GetCachedResult(ControllerContext) + 0xe0
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvokerProvider.OnProvidersExecuting(ActionInvokerProviderContext) + 0x94
   at Microsoft.AspNetCore.Mvc.Internal.ActionInvokerFactory.CreateInvoker(ActionContext) + 0x52
   at Microsoft.AspNetCore.Mvc.Internal.MvcAttributeRouteHandler.<>c__DisplayClass12_0.<RouteAsync>b__0(HttpContext) + 0x98
   at Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.MoveNext() + 0x382
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x1b
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.<ProcessRequests>d__186`1.MoveNext() + 0x732

Activity

  1. mattvaughan commented on Jul 27, 2018

    @mattvaughan
    Author

    After many hours of frustration, it seems to have just started working. In the end, I don't believe I'm doing anything that isn't in the WebApi example.

  2. mattvaughan commented on Jul 28, 2018

    @mattvaughan
    Author

    I was mistaken. The original issue is still a problem

  3. MichalStrehovsky commented on Jul 29, 2018

    @MichalStrehovsky
    Member

    This is probably because we didn't compile a property since it wasn't statically used by the program. The CoreRT compiler only compiles things that are statically used, or specified in RD.XML.

    I submitted #6149 that should make troubleshooting this easier. Once this is merged and a new ILCompiler package is out, you can add <IlcArg Include="--completetypemetadata" /> into one of your project's ItemGroups and you should get a more actionable exception. (You'll still need to mess with RD.XML to fix the actual problem, but it should be more obvious what's missing.)

  4. mattvaughan commented on Jul 29, 2018

    @mattvaughan
    Author

    @MichalStrehovsky I tested this with a few changes to the sample webapi and receive the same error. Wouldn't the static reference to the SimpleModel property in the POST method mark it for compilation? What else would need to be added to rd.xml?:

        public class SimpleModel
        {
            public string Name { get; set; }
        }
    
            [HttpPost("/api/values")]
            public string Post([FromBody] SimpleModel model)
            {
                return model.Name;
            }
    
            <Assembly Name="SampleWebApi" Dynamic="Required All" />
    
  5. MichalStrehovsky commented on Jul 30, 2018

    @MichalStrehovsky
    Member

    If the property being reflected on is in the SampleWebApi assembly, this would be a bug somewhere. But from the stack trace it's not clear what property is the ASP.NET Core stack trying to reflect on. This will be easiest to root cause when the new ILCompiler package with the --completetypemetadata switch support comes out in a couple hours.

  6. mattvaughan commented on Jul 30, 2018

    @mattvaughan
    Author

    @MichalStrehovsky Using the new versions yields this error. I've tried adding the type specified using the URL in the error but still end up with build errors. How should this type be added to rd.xml?:

    fail: Microsoft.AspNetCore.Server.Kestrel[13]                                                                                              
          Connection id "0HLFM7GNSSSGI", Request id "0HLFM7GNSSSGI:00000001": An unhandled exception was thrown by the application.
    EETypeRva:0x01FAA158: MakeGenericMethod() cannot create this generic method instantiation because the instantiation was not metadata-enabled: 'Microsoft.Extensions.Internal.PropertyHelper.CallNullSafePropertyGetter<SampleWebApi.Controllers.SimpleModel,System.String>(Syst
    em.Func<SampleWebApi.Controllers.SimpleModel,System.String>,System.Object)' For more information, please visit http://go.microsoft.com/fwlink/?LinkID=616868
       at SampleWebApi!<BaseAddress>+0x22c5b3                           
       at SampleWebApi!<BaseAddress>+0x53e970                                                                       
       at SampleWebApi!<BaseAddress>+0xaf1ffe                                   
       at SampleWebApi!<BaseAddress>+0x4e2fa6                                  
       at SampleWebApi!<BaseAddress>+0x510be3                                                                                                          
       at SampleWebApi!<BaseAddress>+0xaf164e                                   
       at Microsoft.Extensions.Internal.PropertyHelper.MakeFastPropertyGetter(Type, MethodInfo, MethodInfo) + 0x174
       at Microsoft.Extensions.Internal.PropertyHelper.MakeFastPropertyGetter(PropertyInfo, MethodInfo, MethodInfo) + 0x108
       at Microsoft.Extensions.Internal.PropertyHelper.MakeNullSafeFastPropertyGetter(PropertyInfo) + 0x4d                
       at Microsoft.AspNetCore.Mvc.ModelBinding.Metadata.DefaultModelMetadataProvider.CreatePropertyDetails(ModelMetadataIdentity) + 0x29e
       at Microsoft.AspNetCore.Mvc.ModelBinding.Metadata.DefaultModelMetadataProvider.GetMetadataForProperties(Type) + 0x13a
       at Microsoft.AspNetCore.Mvc.ModelBinding.Metadata.DefaultModelMetadata.get_Properties() + 0x64               
       at Microsoft.AspNetCore.Mvc.Internal.DefaultComplexObjectValidationStrategy.GetChildren(ModelMetadata, String, Object) + 0x3a
       at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.VisitChildren(IValidationStrategy) + 0x96
       at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.VisitComplexType(IValidationStrategy) + 0xea
       at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.Visit(ModelMetadata, String, Object) + 0x435
       at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.Validate(ModelMetadata, String, Object, Boolean) + 0xd8
       at Microsoft.AspNetCore.Mvc.ModelBinding.ObjectModelValidator.Validate(ActionContext, ValidationStateDictionary, String, Object) + 0x11d
       at Microsoft.AspNetCore.Mvc.ModelBinding.ParameterBinder.<BindModelAsync>d__11.MoveNext() + 0x863       
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x31                       
       at SampleWebApi!<BaseAddress>+0xa05532                                                                     
       at SampleWebApi!<BaseAddress>+0xa0544d                                          
       at SampleWebApi!<BaseAddress>+0xa053aa                                   
       at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() + 0x23                                           
       at Microsoft.AspNetCore.Mvc.Internal.ControllerBinderDelegateProvider.<>c__DisplayClass0_0.<<CreateBinderDelegate>g__Bind|0>d.MoveNext() + 0x39c
    --- End of stack trace from previous location where exception was thrown ---                  
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x31                                                                                                                                                                               
       at SampleWebApi!<BaseAddress>+0xa05532                           
       at SampleWebApi!<BaseAddress>+0xa0544d                                                                             
       at SampleWebApi!<BaseAddress>+0xa053aa     
       at System.Runtime.CompilerServices.TaskAwaiter.GetResult() + 0x19                                                                                                        
       at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeInnerFilterAsync>d__13.MoveNext() + 0x148  
    
  7. MichalStrehovsky commented on Jul 30, 2018

    @MichalStrehovsky
    Member

    The URL in the exception message points to the "official" RD.XML syntax. The CoreRT compiler speaks a weird dialect. The format is described here: #4775

  8. mattvaughan commented on Jul 30, 2018

    @mattvaughan
    Author

    Is there a way to output the assembly in this error? This class and method isn't documented with Microsoft and I am having difficulty locating the assembly it resides in.

    Microsoft.Extensions.Internal.PropertyHelper.CallNullSafePropertyGetter

    A grep of the .dll files in refs yields some results, but adding those to RD.XML didn't solve the issue.

    Following the syntax in the issue you posted, I came up with:

        <Assembly Name="<assembly.goes.here>">
          <Type Name="Microsoft.Extensions.Internal.PropertyHelper">
            <Method Name="CallNullSafePropertyGetter" Dynamic="Required">
              <GenericArgument Name="SampleWebApi.Controllers.SimpleModel, System.String" />
            </Method>
          </Type>
        </Assembly>
    
  9. MichalStrehovsky commented on Jul 30, 2018

    @MichalStrehovsky
    Member

    The place where you're putting System.String should be an assembly name (SampleWebApi, I assume). The System.String should be a separate GenericArgument (assembly qualified with System.Runtime).

    I would use a tool such as ILSpy to find the assembly. You can drag/drop all the assemblies into the UI and use Ctrl-F to find the type by name.

  10. mattvaughan commented on Jul 30, 2018

    @mattvaughan
    Author

    Fantastic, this ended up working for those errors. Is there currently a way around having to specify this for each of the classes you are using for model binding?:

        <Assembly Name="Microsoft.AspNetCore.Mvc.Core" Dynamic="Required All">
          <Type Name="Microsoft.Extensions.Internal.PropertyHelper">
            <Method Name="CallNullSafePropertyGetter" Dynamic="Required">
              <GenericArgument Name="SampleWebApi.Controllers.SimpleModel, SampleWebApi" />
              <GenericArgument Name="System.String, System.Runtime" />
            </Method>
            <Method Name="CallPropertySetter" Dynamic="Required">
              <GenericArgument Name="SampleWebApi.Controllers.SimpleModel, SampleWebApi" />
              <GenericArgument Name="System.String, System.Runtime" />
            </Method>
          </Type>
        </Assembly>
    
  11. MichalStrehovsky commented on Jul 31, 2018

    @MichalStrehovsky
    Member

    Is there currently a way around having to specify this for each of the classes you are using for model binding

    If these are reference types (which it seems they are), you don't need to specify this for each property. Generic instantiations over reference types share native code, so we don't need to know exactly what will be used. The runtime can manufacture the types on the go (the problem is really only about having the native code available).

          <GenericArgument Name="System.Object, System.Runtime" />
          <GenericArgument Name="System.Object, System.Runtime" />

    Should also work, and is a bit more flexible because it avoids hardcoding the SampleWebApi string.

    If you have a complete set of RD.XML that is needed to enable this in the SampleWebApi project, it would be great if you could contribute it in a form of a pull request to help others.

  12. mattvaughan commented on Jul 31, 2018

    @mattvaughan
    Author

    @MichalStrehovsky Sounds great! I'll continue to refine the RD.XML and once I feel it is in a good place, I'll submit a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions