【发布时间】:2013-05-09 10:16:16
【问题描述】:
我有一门课,它从气象局或气象局收集 7 天的预报数据并将其呈现在网页上。脚本每 30 分钟运行一次,以从局获取更新数据。
该局以制表符分隔格式提供数据,并带有标题行。提取所有字段后,我将值放入Dictionary<string,string> 进行解析。出于显而易见的原因,组织数据的重要字段是“forecast_date”。所以在我开始解析之前,我要确保我的字典确实包含这个键。
这是我正在做的一个非常简化的示例:
static object ForecastLockingObj= new object();
private void UpdateWeather()
{
if(isTimeForUpdate())
{
lock(ForecastLockingObj)
{
if(isTimeForUpdate())
{
Dictionary<string, string> ForecastData = Get7DayForecast();
int forecastDate = int.MinValue;
if (ForecastData.ContainsKey("forecast_date") && int.TryParse(ForecastData["forecast_date"], out forecastDate))
{
//Parse the data
SetNextUpdateTime();
}
}
}
}
}
这实际上在大多数情况下都有效。但我偶尔会遇到以下异常:
[KeyNotFoundException: The given key was not present in the dictionary.]
System.ThrowHelper.ThrowKeyNotFoundException() +28
System.Collections.Generic.Dictionary`2.get_Item(TKey key) +7457036
CoA.WebUI.Controls.WeatherWidget.UpdateWeather() in C:\dev\WeatherWidget.cs:231
第 231 行是 if 语句,它检查“forecast_date”是否存在,然后尝试将其解析为整数。笔记;该服务可靠地将日期呈现为整数(例如 20130515),因此这更像是一种健全性检查。
ContainsKey 不应该抛出这个异常,所以我觉得它一定是我在 TryParse 中引用 ForecastData["forecast_date"] 的地方。
我的问题是这样的;当然,如果 ContainsKey 返回 false,则 TryParse 不应该运行。那么为什么它会在一个语句中报告一个键的存在,然后在下一个语句中否认它的存在......而我们在一个锁中并且我们正在处理的字典是非静态的和本地的?
顺便说一句;这通常发生在下午,当无线电通信局发布下一个长期预测时。少数页面加载后会发生异常,然后自行调整。
这是完整的 Get7DayForecast 方法
private Dictionary<string, string> Get7DayForecast()
{
int linenumber = 0;
int locationNameKey = 0;
List<string> keys = new List<string>();
Dictionary<string, string> myLocationData = new Dictionary<string, string>();
FtpWebRequest ftp = (FtpWebRequest)WebRequest.Create(ForecastURL);
ftp.Method = WebRequestMethods.Ftp.DownloadFile;
ftp.Credentials = new NetworkCredential();
FtpWebResponse ftp_response = (FtpWebResponse)ftp.GetResponse();
if (ftp_response.WelcomeMessage.StartsWith("230") && ftp_response.StatusDescription.StartsWith("150"))
{
Stream ftp_responseStream = ftp_response.GetResponseStream();
StreamReader ftp_reader = new StreamReader(ftp_responseStream);
while (ftp_reader.Peek() >= 0)
{
linenumber++;
string line = ftp_reader.ReadLine();
List<string> temp = (List<string>)line.Split(ForecastDelimiter).ToList<string>();
if (linenumber == 1)
{
//Break if the header line does not contain the fields we require
if (!ForecastRequiredFields.All(line.Contains)) { break; }
keys = temp;
locationNameKey = keys.IndexOf(ForecastLocationFieldName);
}
else if (temp.Count == keys.Count && temp[locationNameKey] == ForecastLocationName)
{
for (int i = 0; i < keys.Count; i++)
{
myLocationData.Add(keys[i], temp[i]);
}
//Break if we've just parsed the data we were looking for
break;
}
}
ftp_reader.Close();
}
ftp_response.Close();
return myLocationData;
}
【问题讨论】:
-
在锁中定义字典是否重要?我认为在锁中定义的任何东西都是线程安全的。
-
通过
lock声明,我假设您正在使用多个线程。没有其他方法从Get7DayForecast返回的同一对象中排除itens? -
好吧,
Get7DayForecast只用在那里,它返回的对象在其他地方没有用吗? (你可以有两个变量指向同一个对象)。 -
该锁将保护多个线程对 Get7DayForecast 返回的 ForecastData 字典的访问,只要访问该对象的任何其他代码也使用相同的锁。 Get7DayForecast 是做什么的?
-
堆栈跟踪行号可能是错误的。异常可能发生在另一行。仔细查看您按键访问字典的每一行,并考虑它是否可能引发异常。
标签: c# .net exception dictionary