【发布时间】:2015-05-07 22:51:33
【问题描述】:
我有一个非常奇怪的行为,如下图所示:
正如您在 Watch 窗口中看到的,所有可能是 null 的东西都不是 null。
这里是函数的完整代码:
public LogInTokenCache GetUserIdFromToken(string token)
{
LogInTokenCache item = null;
if (this.TokenCache.ContainsKey(token))
{
item = this.TokenCache[token];
if (item.ExpirationTime < DateTime.Now)
{
this.TokenCache.Remove(item.Token);
return null;
}
}
else
{
LogInTokenBusiness tokenBusiness = new LogInTokenBusiness();
var entity = tokenBusiness.FindToken(token);
if (entity != null && entity.Token != null)
{
item = new LogInTokenCache()
{
Token = entity.Token,
UserID = entity.UserId,
ExpirationTime = entity.ExpirationTime,
};
this.TokenCache.Add(item.Token, item);
}
}
return item;
}
我使用了 Find All References 功能,这是我唯一使用构造函数和声明的地方(我将它用于整个 Web 应用程序):
public class IdentityController : Controller
{
private static EmailAdpater EmailAdapter = new EmailAdpater();
private static UserIdentityTokenShortener TokenShortener = new UserIdentityTokenShortener();
public static LoginTokenManager LoginTokenManager = new LoginTokenManager();
...
有人遇到过这个问题吗?我做错了什么?
编辑:添加了 StackTrace 和详细信息 EDIT2:编辑标题,以便将来的人可以搜索此主题,以防遇到同样的问题。
at System.Collections.Generic.Dictionary12.Insert(TKey key, TValue value, Boolean add)
at System.Collections.Generic.Dictionary12.Add(TKey key, TValue value)
at MobileDatingAPI.Models.LoginTokenManager.GetUserIdFromToken(String token) in d:\FPT University\Capstone 2\TFSRepo\Projects\MobileDatingAPI\MobileDatingAPI\Models\LoginTokenManager.cs:line 48
at MobileDatingAPI.Models.Utils.GetUserFromTokenID(Controller controller, String token, BaseApiViewModels model) in d:\FPT University\Capstone 2\TFSRepo\Projects\MobileDatingAPI\MobileDatingAPI\Models\Utils\Utils.cs:line 68
at MobileDatingAPI.Controllers.CommunityController.UpdateActivity(String token, Nullable11 longitude, Nullable11 latitude) in d:\FPTUniversity\Capstone 2\TFSRepo\Projects\MobileDatingAPI\MobileDatingAPI\Controllers\CommunityController.cs:line 84
at lambda_method(Closure , ControllerBase , Object[] )
at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary12 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary12 parameters)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.ActionInvocation.InvokeSynchronousActionMethod()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult12.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.Async.AsyncResultWrapper.End[TResult](IAsyncResult asyncResult, Object tag)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()
【问题讨论】:
-
点击“查看详情”,发布完整的堆栈跟踪。
-
还有……你使用多线程吗?
-
我们无法用这段代码重现它。
-
我也在考虑“检查堆栈跟踪”,但这听起来像是 Intellitrace 的工作。
-
@DatVM:10 次中有 9 次会遇到并发问题。选择答案并使用
ConcurrentDictionary并注意该课程中的任何其他成员。它也可能存在并发问题。
标签: c# .net dictionary asp.net-mvc-5 nullreferenceexception