question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Modify ViewBag in Controller will cause RuntimeBinderException in 6.0.0-preview4

See original GitHub issue

Describe the bug

If I add some “ViewBag” code in MVC Controller will cause RuntimeBinderException in 6.0.0-preview4.

RuntimeBinderException: 'System.Dynamic.DynamicObject' does not contain a definition for 'OK'

It could be a hot reload bug.

To Reproduce

I recorded all steps in video here: https://youtu.be/JDX7nM7Fh7c (Only 85 seconds without any sound/voice)

Exceptions (if any)

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'System.Dynamic.DynamicObject' does not contain a definition for 'OK'
   at CallSite.Target(Closure , CallSite , Object , String )
   at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1) in System.Linq.Expressions.dll:token 0x6001062+0x11d
   at Projects.Controllers.HomeController.Index() in Projects.dll:token 0x600006b+0x58
   at lambda_method1(Closure , Object , Object[] )
   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) in Microsoft.AspNetCore.Mvc.Core.dll:token 0x6000fa1+0x0
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync() in Microsoft.AspNetCore.Mvc.Core.dll:token 0x600099f+0x2e
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) in Microsoft.AspNetCore.Mvc.Core.dll:token 0x600099c+0x33a
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync() in Microsoft.AspNetCore.Mvc.Core.dll:token 0x600099d+0xa
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context) in Microsoft.AspNetCore.Mvc.Core.dll:token 0x60009a1+0x15
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) in Microsoft.AspNetCore.Mvc.Core.dll:token 0x600099c+0x39a
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync() in Microsoft.AspNetCore.Mvc.Core.dll:token 0x60009a0+0xa
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|25_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) in Microsoft.AspNetCore.Mvc.Core.dll:token 0x6000a7f+0x6a
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context) in Microsoft.AspNetCore.Mvc.Core.dll:token 0x6000a74+0x15
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) in Microsoft.AspNetCore.Mvc.Core.dll:token 0x6000a6b+0x835
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync() in Microsoft.AspNetCore.Mvc.Core.dll:token 0x6000a68+0xa
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope) in Microsoft.AspNetCore.Mvc.Core.dll:token 0x6000a77+0x77
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope) in Microsoft.AspNetCore.Mvc.Core.dll:token 0x6000a77+0xfb
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger) in Microsoft.AspNetCore.Routing.dll:token 0x60000aa+0x5e
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) in Microsoft.AspNetCore.Authorization.Policy.dll:token 0x600000b+0x16b
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context) in Microsoft.AspNetCore.Diagnostics.dll:token 0x60000aa+0x82

Further technical details

  • ASP.NET Core version:

    Microsoft.AspNetCore.App 6.0.0-preview.4.21253.5
    
  • Include the output of dotnet --info

    .NET SDK (reflecting any global.json):
    Version:   6.0.100-preview.4.21255.9
    Commit:    950e4949a7
    
    Runtime Environment:
    OS Name:     Windows
    OS Version:  10.0.19042
    OS Platform: Windows
    RID:         win10-x64
    Base Path:   C:\Program Files\dotnet\sdk\6.0.100-preview.4.21255.9\
    
    Host (useful for support):
    Version: 6.0.0-preview.4.21253.7
    Commit:  bfd6048a60
    
    .NET SDKs installed:
    3.1.100 [C:\Program Files\dotnet\sdk]
    3.1.115 [C:\Program Files\dotnet\sdk]
    3.1.201 [C:\Program Files\dotnet\sdk]
    3.1.301 [C:\Program Files\dotnet\sdk]
    3.1.302 [C:\Program Files\dotnet\sdk]
    3.1.402 [C:\Program Files\dotnet\sdk]
    3.1.409 [C:\Program Files\dotnet\sdk]
    5.0.104 [C:\Program Files\dotnet\sdk]
    5.0.203 [C:\Program Files\dotnet\sdk]
    5.0.300 [C:\Program Files\dotnet\sdk]
    6.0.100-preview.4.21255.9 [C:\Program Files\dotnet\sdk]
    
    .NET runtimes installed:
    Microsoft.AspNetCore.All 2.1.28 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
    Microsoft.AspNetCore.App 2.1.28 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
    Microsoft.AspNetCore.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
    Microsoft.AspNetCore.App 3.1.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
    Microsoft.AspNetCore.App 3.1.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
    Microsoft.AspNetCore.App 3.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
    Microsoft.AspNetCore.App 3.1.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
    Microsoft.AspNetCore.App 3.1.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
    Microsoft.AspNetCore.App 5.0.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
    Microsoft.AspNetCore.App 5.0.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
    Microsoft.AspNetCore.App 5.0.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
    Microsoft.AspNetCore.App 6.0.0-preview.4.21253.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
    Microsoft.NETCore.App 2.1.28 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
    Microsoft.NETCore.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
    Microsoft.NETCore.App 3.1.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
    Microsoft.NETCore.App 3.1.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
    Microsoft.NETCore.App 3.1.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
    Microsoft.NETCore.App 3.1.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
    Microsoft.NETCore.App 3.1.15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
    Microsoft.NETCore.App 5.0.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
    Microsoft.NETCore.App 5.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
    Microsoft.NETCore.App 5.0.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
    Microsoft.NETCore.App 6.0.0-preview.4.21253.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
    Microsoft.WindowsDesktop.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
    Microsoft.WindowsDesktop.App 3.1.3 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
    Microsoft.WindowsDesktop.App 3.1.5 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
    Microsoft.WindowsDesktop.App 3.1.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
    Microsoft.WindowsDesktop.App 3.1.8 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
    Microsoft.WindowsDesktop.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
    Microsoft.WindowsDesktop.App 3.1.15 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
    Microsoft.WindowsDesktop.App 5.0.4 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
    Microsoft.WindowsDesktop.App 5.0.5 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
    Microsoft.WindowsDesktop.App 5.0.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
    Microsoft.WindowsDesktop.App 6.0.0-preview.4.21254.5 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
    
    To install additional .NET runtimes or SDKs:
    https://aka.ms/dotnet-download
    
  • The IDE (VS / VS Code/ VS4Mac) you’re running on, and its version

    Version: 1.56.2 (user setup)
    Commit: 054a9295330880ed74ceaedda236253b4f39a335
    Date: 2021-05-12T17:13:13.157Z
    Electron: 12.0.4
    Chrome: 89.0.4389.114
    Node.js: 14.16.0
    V8: 8.9.255.24-electron.0
    OS: Windows_NT x64 10.0.19042
    

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
pranavkmcommented, Sep 21, 2021

@doggy8088, thanks for trying this out with the newest builds. We’ll use https://github.com/dotnet/roslyn/issues/56592 to track addressing this as it’s not specific to MVC. In the meanwhile, you could consider using ViewData instead to pass contents to your views. We generally recommend it over ViewBag anyway since it’s ever so slightly better performing.

1reaction
doggy8088commented, Sep 17, 2021

@mkArtakMSFT I tried 6.0.100-rc.1.21458.32. The issue still there. ☹

Read more comments on GitHub >

github_iconTop Results From Across the Web

RuntimeBinderException when using ViewBag
These exceptions are caused by the underlying dynamic object that is being used. The .NET framework somehow always throws these exceptions.
Read more >
I am getting Error in Asp.Net MVC "Cannot perform runtime ...
I am getting Error in Asp.Net MVC "Cannot perform runtime binding on a null reference"
Read more >
ViewBag in ASP.NET MVC
ViewBag doesn't require typecasting while retrieving values from it. This can throw a run-time exception if the wrong method is used on the...
Read more >
What is ViewBag: How It Works & Code Examples
ViewBag is a property – considered a dynamic object – that enables you to share values dynamically between the controller and view within ......
Read more >
ViewBag in ASP.NET Core MVC with Examples
NET Core MVC application, we can pass the data from a controller to a view using ViewData, ViewBag, TempData, and Strongly Typed View...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found