Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0
This is the forum to discuss the Wrox book ASP.NET 2.0 Website Programming: Problem - Design - Solution by Marco Bellinaso; ISBN: 9780764584640
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 14th, 2007, 09:47 AM
Friend of Wrox
 
Join Date: Mar 2007
Location: Creetown, UK
Posts: 488
Thanks: 2
Thanked 11 Times in 10 Posts
Default Compressed Viewstate PLUS HttpCache

This is an extention of the article that i posted here: http://p2p.wrox.com/topic.asp?TOPIC_ID=60283

regarding compressing the viewstate globally across your site. i've since had a little brainwave on how to change a few lines of code in the BasePage.cs file to make it work completely server side using the HttpRuntime.Cache.

ok - as an academic excerise, thought id try the minimal approach once more but this time use the server cache and guid's. seems to work just as well, not sure about the server load but the client only ever now receives a guid key. anyway, only about 3 lines of code changed from previous version (new/amended lines in blue):

BasePage.cs
namespace MB.TheBeerHouse.UI
{
    public class BasePage : System.Web.UI.Page
    {
        // added to compress viewstate also in globals
        protected override object LoadPageStateFromPersistenceMedium()
        {
            string viewState = Request.Form["__VSTATE"];
            //byte[] bytes = Convert.FromBase64String(viewState);
            // if using client vstate, unrem line above and rem out line below
            if (HttpRuntime.Cache[viewState] != null)
            {
                byte[] bytes = (byte[])HttpRuntime.Cache[viewState];
                bytes = Globals.Decompress(bytes);
                LosFormatter formatter = new LosFormatter();
                HttpRuntime.Cache.Remove(viewState);
                return formatter.Deserialize(Convert.ToBase64String(bytes ));
            }
            else
            {
                return null;
            }
        }

        protected override void SavePageStateToPersistenceMedium(object viewState)
        {
            string viewStateGuid = Guid.NewGuid().ToString();
            LosFormatter formatter = new LosFormatter();
            StringWriter writer = new StringWriter();
            formatter.Serialize(writer, viewState);
            string viewStateString = writer.ToString();
            byte[] bytes = Convert.FromBase64String(viewStateString);
            bytes = Globals.Compress(bytes);
            //ClientScript.RegisterHiddenField("__VSTATE", Convert.ToBase64String(bytes));
            // if using line above (to use client vstate), rem out bottom two lines
            HttpRuntime.Cache.Insert(viewStateGuid, bytes, null,
                                             DateTime.Now.AddMinutes(10),
                                             System.Web.Caching.Cache.NoSlidingExpiration);
            ClientScript.RegisterHiddenField("__VSTATE", viewStateGuid);
        }
        // end viewstate additions

I think this one is the 'winner' in my book - looking fwd to comments on it's shortcomings tho' (i.e. should i be using Session[viewStateGuid] or HttpRuntime.Cache[viewStateGuid]). i'm experimenting with both the Session object and the HttpRuntime.Cache to see what differences i can find. i'm certain that i should be using the Session object after reading references to webfarms etc... however, i beleive that the session objects don't expire, so was worried about the implications on that front. of course, i suppose the session 'dies' when the client closes or timesout.

i also have noticed that the SavePageStateToPersistenceMedium is called far more than the LoadPageStateFromPersistenceMedium (which only get's called on a postback), so altho' i'm setting the cache to expire after 10 mins, there will be quite a few cache items that will never be used. food for thought anyway


jimi

http://www.jamestollan.com
__________________
jimi

http://www.originaltalent.com
  #2 (permalink)  
Old May 14th, 2007, 02:45 PM
Friend of Wrox
 
Join Date: Mar 2007
Location: Creetown, UK
Posts: 488
Thanks: 2
Thanked 11 Times in 10 Posts
Default

just found something very similar here:

http://www.codeproject.com/aspnet/hm...&forumid=23359

jimi

http://www.jamestollan.com
  #3 (permalink)  
Old May 14th, 2007, 09:51 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Atlanta, Georgia, USA.
Posts: 917
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The cache method is quick, but unworkable in a server farm where the user might hit a different server on his next request.

The session state can be used with a server farm, but this is going to be quite slow if out-of-process session state is used (as needed by a farm).

For a busy server, I still like the idea of passing it as compressed baggage with the page.

You may also want to consider how well this may work in an AJAX environment. It might work just fine? You'd need to avoid compressing anything that will be used directly on the client.

Eric

  #4 (permalink)  
Old May 15th, 2007, 02:54 AM
Friend of Wrox
 
Join Date: Mar 2007
Location: Creetown, UK
Posts: 488
Thanks: 2
Thanked 11 Times in 10 Posts
Default

yes - i've decided that i don't like the session/cache method after all - think i'll stick with the compression method as this seems to offer the best bang for buck and is still inherently simple.

jimi

http://www.jamestollan.com
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
WS client sending compressed request? wakeup .NET Web Services 0 October 25th, 2007 04:37 AM
convert any file into Compressed file neer .NET Framework 2.0 6 September 5th, 2005 05:29 AM
Compressed XML in DOM Sam_saha XML 2 April 11th, 2004 06:18 PM
Zip a file from using Win XP compressed folder Mitch VBScript 1 January 6th, 2004 08:58 AM



All times are GMT -4. The time now is 09:22 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.