所以我在c#中创建了一个HTTP处理程序(.ashx.cs),并且不断收到System.StackOverflowException,它似乎并没有告诉我问题出在哪里。我了解到,通过某些Google搜索,它可以告诉我他们太多的递归调用,但是我无法确切知道我在哪里进行此操作。
这是我的处理程序页面;
public void ProcessRequest(HttpContext context)
{
int userID;
userID = Convert.ToInt32(HttpContext.Current.Session["UserID"]);
string streamHash;
streamHash = HttpContext.Current.Session["Stream"].ToString();
int streamID;
streamID = GetStreamID(streamHash);
List<StreamUserMenues> listMenu = new List<StreamUserMenues>();
using (DBEntities contextDB = new DBEntities())
{
var userStreamMenuList = contextDB.StreamMembers.ToList();
foreach (var listItem in userStreamMenuList)
{
StreamUserMenues streamUserMenu = new StreamUserMenues();
streamUserMenu.Id = listItem.StreamMembersID;
streamUserMenu.streamId = listItem.FKStreamID;
streamUserMenu.UserId = listItem.FKUserID;
streamUserMenu.UserNameText = listItem.FKUserID.ToString();
//streamUserMenu.isApproved = (bool) listItem.isUserApproved;
//streamUserMenu.isUserGuest = (bool) listItem.isGuest;
//streamUserMenu.isOnline = (bool) listItem.isUserOnline;
listMenu.Add(streamUserMenu);
}
}
List<StreamUserMenues> menuTree = GetMenuTree(listMenu, streamID);
JavaScriptSerializer js = new JavaScriptSerializer();
context.Response.Write(js.Serialize(menuTree));
}
在处理程序页面上,这是我收到错误消息的地方;
private List<StreamUserMenues> GetMenuTree(List<StreamUserMenues> list, int streamID)
{
return list.Where(x => x.streamId == streamID).Select(x => new StreamUserMenues()
{
Id = x.Id,
streamId = x.streamId,
UserId = x.UserId,
UserNameText = x.UserId.ToString(),
//isApproved = x.isApproved,
//isUserGuest = x.isUserGuest,
//isOnline = x.isOnline,
List = GetMenuTree(list, x.streamId)
}).ToList();
这是我叫处理程序的地方;
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
url: 'StreamUserMenuHandler.ashx',
method: 'get',
dataType: 'json',
success: function (data) {
buildMenu($('#menu'), data);
$('#menu').menu();
},
error: function (err) {
alert(err.statusText);
}
});
function buildMenu(parent, items) {
$.each(items, function () {
//var li = $("<li><a href=" + window.location.href + "?StreamHash=" + this.URL + ">" + this.MenuText + "</a></li>");
//var myLink = '<li><a href="' + window.location.href + "'";
//myLink += '?StreamHash=';
var li = $("<li>" + this.UserNameText + "</li>");
if (!this.Active) {
li.addClass('ui-state-disabled');
}
li.appendTo(parent);
if (this.List && this.List.length > 0) {
var ul = $("<ul></ul>");
ul.appendTo(li);
buildMenu(ul, this.List);
}
});
}
});
</script>
我得到的例外是;
System.StackOverflowException
HResult=0x800703E9
Message=Exception of type 'System.StackOverflowException' was thrown.
答案 0 :(得分:2)
让我们说:
StreamMembers
包含5行,这些行具有相同的streamId
,id为1001
。然后:
List<StreamUserMenues> menuTree = GetMenuTree(listMenu, "1001")
; list.Where(x => x.streamId == 1001)
将获得5行。List = GetMenuTree(list, "1001")
将一次又一次地给这5行,无休止.... 我相信您的StreamMember.FKStreamID
是指向StreamMember.StreamMembersID
的FK,因此您的逻辑应该是:
private List<StreamUserMenues> GetMenuTree(List<StreamUserMenues> list, int streamID)
{
return list.Where(x => x.streamId == streamID).Select(x => new StreamUserMenues()
{
.....
List = GetMenuTree(list, x.Id) //be aware: it's x.Id, not x.streamId
}
}
答案 1 :(得分:0)
在使用递归函数时,必须始终确保有一个基本案例,但事实并非如此。
在我看来,您只是一次又一次地传递相同的列表和ID,这将导致您的程序抛出错误。