Getting StackOverFlow Exception in ASP.NET C#

0

So I have created an HTTP Handler (.ashx.cs) in c# and I am constantly getting a System.StackOverflowException, it doesn't seem to tell me where the problem lies. I understand that with some google search it tells me theirs too many recursive calls, but I cannot find out exactly where I am doing this.

Here is my Handler Page;

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

On the handler page, this is where I am getting an error;

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

This is where I am calling the Handler;

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

The Exception that I am getting is;

System.StackOverflowException
  HResult=0x800703E9
  Message=Exception of type 'System.StackOverflowException' was thrown.
javascript
c#
jquery
asp.net
ajax
asked on Stack Overflow Nov 13, 2019 by Milan Conhye

2 Answers

2

Let's say:

  1. the StreamMembers contains 5 rows which have same streamId, the id is 1001.

then:

  1. List<StreamUserMenues> menuTree = GetMenuTree(listMenu, "1001");
  2. list.Where(x => x.streamId == 1001) will the get 5 rows.
  3. List = GetMenuTree(list, "1001") will give the 5 rows again and again, endless....

I believe your StreamMember.FKStreamID is a FK which points to StreamMember.StreamMembersID, so your logic should be:

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
    }
}
answered on Stack Overflow Nov 13, 2019 by Dongdong • edited Nov 14, 2019 by Dongdong
0

When you use a recursive function, you have always to be sure to have a base case, which it doesn't see to be the case.

It seems to me that you are just passing the same list and id over and over again, which will cause your program to throw the error.

answered on Stack Overflow Nov 13, 2019 by LCPunch

User contributions licensed under CC BY-SA 3.0