Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot use dynamic type with asp.net vnext core

I need to read and save a JSON file inside an ASP.NETt vNext app, and I would like to use a dynamic variable to store the value loaded using JSON.net, but when I go to compile I received this error message:

ASP.NET Core 5.0 error CS1980: Cannot define a class or member that utilizes 'dynamic' because the compiler required type 'System.Runtime.CompilerServices.DynamicAttribute' cannot be found. Are you missing a reference?

How can solve this? If I use dynamic, can I run an application using ASP.NET core?

like image 465
Luca Morelli Avatar asked Jan 09 '15 05:01

Luca Morelli


2 Answers

You need to include the packages Microsoft.CSharp and System.Dynamic.Runtime for the aspnetcore50 framework.

This seems to work for me with CoreCLR version 1.0.0-beta1:

using Newtonsoft.Json.Linq;
using System;

namespace DynamicTest
{
    public class Program
    {
        public void Main(string[] args)
        {

            dynamic dobject = JObject.Parse("{number:1000, str:'string', array: [1,2,3,4,5,6]}");

            Console.WriteLine(dobject.number);
            Console.WriteLine(dobject.str);
            Console.WriteLine(dobject.array.Count);
            Console.ReadLine();
        }
    }
}

The project.json

{
    "version": "1.0.0-*",
    "dependencies": {
        "Newtonsoft.Json" :  "6.0.7"
    },
    "commands": { 
        "run" : "run"
    },
    "frameworks" : {
        "aspnet50" : { },
        "aspnetcore50" : { 
            "dependencies": {
                "System.Console": "4.0.0-beta-22231",
                "System.Dynamic.Runtime": "4.0.0-beta-22231",
                "Microsoft.CSharp": "4.0.0-beta-22231"
            }
        }
    }
}
like image 171
AndersNS Avatar answered Sep 21 '22 16:09

AndersNS


I was fighting this problem, along with an even less useful error message...

The type or namespace name 'Linq' does not exist in the namespace 'System.Data'

But, I was running into the problem on my IIS Server (2012 R2 - IIS 8). Most of the posts I read on these problems were dealing with their development environment, not their web server.

In order to resolve both problems, I had to copy the System.Core.dll, from the servers framework folder (%windows path%\Microsoft.Net\Framework\v4.0.30319), into my web applications bin folder.

I'm not clear on why this was necessary, but I spent 3 days reading through documentation and forum posts, and nothing out there worked. We just got lucky...

Maybe this will help someone else that is struggling.

like image 45
Brent Goedert Avatar answered Sep 17 '22 16:09

Brent Goedert