I have find a currious situation, that I can't explain. Basicaly property "UserCalls" works fine at the begining, but after long time since start of application (more than a month), property that should never take null value, throws null exception when functions are called on it (eg. linq .FirstOrDefault() ).
Code example:
public class UserCall
{
public long UserID { get; set; }
public long CallID { get; set; }
}
public static class Cache
{
private static List<UserCall> userCalls = new List<UserCall>();
public static List<UserCall> UserCalls
{
get
{
if (userCalls == null)
{
userCalls = new List<UserCall>();
}
return userCalls;
}
set
{
userCalls = value;
}
}
private static void AddCall(long userID, long callID)
{
UserCalls.Add(
new UserCall
{
CallID = callID,
UserID = userID
});
}
public static UserCall GetCall(long userID)
{
var userCall = UserCalls.FirstOrDefault(x => x.UserID == userID);
return userCall;
}
public static void RemoveCall(long userID)
{
UserCalls.RemoveAll(x => x.UserID == userID);
}
}
The following exception is thrown when calling "GetCall":
[ERROR] Object reference not set to an instance of an object. | at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
Methods "AddCall", "GetCall" and "RemoveCall" can be called from multiple threads. This exeception happens at random (but only after long time since service started) on more loaded services. If exception occurs, it is locked in this permanent null state and will not correct itself, only service restart will help. I am aware that this implementation is not exactly thread safe, but still it should never return null. Why does it happen? Have anyone met with similar situation ?