6

Microsoft Visual Studio 2010 Ultimate (Version 10.0.40219.1 SP1Rel).

Microsoft .NET Framework version 4.5.50709 SP1Rel

I am compiling to .net framework 4.0.

Whenever I try to use the dynamic or var data types, I get the error shown in the subject line:

The type or namespace name 'dynamic' could not be found.

The type or namespace name 'var' could not be found.

I am trying to use JsonFX to parse data that I receive from another web service. Sometimes with data will represent a "message", and sometimes it will represent a "track". According to this JsonFx Documentation, I should be able to follow the example for "Serialize to/from dynamic types (default for .NET 4.0):"

I added a page to my site called test. The code block below is from Test.aspx.cs The code I am trying to use is this:

using System;
using System.Text;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using JsonFx;
using JsonFx.Json;
using Microsoft.CSharp;

public partial class Test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string Data = "";
        Data = @"[{""meta"":{""account"":""orbitinte"",""event"":""track""},""payload"":{""id"":410827200397312213,""id_str"":""410827200397312213"",""asset"":""359551031717134"",""recorded_at"":""2013-02-07T15:59:04Z"",""received_at"":""2013-02-07T16:00:37Z"",""fields"":{}}},{""meta"":{""account"":""orbitinte"",""event"":""track""},""payload"":{""id"":410827200409895125,""id_str"":""410827200409895125"",""asset"":""359551031717134"",""recorded_at"":""2013-02-07T16:00:04Z"",""received_at"":""2013-02-07T16:00:37Z"",""fields"":{}}}]";
        Data = @"[{""meta"":{""account"":""orbitinte"",""event"":""message""},""payload"":{""id"":410865901198377173,""thread_id"":null,""parent_id"":410865891354345685,""id_str"":""410865901198377173"",""thread_id_str"":"""",""parent_id_str"":""410865891354345685"",""type"":""message"",""channel"":""com.mdi.services.adminProtocol"",""sender"":""359551031717134"",""recipient"":""@@server@@"",""asset"":""359551031717134"",""b64_payload"":""eyJlcnJvciI6ImNhbm5vdCBwYXJzZSBjb21tYW5kIn0="",""recorded_at"":""2013-02-07T18:34:25Z"",""received_at"":""2013-02-07T18:34:24Z""}}]";


        JsonReader Reader = new JsonReader();
        dynamic Output = Reader.Read(Data);

        Notifications oNotifications = new Notifications();
        oNotifications.ProcessNotifications(Data);
    }
}

In the web.config file:

  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
        <providerOption name="CompilerVersion" value="v4.0"/>
        <providerOption name="WarnAsError" value="false"/>
      </compiler>
    </compilers>
  </system.codedom>

I am fairly new to C# and asp.net. But I've been searching for a solution to this problem for a while now. Everything I see mentions the compiler version and the .net framework version. I think I have provided all of the relevant details, but if there is anything else I should add to this question, please let me know.

George Mastros
  • 24,112
  • 4
  • 51
  • 59

2 Answers2

5

Make sure you have a reference to Microsoft.CSharp in your project.

A little more info on this DLL can be found here.

Community
  • 1
  • 1
zimdanen
  • 5,508
  • 7
  • 44
  • 89
  • This is most likely the issue - use of the `dynamic` keyword implicitly relies on this assembly. – JerKimball Feb 07 '13 at 20:31
  • I did not originally have a reference to Microsoft.CSharp in my project. I added it, and it did not help. I also added a "using Microsoft.CSharp" to the code file, and I still get the error. – George Mastros Feb 07 '13 at 20:37
  • I just restarted the IDE hoping it would solve the problem, but it did not. – George Mastros Feb 07 '13 at 20:43
  • 1
    Problem solved. In my web.config file, I was missing the targetFramework="4.0" in the system.web section. Thank you very much for your help. – George Mastros Feb 07 '13 at 21:47
  • @zimdanen, please understand that your advice was necessary in helping me resolve this problem. I wish I could accept both answers, since it was a combination of both that ultimately led to the solution. – George Mastros Feb 07 '13 at 21:58
4

Is your website in IIS configured to use .NET 2.0 ? That's what it sounds like to me. Check the configuration first.. does your test work locally ?

Your compiler should look like this:

<compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">

Yours is set to Version=2.0.0.0

Matthew M.
  • 3,422
  • 4
  • 32
  • 36
  • During runtime, Environment.Version reports 4.0.30319.18034 – George Mastros Feb 07 '13 at 21:05
  • I made the changes you suggested, and it did not make a difference. – George Mastros Feb 07 '13 at 21:05
  • What is the actual error that you're getting when you go to visit the page ? In IIS, under the AppPool section (I think) did you verify that the dropdown is set to .NET 4 ? Are you running this in VS or on IIS ? Are you using VS or IIS express to run ? Did you verify on the Project properties screen that you are in fact targeting .NET 4 ? – Matthew M. Feb 07 '13 at 21:10
  • Hmmm.... This is weird. There are no error messages when I run the page. I did verify that the AppPool is set to 4. I did need to change this, but it is now 4.0. I am trying to run in VS. My desktop machine has Windows 7 Ultimate. When I click Help->About in IIS, it shows "Internet Information Services (Version 7.5.7600.16385). I did verify that I am targeting .NET 4. So it looks like the real problem is that I cannot run the project from within VS because of this error, but the site runs. I really want to be able to debug this, so running in the IDE is necessary. – George Mastros Feb 07 '13 at 21:25
  • 1
    Problem solved. In my web.config file, I was missing the targetFramework="4.0" in the system.web section. Thank you very much for your help. – George Mastros Feb 07 '13 at 21:48
  • Oh great! I knew it had to be some sort of configuration issue.. it's odd that the settings screens were showing that when it wasn't the case. Good luck on your project. – Matthew M. Feb 07 '13 at 22:00