View all Session content?

0 views
Skip to first unread message

Joshua Russo

unread,
May 13, 2008, 12:15:29 PM5/13/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Ok, this seems like it should be really easy and I'm sure there's a
simpler way to do this. I just want to get an overview of the current
values in Session. When I'm debugging it's very obscure where the
actual list of values are in the Session instance. So, I just wanted
to make a simple method to create a string of key value pairs.

public static string GetSessionContent(HttpSessionState a_session)
{
StringBuilder retVal = new StringBuilder();
foreach (KeyValuePair<object, object> elm in a_session)
{
if (retVal.Length > 0)
retVal.Append("\n");
retVal.Append(elm.Key).Append(": ").Append(elm.Value);
}
return retVal.ToString();
}

My problem is that it tells me that KeyValuePair<object, object> is an
invalid cast for session state.

How do I do this?

Josh

Cerebrus

unread,
May 13, 2008, 1:08:24 PM5/13/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
IMO, the cast is not valid because :

1. The NVC stores it's keys as strings, not objects.
2. The NVC can store multiple string values per key.
3. There is no direct mapping between a NameValuePair and the
NVC.Item.

You could use a KeyValuePair<T, T> if you were iterating through a
Dictionary object.

Why not simply use something like :

----
StringBuilder retVal = new StringBuilder();
foreach (string key in Session.AllKeys)
{
retVal.Append(key).Append(": ").Append(Session[key].ToString());
}
return retVal.ToString();
----

Andrew Badera

unread,
May 13, 2008, 1:13:12 PM5/13/08
to DotNetDe...@googlegroups.com

Joshua Russo

unread,
May 13, 2008, 3:08:59 PM5/13/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
What do you concider an ungodly amount and what's considered best
practice when you find the number of session variables growing?


On May 13, 1:13 pm, "Andrew Badera" <and...@badera.us> wrote:
> But the real question is, why are you storing an ungodly amount of data in
> the session?
>
> On Tue, May 13, 2008 at 12:15 PM, Joshua Russo <joshua.rupp...@gmail.com>
> wrote:
>
>
>
>
>
> > Ok, this seems like it should be really easy and I'm sure there's a
> > simpler way to do this. I just want to get an overview of the current
> > values in Session. When I'm debugging it's very obscure where the
> > actual list of values are in the Session instance. So, I just wanted
> > to make a simple method to create a string of key value pairs.
>
> >    public static string GetSessionContent(HttpSessionState a_session)
> >    {
> >        StringBuilder retVal = new StringBuilder();
> >        foreach (KeyValuePair<object, object> elm in a_session)
> >        {
> >            if (retVal.Length > 0)
> >                retVal.Append("\n");
> >            retVal.Append(elm.Key).Append(": ").Append(elm.Value);
> >        }
> >        return retVal.ToString();
> >    }
>
> > My problem is that it tells me that KeyValuePair<object, object> is an
> > invalid cast for session state.
>
> > How do I do this?
>
> > Josh
>
> --
> --
> --Andy Baderahttp://higherefficiency.nethttp://flipbitsnotburgers.blogspot.com/http://andrew.badera.us/http://changeroundup.com/

Joshua Russo

unread,
May 13, 2008, 3:09:23 PM5/13/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Ah, Ok thanks

Crisatunity (blog.crisatunity.com)

unread,
May 14, 2008, 1:47:02 PM5/14/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Not speaking for Andy - but I think this is one of those "it depends"
answers. It depends if your application is a department intranet
application with 50 total users or a public internet application with
thousands. It depends whether the development team is a one-man-shop,
small team or massive distributed team.

My personal best practices on using them is: less is better. Beyond a
handful (maybe 3) my code becomes less clear, more obfuscated. Also,
their use is inherently anti-REST and REST is usually a bread and
butter architecture for me.

If my web application can without them, I don't use them.
> > --Andy Baderahttp://higherefficiency.nethttp://flipbitsnotburgers.blogspot.com/htt...
> > and...@badera.us
> > (518) 641-1280
> > Google me:http://www.google.com/search?q=andrew+badera- Hide quoted text -
>
> - Show quoted text -

Andrew Badera

unread,
May 14, 2008, 2:16:47 PM5/14/08
to DotNetDe...@googlegroups.com
If you have so many session params that you need to enumerate them in callouts, rather than simply depend on visualizers, you might have too many session params.

I try to use absolutely as few as possible. Use ViewState, use Session, but use them wisely! There's almost never a need to put a lot of stuff in Session, especially with Context.Cache available.
Reply all
Reply to author
Forward
0 new messages