Realm 10.2.0 Exception

Hi, using Realm 10.1.4 my aspcore project works fine.

After update to 10.2.0 crash when I passing login and password from cshtml view to controller with this exception that I don’t undenstand (I downgrade to 10.1.4).
I create User Object Realm not persistent in Get Login Page, user post value of user and oassword to Controller in post for check login, with 10.1.4 all ok, with 10.2.0 post controller is not called, first I have this exceptio when click on subit button:

Can you help me? Thanks Luigi

  • NotSupportedException: Using the dynamic API to access a RealmObject is only possible for managed (persisted) objects.

    • Realms.RealmObjectBase.get_DynamicApi()

    • Microsoft.Extensions.Internal.PropertyHelper.CallNullSafePropertyGetter<TDeclaringType, TValue>(Func<TDeclaringType, TValue> getter, object target)

    • Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationEntry.get_Model()

    • Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.VisitChildren(IValidationStrategy strategy)

    • Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.VisitComplexType(IValidationStrategy defaultStrategy)

    • Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.VisitImplementation(ref ModelMetadata metadata, ref string key, object model)

    • Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.Visit(ModelMetadata metadata, string key, object model)

    • Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.VisitChildren(IValidationStrategy strategy)

    • Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.VisitComplexType(IValidationStrategy defaultStrategy)

    • Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.VisitImplementation(ref ModelMetadata metadata, ref string key, object model)

    • Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.Visit(ModelMetadata metadata, string key, object model)

    • Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.Validate(ModelMetadata metadata, string key, object model, bool alwaysValidateAtTopLevel, object container)

    • Microsoft.AspNetCore.Mvc.ModelBinding.ObjectModelValidator.Validate(ActionContext actionContext, ValidationStateDictionary validationState, string prefix, object model, ModelMetadata metadata, object container)

    • Microsoft.AspNetCore.Mvc.ModelBinding.ParameterBinder.EnforceBindRequiredAndValidate(ObjectModelValidator baseObjectValidator, ActionContext actionContext, ParameterDescriptor parameter, ModelMetadata metadata, ModelBindingContext modelBindingContext, ModelBindingResult modelBindingResult, object container)

    • Microsoft.AspNetCore.Mvc.ModelBinding.ParameterBinder.BindModelAsync(ActionContext actionContext, IModelBinder modelBinder, IValueProvider valueProvider, ParameterDescriptor parameter, ModelMetadata metadata, object value, object container)

    • System.Runtime.CompilerServices.ValueTaskAwaiter.GetResult()

    • Microsoft.AspNetCore.Mvc.Controllers.ControllerBinderDelegateProvider+<>c__DisplayClass0_0+<g__Bind|0>d.MoveNext()

    • Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)

    • Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)

    • Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)

    • Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)

    • Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()

    • Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)

    • Microsoft.AspNetCore.Routing.EndpointMiddleware.g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)

    • Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)

    • Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)

    • Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)

    • Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

Hm, that’s interesting - it seems like MVC is trying to access the DynamicApi property through reflection but that’s not something that will not work for unmanaged objects.

I’ll need to research how, but there’s probably a way to tell MVC to ignore some properties.

Looks like you can implement IPropertyValidationFilter and apply the attribute on your type to tell the validator to ignore all Realm-related properties, including DynamicApi.

1 Like

ok, I try but what’s the difference from 10.1.2 and 10.2.0? Why there’s this exception?
Solution is more more complex for me. I condivide same model class realm in Xamarin App and class not compile in Xamarim with specific annotation of AspCore…

Ok, because I don’t use validation of MVC, I solved disabling validation like suggest in this post:

Now 10.2.0 works fine. This solution consists in a new class and a single line in program.cs, It’s not modify my Realm Object, Xamarin Projects works too

Thanks
Luigi

10.2.0 introduces the DynamicApi property on RealmObject - it is intended to allow developers to dynamically read and write data to the database - e.g. during migrations when the C# model doesn’t match the database view. Since it’s only intended for accessing database objects, it throws an exception when accessed on unmanaged objects. Typically this is not a problem as you won’t accidentally try to use the dynamic API, but with MVC, the validator attempts to touch every property on the object, thus hitting this exception.