Sunday, September 7, 2008

Improve ASP.net response time

-         View State

o       Reduce the use of view state as much as possible.

o       If need to store objects in view state consider using type convertors.

o       Move view state to the bottom of the page.

o   Compress View State.

-         Java Script and CSS

o       Try to use ToolkitScriptManager instead of ScriptManager if using ASP.net AJAX Control Toolkit. It has the ability to combine Toolkit scripts together to reduce number of request.

o       Use release mode in production for ToolkitScriptManager to get already compact ASP.net AJAX Control Toolkit script files.

o       Combine custom java script and CSS files into one file.

o       Cache CSS and Java Script in the browser.

o       Compact Java Script and CSS to reduce size.

-         Images

o       Cache Images in browser.

-         Cache

o       Cache data and page output. (Note: ASP.net cache is not a distributed cache and will not work in a Web farm. Consider using memcached and memcached providers.)

Saturday, September 6, 2008

Creating an Alert System with NHibernate, WCF and MSMQ

For a project that I am working on we needed to create an alert system. This system alerts the user about certain changes in the database via email or text. When I started googling for an alert system, nothing relevant came up. But then trying to solve another problem I came across two very interesting posts.

- Manuel Abadia's ASP.NET stuff - NHibernate and calculated properties
- SOA'izing MSMQ with WCF (and Why It's Worth It)

Using these two posts we were able to create a subscriber based alert system. For our solution we created a marker interface IAlert and have all domain objects we needed to alert on inherit from this interface. Instead of using the FindDirty method as mentioned in the first post, we used OnSave [for Insert], OnFlushDirty [for Update] and OnDelete [for delete]. In these functions we place a check if the object is of type IAlert we converted its current and previous state if any into an xml packet. Then that packet was saved on the HttpContext.Current.Items collection. The reason for saving it on the Items collection is that transaction could fail and we don’t want to generate alerts for failed transactions. Then in the PostFlush method in the interceptor we called the WCF service that places that xml packet on to the MSMQ queue. This operation is asynchronous and does not add time to the transaction. On the other side we wrote a WCF service that processes that packet. This way, alert was sent asynchronously and transaction time was not effected. Another advantage of using this approach is processing WCF service has all the information needed to process that alert in the xml packet and only needs to hit that database when it needs to gets that list of subscribers to send messages.