Mvc: ASP.NET Web Api on Raspberry PI with Kestrel Web Server

Created on 24 Apr 2017  路  14Comments  路  Source: aspnet/Mvc

i have developed an AST.NET Core app targeting core version "2.0.0-preview1-001875-00"

When i run my app on my Windows dev-machine or an ubuntu desktop everthings works fine. But when i try to run it on ubuntu 16.04 on an raspberry 3 i get an nullrefexception from the kestrel webserver.

The stack is as follows:

Microsoft.AspNetCore.Server.Kestrel[13] Connection id "0HL4AVO08A0CQ", Request id "0HL4AVO08A0D2": An unhandled exception was thrown by the application.
System.NullReferenceException: Object reference not set to an instance of an object.
 at Microsoft.AspNetCore.Mvc.ModelBinding.CompositeValueProvider.GetValue(String key)
 at Microsoft.AspNetCore.Mvc.ModelBinding.Binders.SimpleTypeModelBinder.BindModelAsync(ModelBindingContext bindingContext)
 at Microsoft.AspNetCore.Mvc.ModelBinding.ParameterBinder.d__5.MoveNext()
 --- End of stack trace from previous location where exception was thrown ---
 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
 at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__26.MoveNext()
 --- End of stack trace from previous location where exception was thrown ---
 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
 at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__21.MoveNext()
 --- End of stack trace from previous location where exception was thrown ---
 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
 at Microsoft.AspNetCore.Mvc.Core.Internal.ResourceInvoker.d__18.MoveNext()
 --- End of stack trace from previous location where exception was thrown ---
 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
 at Microsoft.AspNetCore.Mvc.Core.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
 at Microsoft.AspNetCore.Mvc.Core.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
 at Microsoft.AspNetCore.Mvc.Core.Internal.ResourceInvoker.d__13.MoveNext()
 --- End of stack trace from previous location where exception was thrown ---
 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
 at Microsoft.AspNetCore.Mvc.Core.Internal.ResourceInvoker.d__11.MoveNext()
 --- End of stack trace from previous location where exception was thrown ---
 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
 at Microsoft.AspNetCore.Builder.RouterMiddleware.d__4.MoveNext()
 --- End of stack trace from previous location where exception was thrown ---
 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
 at Microsoft.AspNetCore.Hosting.Internal.RequestServicesContainerMiddleware.d__3.MoveNext()
 --- End of stack trace from previous location where exception was thrown ---
 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
 at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Frame`1.d__2.MoveNext() Microsoft.AspNetCore.Builder.RouterMiddleware.

Is there something i am missing?

All 14 comments

After some digging I found that it is this httpget which returns an simple bool. The get which return an complex type work fine.

image

Hi - just adding to this. I also get an identical error. I've tried returning a complex type instead of a simple type as per the suggestion above but I still get that error.

My application is a simple WebAPI application (in fact it's just the ValuesController generated in a default project created by "dotnet new webapi -n MyProject".

My observation is that when I call the HttpGet method with a signature of Get(), it works fine.
When I call the HttpGet method with a signature of Get(int id), then I get the same error message reported above.
But if I refresh that page, then the Get(int id) method call works fine.

It was my mistake it is not the return type that works for simple types. It is the parameter that works for complex but not for simple. It seems that the RouteValueProvider throughs the NullRef because the key is null. I removed the exception and returned none. Now it seems to work. But I have no Idea why the key is null.

```c#
public override ValueProviderResult GetValue(string key)
{
if (key == null)
{
return ValueProviderResult.None;
//throw new ArgumentNullException(nameof(key));
}

        object value;
        if (_values.TryGetValue(key, out value))
        {
            var stringValue = value as string ?? value?.ToString() ?? string.Empty;
            return new ValueProviderResult(stringValue, Culture);
        }
        else
        {
            return ValueProviderResult.None;
        }
    }

```

Thank you - this is good information. I will try to recreate this later at home and see if using a non-primitive parameter works.

One more thing I found - I got exactly the same issue on a Raspberry Pi 3 hosting Windows 10 IoT Core, so this isn't just an Ubuntu issue,

@dos-ise I looked at the code for CompositeValueProvider's GetValue method,
and the only place I see a possibility for a NullRef is this line. So that makes me thing that something is adding a null ValueProvider to the list of value providers, which would be quite unusual.

Is there any code in your app (in Startup.cs, most likely) that is changing MVC's options and maybe adding a null value provider? Or a value provider factory that returns null?

If you could share your Startup.cs's ConfigureServices method that might help. Thanks!

I am not changing the behaviour of the MVC. As mentioned by @jeremylindsayni it already occurs in the values example template.

I can share my complete workaround code here on Monday.

Ah sorry I missed that. If you have a repro app we will try to take a look!

Hi there - I've a sample app up on Github here which reproduces this issue (for me at least). I created this while .NET Core before the recent preview release but I hope it's still valid. If I can help with more details, please let me know!

Hey @dos-ise - do you have some workaround code that you could share please? It would be very useful to me if you could share it! Thanks, Jeremy

Hey @dos-ise - thank you for this.

Hey - I just pulled the Dev branch of MVC, created a basic WebAPI project and deployed to a RaspPi 3 - I'm not recreating this issue any more! Which is great - so it looks like the bug has been fixed at some point.

Yes, i can verify this. I updated to 2.0.0-preview3-25602 and the bug is gone!! Awesome! :)

Sorry for the delay getting back to you, thank you for confirming that this issue is gone!

Was this page helpful?
0 / 5 - 0 ratings