Determine which objects in ASP.NET are used in the session.

I inherited a very large ASP.NET application that needs to be modified to use State Server instead of in-proc sessions. I need to keep track of all the classes used in a session throughout the application, and then determine if they can be serialized. Are there any tools you can use to analyze the code to determine the classes used in the session?

+4
source share
2 answers

You can quickly get a β€œsnapshot” of the current session objects using the following code (C #):

BinaryFormatter b = new BinaryFormatter(); StringBuilder debug = new StringBuilder(); MemoryStream m; foreach (String s in Session.Keys) { // try to serialize the object var obj = Session[s]; if (obj != null) { m = new MemoryStream(); try { b.Serialize(m, obj); debug.AppendFormat("{0}: {1} bytes\n", s, m.Length); } catch (Exception ex) { debug.AppendFormat("ERROR: {0}, Message: {1}\n", s, ex.Message); } } } 

Then display the string "debug" in your favorite text container.

This is useful because simply switching to State Manager / SQL Server will really tell you that it cannot be serialized, but it will stop at the first non-serializable object. He also will not tell you which key "owns" this object.

Code adapted from this stop request response .

+1
source

In Visual Studio, you can perform a wildcard search and look for a template such as Session [* =.

Another option is to use the Immediate window during debugging and request that it is in a session state after navigating your site. Not a complete proof, but it can already help.

Another method would be to simply set the session state in Sql Server or the state manager mode (outside the process), debug it and see where it starts complaining if objects cannot be serialized. At the same time, you also thoroughly tested your application.

+1
source

All Articles