Kategoriler


SON YORUMLAR
mehmet uluğ
bu makalemsi makale! şaka olmalı:)
sevda
beğendim
Reyting Amca
Sitenin reytingi düştü. Google.da aratınca arkalarda çıkıyor. Eskiden ilk sırada çıkardı. Site sahibi ateşli yazılar yazmıyor 1-2 yıldır...
Seda
web sitesi, masa üstü uygulamaları, muhasebe, restaurant, otel, barkot sistemleri yazılımı yapılır. adres:s.sedaerden@gmail.com
Hafize
Yazılım ödevleri ve web sitesi yapılır.Bilgi için hafizearduc@hotmail.com
Gamze
C#, C++, ASP.NET-MVC, HTML-CSS, Matlab, Arduino ödevleri yapılır. Bilgi için; gamze.zirh@gmail.com
Eyüp yılmaz
merve seni seviıyorum asl bebeyoö
BEYZADE BOZTEPE
18 Yaşındayım ınstagram=beyzadebztp
Eyüp yılmaz
39 yasındayım coderım hackerıım ıp verın modemınıze vurayım
BEYZADE BOZTEPE
18 Yaşındayım ınstagram=beyzadebztp (çıkın çıkın gelin GIZLARRRRR)
BEYZADE BOZTEPE
EYVALLAH BENDE İYİYİM
BEYZADE BOZTEPE
İYİYİM GEZEGEN SEN NASILSIN
Eyüp yılmmaz
17 yasındayım kız talıplerımı beklıyom ınsatgram= eyup.yılmaz3
BEYZADE BOZTEPE
MERHABA DÜNYA

Bilgisayar Mühendisleri
Here is the website inspired me to use 
it as a guide when I tried to define 
myself as an engineer candidate a few 
years ago. It really helped me to work
 and study feeling in confidence with 
being on the right way. I suggest this 
website to whom it may direct her/his 
to find the right career path. It 
includes many articles varies from 
real life experiences to detailed 
software engineering issues. But the 
most dignified parts for me are the 
articles in general and career titles.
Son okunan makaleler:
ViewState Compression
ViewState’in Sunucuda Saklanması
Bir bilgisayar mühendisinin bilmesi gereken en temel teknolojiler
Web Servislerinde Parametre Girisinin Gosterilmesi
Asp.Net 2.0 ve Client Script Callback
ASP.NET Web Forms
ASP.NET Introduction: What is ASP
Ajax kullananlar için yükleniyor resimleri
ASP.NET - Database Connection
Ado.Net 2.0 - Connection Pooling' in Önemi
İşsizlik psikolojisi
Bilgisayar Mühendisleri Kaç Para Alır?
ASP.NET - The SortedList Object
Bilgisayar Mühendisi Ne İş yapar? Program Nedir? Çeşitli Sorular?
DataGrid ve GridView Kontrollerinde Görünmez Kolonlar
CSS Dersleri - CSS Nedir
ASP.NET - The ArrayList Object
Installing ASP.NET
Staj Yeri Nasıl Bulunur? Staja hazırlık olarak neler yapmalıyım?
ASP.NET ile GIS Uygulamaları

Bilgisayar Mühendisleri Portalı

ViewState Compression

Compressing and Decompressing Data in Memory

First of all, we need a way to compress and decompress an array of bytes in memory. I put together this simple static class that exposes two methods: Compress and Decompress. The two available classes, GZipStream and DeflateStream, according to MSDN, use the same algorithm, so it's irrelevant which one you choose.

The code below is really simple, and doesn't need further explanation:

Collapse
using System.IO;
using System.IO.Compression;

public static class Compressor {

  public static byte[] Compress(byte[] data) {
    MemoryStream output = new MemoryStream();
    GZipStream gzip = new GZipStream(output, 
                      CompressionMode.Compress, true);
    gzip.Write(data, 0, data.Length);
    gzip.Close();
    return output.ToArray();
  }

  public static byte[] Decompress(byte[] data) {
    MemoryStream input = new MemoryStream();
    input.Write(data, 0, data.Length);
    input.Position = 0;
    GZipStream gzip = new GZipStream(input, 
                      CompressionMode.Decompress, true);
    MemoryStream output = new MemoryStream();
    byte[] buff = new byte[64];
    int read = -1;
    read = gzip.Read(buff, 0, buff.Length);
    while(read > 0) {
      output.Write(buff, 0, read);
      read = gzip.Read(buff, 0, buff.Length);
    }
    gzip.Close();
    return output.ToArray();
  }
}

You need to save this class in a .cs file and put it in the App_Code directory of your ASP.NET application, making sure it's contained in the proper custom namespace (if you don't specify any namespace, the class will be available in the built-in ASP namespace).

Compressing the ViewState

Now, we can actually compress the ViewState of the page. To do that, we have to override the two methods LoadPageStateFromPersistenceMedium and SavePageStateToPersistenceMedium. The code simply uses an additional hidden field, __VSTATE, to store the compressed ViewState. As you can see, by viewing the HTML of the page, the __VIEWSTATE field is empty, while our __VSTATE field contains the compressed ViewState, encoded in Base64. Let's see the code.

Collapse
public partial class MyPage : System.Web.UI.Page {

  protected override object LoadPageStateFromPersistenceMedium() {
    string viewState = Request.Form["__VSTATE"];
    byte[] bytes = Convert.FromBase64String(viewState);
    bytes = Compressor.Decompress(bytes);
    LosFormatter formatter = new LosFormatter();
    return formatter.Deserialize(Convert.ToBase64String(bytes));
  }

  protected override void SavePageStateToPersistenceMedium(object viewState) {
    LosFormatter formatter = new LosFormatter();
    StringWriter writer = new StringWriter();
    formatter.Serialize(writer, viewState);
    string viewStateString = writer.ToString();
    byte[] bytes = Convert.FromBase64String(viewStateString);
    bytes = Compressor.Compress(bytes);
    ClientScript.RegisterHiddenField("__VSTATE", Convert.ToBase64String(bytes));
  }

  // The rest of your code here...
}

In the first method, we just decode from Base64, decompress and deserialize the content of the __VSTATE, and return it to the runtime. In the second method, we perform the opposite operation: serialize, compress, and encode in Base64. The Base64 string is then saved into the __VSTATE hidden field. The LosFormatter object performs the serialization and deserialization tasks.

You may also want to create a new class, for example, CompressedPage, inheriting from System.Web.UI.Page, in which you override the two methods and then inherit your page from that class, for example MyPage : CompressedPage. Just remember that .NET has only single inheritance, and by following this way, you "spend" your only inheritance chance to use the ViewState compression. On the other hand, overriding the two methods in every class is a waste of time, so you have to choose the way that best fits your needs.

Performances and Conclusions

After a few tests, I noticed that the ViewState has been reduced from 38 KB to 17 KB, saving 44%. Supposing you have an average of 1 postback per minute per user, you could save more than 885 MB of bandwidth per month on every single user. That's an excellent result: you save bandwidth (and therefore money), and the user notices a shorter server response time.

I wanted to point out that this solution has a performance hit on the server's hardware. Compressing, decompressing, encoding, and decoding data is quite a heavy work for the server, so you have to balance the number of users with your CPU power and RAM.

Bu makaleyi beğendin mi? Yorumunu Yaz!







Sizden Gelen Yorumlar:

Yorum Yazın




Copyright© 2001-2017. Bilgisayar Mühendisleri Portalı | Bütün hakları saklıdır.