简体   繁体   English

未将对象引用设置为ASP.Net Webservice中的对象实例

[英]Object reference not set to an instance of an object in ASP.Net webservice

In my Asp.Net web service i use below 2 method to change an existing client's status form a global list oblject named ClientStatus here. 在我的Asp.Net Web服务中,我使用下面的2种方法来更改一个现有客户端的状态,这是在这里命名为ClientStatus的全局列表的。 This global list is modified from several client side but in a safe way (lock). 此全局列表是从多个客户端修改的,但是是以安全的方式(锁定)进行的。

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

I used below code to pass the value to SetClinetStatus(string ClientID, int clinetstatus) method 我使用下面的代码将值传递给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]));
    }

But sometimes (not every times) i get 但是有时候(不是每次)我都会

Object reference not set to an instance of an object 你调用的对象是空的

form 形成

activeClient = GetClient(ClientID);

i don't understand why it is happening and don't see any problem there. 我不明白为什么会这样,也看不到任何问题。

does anyone see any problem there that is responsible for such kind of exception. 没有人看到那里的任何问题导致这种异常。

EDIT 编辑

In the global list i only add client through below method and here clientID will come from direct webservice method. 在全局列表中,我仅通过以下方法添加客户端,此处的clientID将来自直接的Webservice方法。 and in another end (from where the client ID comes) i added a check for not to null or empty the clientID. 在另一端(来自客户端ID的地方),我添加了一项检查,以确保不将null或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);
            }

        }

and the ActiveClient class structure is 并且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; } 
    }

Try 尝试

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

}

You should also ensure the clientID is in the ClientStatus 您还应该确保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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM