C# “Asp.Net Web服务”;索引超出范围。必须为非负数且小于集合的大小。参数名称:index";
我知道stackoverflow中存在这个问题。但我的似乎不一样。 我看没什么问题。但它有时发生在运行时 我得到的例外情况: “System.Collections.Generic.List`1[System.String]GetTemplateAndPicture(System.String): 索引超出范围。必须为非负且小于集合的大小。 参数名称:索引“ 这是我的代码:有人能看一下并告诉我发生了什么吗C# “Asp.Net Web服务”;索引超出范围。必须为非负数且小于集合的大小。参数名称:index";,c#,web-services,C#,Web Services,我知道stackoverflow中存在这个问题。但我的似乎不一样。 我看没什么问题。但它有时发生在运行时 我得到的例外情况: “System.Collections.Generic.List`1[System.String]GetTemplateAndPicture(System.String): 索引超出范围。必须为非负且小于集合的大小。 参数名称:索引“ 这是我的代码:有人能看一下并告诉我发生了什么吗 public static List<string> GetTemplateA
public static List<string> GetTemplateAndPicture(string sessionID)
{
List<string> data = new List<string>();
try
{
// get the session data from the list.
SessionData sData = null;
try
{
//sData = SessionDataList.Find(p => p.SessionID.Equals(sessionID));
foreach (SessionData sessiondata in SessionDataList.ToList<SessionData>())
{
if (sessiondata != null && !string.IsNullOrEmpty(sessiondata.SessionID))
{
if (sessiondata.SessionID.Equals(sessionID))
{
sData = sessiondata;
break;
}
}
}
}
catch (Exception ex)
{
RightPatientRemoteWebserviceLog.Debug(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType + "::" + System.Reflection.MethodBase.GetCurrentMethod().ToString() + ":" + ex.Message);
}
// get template data from session data
string templateData = (sData == null) ? string.Empty : sData.TemplateData;
// get picture data from session data
string pictureData = (sData == null) ? string.Empty : sData.PictureData;
string errorCode = (sData == null) ? string.Empty : sData.ErrorCode;
// remove the session data from the list. no more usage with this data.
if (sData != null && SessionDataList.Count>0)
SessionDataList.Remove(sData);
// create a list for sending.
data.Add(templateData);
data.Add(pictureData);
data.Add(errorCode);
return data;
}
catch (Exception ex)
{
RightPatientRemoteWebserviceLog.Debug(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType + "::" + System.Reflection.MethodBase.GetCurrentMethod().ToString() + ":" + ex.Message);
}
return data;
}
公共静态列表GetTemplateAndPicture(字符串sessionID)
{
列表数据=新列表();
尝试
{
//从列表中获取会话数据。
SessionData-sData=null;
尝试
{
//sData=SessionDataList.Find(p=>p.SessionID.Equals(SessionID));
foreach(SessionData.ToList()中的SessionData SessionData)
{
if(sessiondata!=null&&!string.IsNullOrEmpty(sessiondata.SessionID))
{
if(sessiondata.SessionID.Equals(SessionID))
{
sData=会话数据;
打破
}
}
}
}
捕获(例外情况除外)
{
RightPatientRemoteWebserviceLog.Debug(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType+”:“+System.Reflection.MethodBase.GetCurrentMethod().ToString()+”:“+ex.Message);
}
//从会话数据获取模板数据
string templateData=(sData==null)?string.Empty:sData.templateData;
//从会话数据中获取图片数据
string pictureData=(sData==null)?string.Empty:sData.pictureData;
string errorCode=(sData==null)?string.Empty:sData.errorCode;
//从列表中删除会话数据。不再使用此数据。
if(sData!=null&&SessionDataList.Count>0)
SessionDataList.Remove(sData);
//创建要发送的列表。
添加(模板数据);
数据。添加(图片数据);
添加数据(错误代码);
返回数据;
}
捕获(例外情况除外)
{
RightPatientRemoteWebserviceLog.Debug(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType+”:“+System.Reflection.MethodBase.GetCurrentMethod().ToString()+”:“+ex.Message);
}
返回数据;
}
您使用的全局列表似乎不是线程安全的。因为您在最后一条评论中说“列表对象将保存来自其他几个客户端的数据”。因此,当你试图从列表中删除一个项目时,如果他们同时试图修改列表,那么它将崩溃。因此,为了线程安全,只需将全局列表修改为private并将其设置为public方法。一个用于向列表中添加项目,另一个用于从列表中删除项目。当然,不要忘记锁定SessionDataList的全局列表
因此,当您需要向表单列表添加或删除项时,只需使用下面指定的两个公共方法
代码片段如下所示:
private static SessionDatalist<SessionData> SessionDataList=new SessionDatalist<SessionData>();
public addSessionData()
{
lock(SessionDataList)
{
//add list item here to the SessionDataList
}
}
public removeSessionData()
{
lock(SessionDataList)
{
//remove item from SessionDataList
}
}
private static SessionDatalist SessionDatalist=new SessionDatalist();
公共addSessionData()
{
锁(SessionDataList)
{
//将列表项添加到SessionDataList
}
}
public removeSessionData()
{
锁(SessionDataList)
{
//从SessionDataList中删除项
}
}
希望这有帮助。您最好跟踪
ex.ToString()
,这将为您提供堆栈跟踪,帮助您找到有问题的行。如果您在调试模式下编译,跟踪将包括引发异常的行号。异常应发生在SessionDataList.Remove(sData)中;或者foreach(SessionDataList.ToList()中的SessionData SessionData)我很确定。但我无法处理@你在用线吗?在其他地方,线程正在与全局SessionDataList发生冲突?它有时会在运行时与SessionDataList.Remove(sData)结合使用代码>提示线程问题。不,我没有在这里使用线程。但当我为这位会期专家做每一件事的时候。在这段时间内,它可以从任何其他方法更新。所以在本例中,为了防止出现异常,我在foreach中使用.toList()对其进行修改。你可以看到@史蒂文:谢谢你。我正在尝试你的建议,并让你知道更新。