49

I'm trying to run a .NET MVC application on my local computer that I got from GitHub.

When I hit run on Visual Studio, everything complies and a new browser window opens with the error:

CS1980: Cannot define a class or member that utilizes 'dynamic' because the compiler required type 'System.Runtime.CompilerServices.DynamicAttribute'

The compiler section in the same window, shows the following error:

enter image description here

I've checked on google and this seems to be the same error.

This guy was using a dynamic type himself, on the other hand I'm not using any dynamic type, this is being auto generated by .NET's compiler, and I'm using .net 4.5 which should has support for dynamics.

I tried to apply the same solution (Changing all System.Core references to 4.0) but the thing is that all of them are version 4.0 already.

Do you have any idea how could I resolve this?

Community
  • 1
  • 1
Alejandro Lozdziejski
  • 1,063
  • 4
  • 12
  • 30

12 Answers12

38

I had a similar error on a project I was trying to migrate. Try re-targeting the framework of the project back a version or two, and once you find a target version where you do not get the error, re-target back to the version you originally had trouble with.

I was getting the same error on a website project targeted for 4.6.2 that was referencing some older libraries, re-targeted it back to 4.5.1 and that resolved the error. I immediately re-targeted back to 4.6.2 and did not encounter the error again.

Granicus
  • 854
  • 10
  • 19
  • This worked for me but I needed to close my solution before I rebuilt it in the earlier framework. – DaVinciCoder Sep 29 '16 at 20:46
  • 1
    This worked for me. VS 2015. Changed from framework 4.6 back to 4.5.2 and back to 4.6. Nice. Thanks. – Leonardo Cunha Apr 11 '18 at 16:00
  • I can't believe this worked... I had a couple other nuget packages doing the same thing. The views would run mostly but the Error List in Visual Studio was full of annoying errors. – WHol Feb 28 '19 at 16:45
  • 1
    I tried targeting all frameworks from 4 to 4.7 and none of these re-targeting worked for me. – Marc Roussel Jul 16 '19 at 13:40
10

Saw this in VS 2019. I excluded and re-included the problematic file (in my case _Layout.cshtml) and that fixed the problem.

Ajay2707
  • 5,690
  • 6
  • 40
  • 58
Ron Swanson
  • 101
  • 1
  • 4
  • 1
    This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/25817638) – Miss Chanandler Bong Apr 09 '20 at 14:57
  • 4
    I disagree with you on that. My solution worked in my case and was offered as an option/possibility for anyone who might stumble upon this thread via a Google search like I did. If my solution is wrong, so is brichins' which is basically the same thing and I don't see you nagging that person. – Ron Swanson Apr 10 '20 at 16:06
  • 2
    In development we often face red herring errors, especially when compiling. This is one of those cases for many who see this error I suspect. This actually does fix the problem, even if it works indirectly. – Adam R. Turner Jun 03 '21 at 17:31
9

I had this same issue, but none of the answers here solved it, but did point me in the right direction. I think I didn't update the .NET version on my machine to the newer one. After installing it, I rolled back the targeted framework (as was suggested), retargeted the newer framework, and did a few extra things also:

  1. Reinstalled ALL packages by running this in the package manager:

    update-package -reinstall

I also compared my project to one that was working for 4.5 and did this:

  1. Opened my project file and removed all "BCL" related elements.
  2. Uninstalled all "BCL" packages in the NuGet package manager for the project (guess it's not needed moving to 4.5+...?)
  3. Removed an old package version of System.Net.Http, which did not get updated for some reason (see web.config - versions should show oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0").
  4. Have to restart VS.
  5. Rebuild solution.
  6. Error is now gone. ;)

One other thing I did to resolve some other errors was to clear the component cache by deleting all files in here:

C:\Users\{USERNAME}\AppData\Local\Microsoft\VisualStudio\14.0\ComponentModelCache\

[rant] Wasted a whole day with all this. Thanks to MS for stealing one more day of my life fixing senseless issues that should never be. Every time I upgrade I always get a wonderful surprise, and a lovely game of fix the red herring. Would it not hurt to detect these conflicts for those upgrading old projects? Perhaps better error traces when things go wrong? I doubt it would be that difficult. Perhaps even something like AsmSpy, but more advanced, built in to VS? [/rant]

James Wilkins
  • 6,836
  • 3
  • 48
  • 73
  • I tried the complete solution here witch took a very long time by the way and to no luck, The error is still there in a .cshtml file opened. – Marc Roussel Jul 16 '19 at 13:43
  • 1
    That was back in 2016. There could be a lot of differences today depending on the project types, references, VS version, etc. For instance, I was using 2015 back then. Today I'm using 2017. – James Wilkins Jul 16 '19 at 18:01
  • I'm using 2019 latest here. Ok it's solved. Somehow, I was missing the assembly System.Core in the Web.Config. After adding again, closing the solution and opening again, the message was gone. Even thought the System.Dynamic.Runtime wasn't referenced. I think the Web.Config contains the link which is taken in System.Core but I'm not sure. All in all, adding the assembly System.Core under is enough – Marc Roussel Jul 18 '19 at 03:04
8

I got this error suddenly a couple days after updating to VS2015.3 using 'traditional' MVC app (not new .NET Core / vNext app).

Turns out this error can be a complete red herring.

Make sure:

  • You haven't got any other errors at all
  • You've checked the output window for any errors that don't show in the 'Output' window.
  • Make sure you don't have any conflicting references

In my case I had a typescript error that was causing an error and for some reason triggering this weird 'red herring' dynamic error too.

enter image description here

My fix was to go into the .csproj file and update the typescript tools version to 1.8 (need to unload project and edit by hand). As I said this is unrelated to the error, but I mention the specifics because someone else may end up with this same problem.

I believe that 1.7 didn't accept 'None' for the typescript module system whereas 1.8 does, hence the error.

Community
  • 1
  • 1
Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689
7

Adding a reference to System.Dynamic.Runtime did it for me.

Fredrik Hall
  • 308
  • 3
  • 5
6

I hesitate to write this as an answer, but for me the error appeared in a .cshtml file. I closed the page, rebuilt, and the error vanished. Always best to try the simple things first.

nmit026
  • 3,024
  • 2
  • 27
  • 53
1

My solution was to correct the version of MVC in web.config under Views folder. E.g. the project referenced MVC assembly version 5.2.2.0 but in the Views/web.config file was version 5.2.4.0 (see image).

Image

Nexaspx
  • 371
  • 4
  • 20
Geafer
  • 11
  • 3
1

I had the same error, there were just 2 unused tables of my databaseset that I had to delete. I wonder why it worked when I manually change sth in an auto-generated code work but after deleting, it worked.

So be aware that such inconsistencies can also cause this error.

thestruggleisreal
  • 940
  • 3
  • 10
  • 26
1

Tried a few other answers with no luck, but the overall consensus appears to be that this some sort of issue with cached build artifacts or compiler state or something. Here's what worked for me:

  • Delete the problem .cshtml file, watch error disappear
  • Open source control and revert the deletion
brichins
  • 3,825
  • 2
  • 39
  • 60
0

This error will come because of the different versions of your Sitecore.Web.Mvc.Dll in solution reference folder(Project name->References->Sitecore.Web.Mvc->right click and check properties) and the version of Sitecore.Web.Mvc.Dll in Web.Config in views Folder.

For solving this error you have to make sure that the version of Sitecore.Web.Mvc.Dll is same in both folders.

Thanx.

0

For my case, I fixed to run the command line "update-package -reinstall" on the Package Manager Controler.

0

My problem was in the _Layout.cshtml. None of the solutions above did the trick for me. I am using VS2017 for this project, upgraded an ran into the same message.

I had commented out a 2 lines in 1 comment @* *@

My solution was to comment out each line separately. What a mystery!

A few hours lost on this.

Coen
  • 1
  • 2