在我的Asp.Net网络服务中,我使用以下2方法将现有客户端的状态更改为此处名为 ClientStatus 的全局列表项目。这个全局列表是从几个客户端修改的,但是以安全的方式(锁定)。
private static List<ActiveClient> ClientStatus = new List<ActiveClient>();
public static void SetClinetStatus(string ClientID, int clinetstatus)
{
ActiveClient activeClient=null;
try
{
activeClient = GetClient(ClientID);
if (activeClient != null)
{
activeClient.statuschanged = true;
activeClient.status = clinetstatus;
}
}
catch (Exception ex)
{
WebserviceLog.Debug(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType + "::" + System.Reflection.MethodBase.GetCurrentMethod().ToString() + ":" + ex.Message);
}
}
public static ActiveClient GetClient(string clientID)
{
ActiveClient activeClient = null;
try
{
lock (ClientStatus)
{
activeClient = ClientStatus.Find(c => c.clinetID == clientID);
}
}
catch (Exception ex)
{
throw ex;
}
return activeClient;
}
我使用下面的代码将值传递给 SetClinetStatus(字符串ClientID,int clinetstatus)方法
string errorData = Encoding.Default.GetString(data);
string[] tokens = errorData.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
if (tokens.Length == 2)
{
SessionVariables.SetClinetStatus(tokens[0],Convert.ToInt32(tokens[1]));
}
但有时(不是每次都)我得到
对象引用未设置为对象的实例
表格
activeClient = GetClient(ClientID);
我不明白它为什么会发生,也没有看到任何问题。
是否有人看到任何导致此类异常的问题。
修改
在全局列表中,我只通过以下方法添加客户端,此处clientID将来自直接webservice方法。在另一端(从客户端ID到来的地方)我添加了一个检查,以便不为null或清空clientID。
public static void AddClient(string clientID)
{
try
{
lock (ClientStatus)
{
ClientStatus.Add(new ActiveClient { clinetID = clientID });
}
}
catch (Exception ex)
{
WebserviceLog.Debug(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType + "::" + System.Reflection.MethodBase.GetCurrentMethod().ToString() + ":" + ex.Message);
}
}
并且ActiveClient
类结构是
public class ActiveClient
{
public ActiveClient()
{
clinetID = string.Empty;
status = 0;
statuschanged = false;
}
public string clinetID { get; set; }
public int status { get; set; }
public bool statuschanged { get; set; }
}
答案 0 :(得分:0)
尝试
public static void SetClinetStatus(string ClientID, int clinetstatus)
{
ClientID = ClientID.Trim();
// Cannot run unless there is a ClientID submitted
if(string.IsNullOrEmpty(ClientID))
{
// Log handling of event
return;
}
ActiveClient activeClient=null;
try
{
activeClient = GetClient(ClientID);
if (activeClient != null)
{
activeClient.statuschanged = true;
activeClient.status = clinetstatus;
}
}
catch (Exception ex)
{
WebserviceLog.Debug(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType + "::" + System.Reflection.MethodBase.GetCurrentMethod().ToString() + ":" + ex.Message);
}
}
您还应该确保clientID在ClientStatus中
public static ActiveClient GetClient(string clientID)
{
// Cannot continue without a ClientStatus
if(ClientStatus == null)
{
return null;
}
ActiveClient activeClient = null;
try
{
lock (ClientStatus)
{
// Test if there are any matching elements
if(ClientStatus.Any(c => c.clinetID == clientID))
{
activeClient = ClientStatus.Find(c => c.clinetID != null && c.clinetID == clientID);
}
}
}
catch (Exception ex)
{
throw ex;
}
// This will return null if there are no matching elements
return activeClient;
}