在C#

时间:2017-09-06 07:22:55

标签: c# .net visual-studio

我每天运行一个程序,转到API,以JSON格式获取一堆天气数据并将其存储在数据库中。它已经好几个月了,但从昨天开始,它已经失败了。我试图调试,无法弄清楚发生了什么。我得到一个有效的响应,格式与以前一样,并将其记录在控制台中或在断点处查看它,似乎没问题。但是,我得到了这个例外:

  

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:无法执行   对空引用的运行时绑定          在CallSite.Target(Closure,CallSite,Object)          在System.Dynamic.UpdateDelegates.UpdateAndExecute1 [T0,TRet](CallSite)   网站,T0 arg0)          在CallSite.Target(Closure,CallSite,Object)          在DbMgr.DarkSky.d__9.MoveNext()在C:\ Users \ BaruchKogan \ documents \ visual studio 2017 \ Projects       \ DbMgr \ DbMgr \ DarkSky.cs:第434行       pausedSystem.AggregateException:发生一个或多个错误。 ---> Microsoft.CSharp.RuntimeBinder.RuntimeBinderException       :无法对空引用执行运行时绑定          在DbMgr.DarkSky.d__9.MoveNext()在C:\ Users \ BaruchKogan \ documents \ visual studio 2017 \ Projects       \ DbMgr \ DbMgr \ DarkSky.cs:第461行          ---内部异常堆栈跟踪结束---          在System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)          在System.Threading.Tasks.Task 1.GetResultCore(Boolean waitCompletionNotification) at System.Threading.Tasks.Task 1.get_Result()          在DbMgr.DarkSky.d__6.MoveNext()在C:\ Users \ BaruchKogan \ documents \ visual studio 2017 \ Projects \ DbMgr \ DbMgr       \ DarkSky.cs:第205行       ---> (内部异常#0)Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:无法执行   nul上的运行时绑定       我参考          在DbMgr.DarkSky.d__9.MoveNext()在C:\ Users \ BaruchKogan \ documents \ visual studio 2017 \ Projects       \ DbMgr \ DbMgr \ DarkSky.cs:第461行< ---

违规代码在这里:

Console.WriteLine(responseJson.daily.data[0]);
JObject darkSkyDaily = responseJson.daily.data[0];
if (DailyBreakdown(darkSkyDaily, plot, blobUri, false).Result != 0)
{
    JArray darkSkyHourlies = responseJson.hourly.data;
    if (darkSkyHourlies.Count > 0)
    {
       foreach (JObject item in darkSkyHourlies)
        {
            await HourlyBreakdown(item, plot.ID, false);
        }
    }
}

第435行是第一行,运行正常,并将有效的对象字符串写入控制台。

在这种特殊情况下,DailyBreakdown()返回0,因为每日都在数据库中。

static async Task<int> DailyBreakdown(JObject daily, GeographicalPlot plot, String blobUri, Boolean isForecast)
        {
            using (var newEntities = new atpLabsDbEntities1())
            {
                try
                {
                    DateTime dailyTime = new DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(Convert.ToDouble((int)daily["time"]));
                    var testDailies = from c in newEntities.WeatherDailies
                                      where c.GeographicalPlotID == plot.ID && c.Time == dailyTime
                                      select c;
                    if ((testDailies.Count() == 0) || (isForecast == true))
                    {
                        string summary = (string)daily["summary"];
                        int sunsetTime = (int)daily["sunsetTime"];
                        int sunriseTime = (int)daily["sunriseTime"];
                        double? moonPhase = (double?)daily["moonPhase"];
                        string precipType = (string)daily["precipType"];
                        double? temperatureMin = (double?)daily["temperatureMin"];
                        double? temperatureMax = (double?)daily["temperatureMax"];
                        double? apparentTemperatureMin = (double?)daily["apparentTemperatureMin"];
                        double? apparentTemperatureMax = (double?)daily["apparentTemperatureMax"];
                        int temperatureMinTime = (int)daily["temperatureMinTime"];
                        int temperatureMaxTime = (int)daily["temperatureMaxTime"];
                        int apparentTemperatureMinTime = (int)daily["apparentTemperatureMinTime"];
                        int apparentTemperatureMaxTime = (int)daily["apparentTemperatureMaxTime"];
                        double? dewPoint = (double?)daily["dewPoint"];
                        double? humidity = (double?)daily["humidity"];
                        double? windSpeed = (double?)daily["windSpeed"];
                        double? windBearing = (double?)daily["windBearing"];
                        double? visibility = (double?)daily["visibility"];
                        double? cloudCover = (double?)daily["cloudCover"];
                        double? pressure = (double?)daily["pressure"];
                        dynamic newWeatherDaily;
                        if (isForecast == true)
                        {
                            newWeatherDaily = new WeatherDailyForecast();
                            newWeatherDaily.ForecastTime = DateTime.Now;
                        }
                        else
                        {
                            newWeatherDaily = new WeatherDaily();
                        }
                        newWeatherDaily.GeographicalPlotID = plot.ID;
                        newWeatherDaily.Time = dailyTime;
                        newWeatherDaily.Summary = summary;
                        newWeatherDaily.SunriseTime = new DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(Convert.ToDouble(sunriseTime));
                        newWeatherDaily.MoonPhase = moonPhase;
                        newWeatherDaily.SunsetTime = new DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(Convert.ToDouble(sunsetTime));
                        newWeatherDaily.PrecipType = precipType;
                        newWeatherDaily.TemperatureMin = temperatureMin;
                        newWeatherDaily.TemperatureMax = temperatureMax;
                        newWeatherDaily.ApparentTemperatureMax = apparentTemperatureMax;
                        newWeatherDaily.ApparentTemperatureMin = apparentTemperatureMin;
                        newWeatherDaily.TemperatureMinTime = new DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(Convert.ToDouble(temperatureMinTime));
                        newWeatherDaily.TemperatureMaxTime = new DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(Convert.ToDouble(temperatureMaxTime));
                        newWeatherDaily.ApparentTemperatureMinTime = new DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(Convert.ToDouble(apparentTemperatureMinTime));
                        newWeatherDaily.ApparentTemperatureMaxTime = new DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(Convert.ToDouble(apparentTemperatureMaxTime));
                        newWeatherDaily.DewPoint = dewPoint;
                        newWeatherDaily.Humidity = humidity;
                        newWeatherDaily.WindBearing = windBearing;
                        newWeatherDaily.WindSpeed = windSpeed;
                        newWeatherDaily.Visibility = visibility;
                        newWeatherDaily.Pressure = pressure;
                        newWeatherDaily.CloudCover = cloudCover;
                        newWeatherDaily.BlobUri = blobUri;
                        if (isForecast == true)
                        {
                            newEntities.WeatherDailyForecasts.Add(newWeatherDaily);
                        }
                        else
                        {
                            newEntities.WeatherDailies.Add(newWeatherDaily);
                        }
                        int result = await newEntities.SaveChangesAsync();
                        return result;
                    }
                    else
                    {
                        return 0;
                    }

                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw e;
                }
            }
        } 

我摧毁了我的大脑,无法弄清楚哪条线实际上引发了异常,或者如何解释这一点。有什么想法吗?

0 个答案:

没有答案