.net困惑已经的bug,当大量用户访问时才出现

baya 2014-10-23 09:02:34
程序一切正常,发布到正式环境也正常,可是运行一段时间,可能是人多的时候,对象就不存在了。需要重启应用程序池才好。这是什么原因呢? 这究竟是程序问题?还是web府二期问题?请高手指教!

日志信息:未将对象引用设置到对象的实例。在 Hyun.WebService.MainWebService.<>c__DisplayClass2.<HasPermission>b__0(VRolePermissionList rp)
在 Hyun.Entities.ListBase`1.ApplyFilter(Predicate`1 match)
在 Hyun.WebService.MainWebService.HasPermission(Result r)


程序代码
/// <summary>
/// 所有角色权限列表
/// </summary>
private static VList<VRolePermissionList> rolePermissionList = new VRolePermissionListService().GetAll();

/// <summary>
/// 用户权限判断
/// </summary>
/// <param name="funIdCode">功能ID、或功能Code</param>
/// <returns>true:有权,false:无权</returns>
[WebMethod(EnableSession = true)]
private bool HasPermission(Result r)
{


//用户、角色 页面权限验证
string url = HttpContext.Current.Request.RawUrl;
url = url.Substring(url.LastIndexOf("/") + 1);

if (VUser.IsUser)
{
if (rolePermissionList == null || rolePermissionList.Count <= 0)
{
rolePermissionList = new VRolePermissionListService().GetAll();
}

try
{
rolePermissionList.RemoveFilter();
}
catch (Exception e)
{
FileLog.LogMessage("rolePermissionList.RemoveFilter():" + e.ToString(), Constant.ERROR_LOG_FILE_PATH);
}

try
{
rolePermissionList.ApplyFilter(delegate(VRolePermissionList rp)
{
return VUser.IsRole(rp.RoleId) && string.Equals(rp.Path, url, StringComparison.OrdinalIgnoreCase);
});
}
catch (Exception e)
{
FileLog.LogMessage("rolePermissionList.ApplyFilter:" + e.ToString(), Constant.ERROR_LOG_FILE_PATH);
}

if (rolePermissionList.Count > 0)
{
return true;
}
}

r.flag = -2;
r.msg = string.Format("没有对{0}的权限", url);
return false;
}


...全文
209 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
宝_爸 2014-10-23
  • 打赏
  • 举报
回复
List<>在msdn中对于thread safety的描述 It is safe to perform multiple read operations on a List<T>, but issues can occur if the collection is modified while it’s being read. To ensure thread safety, lock the collection during a read or write operation. To enable a collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization. For collections with built-in synchronization, see the classes in the System.Collections.Concurrent namespace. For an inherently thread–safe alternative, see the ImmutableList class. 来自: http://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx
宝_爸 2014-10-23
  • 打赏
  • 举报
回复
看你用了static,static整个进程只有一份,是不是并发造成的。试一试写入static 容器的时候用lock锁定一下,确保只有一个线程在同一个时间可以写入。 重启application pool就是重启asp.net的进程。
  • 打赏
  • 举报
回复
你这程序,不是仅仅在没有读取过或者内容为空时才读取一次数据、以后不再读取嘛!不用去找“对象保存在哪里”的问题。 另外,asp.net程序随时会重启,会经常造成Session、Application、static变量引用的对象的“丢失”现象。如果想缓存数据,就真正学会使用Cache,了解Cache数据随时可能丢失的事实。如果你不是想真正缓存对象,以为static引用的对象不丢失,那么就写c/s程序,而不是asp.net程序。
baya 2014-10-23
  • 打赏
  • 举报
回复
014-10-23 09:08:54-rolePermissionList.RemoveFilter():System.ArgumentOutOfRangeException: 索引必须位于该列表的界限内。 参数名: index 在 System.Collections.Generic.List`1.Insert(Int32 index, T item) 在 Hyun.Entities.ListBase`1.ApplyFilter() 在 Hyun.WebService.MainWebService.HasPermission(Result r) ----------------------- 2014-10-23 09:08:54-rolePermissionList.ApplyFilter:System.NullReferenceException: 未将对象引用设置到对象的实例。 在 Hyun.WebService.MainWebService.<>c__DisplayClass2.<HasPermission>b__0(VRolePermissionList rp) 在 Hyun.Entities.ListBase`1.ApplyFilter(Predicate`1 match) 在 Hyun.WebService.MainWebService.HasPermission(Result r)
baya 2014-10-23
  • 打赏
  • 举报
回复
程序说明:先把所有权限读出来缓存到内存中,然后与每个用户的权限比较,如果权限表中存在,则有权限。另外还有一个问题:我每次重新设置权限之后,必须重启应用程序池,才能更新新的权限设置。static 变量只存在应用程序池中?
夏至雨后 2014-10-23
  • 打赏
  • 举报
回复
项目中尽量不写静态变量
三楼の郎 2014-10-23
  • 打赏
  • 举报
回复
web.config里面SessionState的Mode设置的是Inproc吧,改成StateServer试试,当然之前得把Asp.Net状态服务启动起来。
Rajesh_James 2014-10-23
  • 打赏
  • 举报
回复
Session保存信息容易丢失,建议使用Application Cache

62,039

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧