0

Maybe this is a stupid issue or question, but I'm facing it since this morning. I am writing a windows service in C#. In the timer-elapsed event handler I make a request to a Web API that returns me a Json, like this:

 void OnTimerElapsed(object sender, ElapsedEventArgs e)
    {
        List<Anomalia> anomalie = null;
        TimeIntervalConfigurationModel timeConf = new XmlConfigurationReader.XmlConfigurationHelper().GetTimeIntervalConfiguration();
        if (CanNotify(timeConf))
        {
            try
            {
                //ricerca anomalie
                eventLog.WriteEntry("Contact service: ricerca anomalie....");
                WebRequest requestUrl = WebRequest.Create(urlConfig.BaseUrl + urlConfig.Anomalia);
                Stream objStream = requestUrl.GetResponse().GetResponseStream();
                StreamReader objReader = new StreamReader(objStream);
                String sLine = "";
                sLine = objReader.ReadToEnd();
                anomalie = JsonConvert.DeserializeObject<List<Anomalia>>(sLine);
                //cut
            }
            catch (Exception ex)
            {
                //cut
            }
        }
    }

Now I am facing a very strange behavior: if I keep the JsonConvert.DeserializeObject>(sLine); instruction the handler method does not execute, if I remove it the methods fires normally. This issue is not due to data format: if I try to deserialize an empty string, or an empty json array, the method does not execute.

  • What is the input of sLine? – Bauss Sep 29 '15 at 10:40
  • "[{\"IdAnomalia\":1,\"DataAnomalia\":\"2015-09-18T00:00:00\",\"DataSegnalazione\":\"2015-09-18T15:27:38.85\",\"DataPresaVisione\":\"0001-01-01T00:00:00\",\"DataRisoluzione\":\"0001-01-01T00:00:00\",\"Task\":{\"IdTask\":1,..... And so on. I can't post it all, there is reserved data in it. – Pierpaolo Paris Sep 29 '15 at 10:48
  • Sounds like a problem related to references: if I go in the immediate window, and try to write "JsonConvert...." the namespace cannot be found. – Pierpaolo Paris Sep 29 '15 at 10:56
  • You need the full namespace in the Immediate window. Is the Newtonsoft.Json.dll also deployed? Any messages in the event log? – CodeCaster Sep 29 '15 at 12:03
  • The dll is deployed. And in the immediate window I receive "The name Newtonsoft does not exist in the current context" (or something like this, I just translated from italian) if I use the full namespace – Pierpaolo Paris Sep 29 '15 at 12:17

1 Answers1

0

Just found the solution. The problem was that I installed the Nuget package from Solution->Manage Nuget Packages from solution. Before that, the package was installed directly via Nuget. Opening the packages.config file for the project I found this line:

<package id="Newtonsoft.Json" version="4.5.11" targetFramework="net40-Client" />

After removing it, and reinstalling the package via Nuget I found this line in packages.config

<package id="Newtonsoft.Json" version="7.0.1" targetFramework="net40-Client" />

Clearly, the problem was dued to the wrong version number. I found the solution after reading this answer: https://stackoverflow.com/a/12027715/1334525

Community
  • 1
  • 1