【问题标题】:Object reference not set to an instance of an object in ASP.Net webservice对象引用未设置为 ASP.Net Web 服务中的对象实例
【发布时间】:2013-07-09 10:43:48
【问题描述】:

在我的 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(string 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 的地方)我添加了一个检查,以确保不为空或清空客户端 ID。

 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; } 
    }

【问题讨论】:

  • throw ex; 通常是一种非常糟糕的做法,请考虑切换到throw;
  • ClientStatus 在何处/何时初始化?
  • ClientStatus 可能是这里的问题,但在您的代码中没有指示设置它的位置。 @Andrei 只是补充一点,捕捉Exception 也不是一个好主意......
  • ClientID 是否为空?
  • @Rezoan,不,问题出在其他地方,但无论如何这是一个需要改进的地方。 Read this 了解更多详情。

标签: c# web-services


【解决方案1】:

试试

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;
}

【讨论】:

  • 但我看到发生此异常时 ClientID 不为空或为空。因为我设置了检查日志。 @Eric Herlitz
  • 在设置 ClientID string[] tokens = errorData.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries); if (tokens.Length == 2) { SessionVariables.SetClinetStatus(tokens[0],Convert.ToInt32(tokens[1])); }
  • 可能需要修剪ClientID?,添加ClientID = ClientID.Trim();
  • 还将 ClientStatus 检查添加到 GetClient
  • 在发送 ClientID 表单 ClientEnd 之前,我修剪了这个@Eric Herlitz
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-13
  • 2013-04-19
相关资源
最近更新 更多