Yesterday at work, I came across the situation where I had inserted a breakpoint to debug some code inside my global.asax file in a web application, but the VS debugger never reaches it.
The first indication of trouble came when some data values loaded during application start never changed, even after I manually changed the database records. After a clean/build in VS, the values changed to what I expected, and debugging the global.asax file became possible. But the next debug run show the values never change, and the code in global.asax is never reached. The initial thought that came to mind was that global.asax, where the application start events are fired, was acting very funny.
Reading up on this issue on the net brought up the overlooked fact that Cassini, the lightweight web server distributed with ASP.Net, does not close down after it has initially started, in order to increase performance the next time the application is run. This means that the application start events do not fire until the next time the web server process is restarted.
In essence, the problem I was facing was that each time I try to debug my global.asax file, if the application start events have already been fired and loaded, the code in the global.asax is skipped. Why it works when I do a clean/build is that cassini has to restart after each build. The direct workaround this is to go to the system tray, right-click on the cassini icon and select stop. Now the debugger can reach breakpoints inside global.asax next time the application is run.
Rather than manually killing the process each time you stop debugging, you can use the edit and continue option in VS. Enabling edit and continue does not kill the cassini process, but restarts it each time you debug the application. The option can be enabled in VS 2010 by selecting ‘debug’ in menu bar, and then option and settings:
If you are using IIS (as I was) to debug the web application, killing the cassini process will not help. In this case, you have to start the IIS manager, and check which application pool your web application has been assigned to. Once you know the application pool, right clicking it and selecting advanced settings brings up the window shown below:
In this case, the idle time-out sets the time limit for the worker process to shut down. Changing the time to zero would disable the setting, and each time requests come in, the web application would load its application settings anew. If you do not want to change the setting, the alternative would be to restart the application pool by right clicking it, selecting stop, and again selecting start. But you would have to do this after each debug run, so disabling it by entering zero for the idle time-out would be the better option.
This in essence, is the activities you have to go through in order to debug global.asax files. I welcome any suggestions and ideas on the readers’ part.
XStream is a very simple and lightweight library that I came across when developing my final year project at University. It can be used for serializing objects into an XML string and vice-versa. XStream also supports JSON output as of the latest release. No object code has to be modified, and the library is a very simple one to use. A necessary requirement in my project was to serialize an object to XML and persist the string in an SQL server. When needed, the string would be retrieved and converted back to the specific object it represented.
For this purpose, I had two methods, one to serialize objects into XML and another to reconstruct the object from the XML. These methods are shown below (The XStream JAR file has to be referenced in your project. In Netbeans, you would need to right click the libraries package under your project, and choose ‘Add JAR’ in order to select the XStream JAR file):
public static String ObjectToXml(Object obj)
XStream xstream = new XStream();
xmlStr = xstream.toXML(obj);
public static Object XmlToObject(String xmlEncoded)
XStream xT = new XStream();
recreated = xT.fromXML(xmlEncoded);
As can be seen above, the library is a very simple one to use, and is a good option if all you need to do is juggle between objects and XML strings in a very simple manner. The XStream library is open source, and can be downloaded from http://xstream.codehaus.org/.
Hi, and welcome to Forays in software development. My name is Mark Sinnathamby, and I live in Sri Lanka. I graduated from the University of Westminster, majoring in software engineering, and am currently a developer at Eurocenter DDC.
This site contains blogs and articles on various topics, many of them to do with my experience in software development and tools, in and out of the industry. Please share your thoughts and comments by replying to blog postings or by mailing me at email@example.com. Happy coding!
Subscribe to Forays in software development by Email