WSE 2.0 Progress Bars That Work

Ite's been a while since I posted some amazingly useful, but insanely impossible to find code for you. But, I am still alive and kicking and working on all sorts of interesting projects, so it was only a matter of time until I ran into another Microsoft oversight that needed a workaround. If you have ever tried to implement a progress bar with WSE 2.0 in your application, it probably wasn't a fun experience. In fact, if you were using something like DIME, you probably gave up. Yes, theoretically, there are ways to use the DimeReader / DimeWriter to do the task, but that requires a lot of extra work and doesn't fit nearly as nice and neat into the framework as the auto-generated WSE classes that you are used to. Personally, I like using the WSE auto-generated code, but I was amazed to discover that there is absolutely no way built-in for you to obtain progress information while a large DIME message is being streamed to the server. What is worse is that due to the way that WSE was built, you cannot even plug into WSE to provide this functionality yourself... well, at least that is what everyone was saying. But, as is often said, "Where there is a will, there is a way." Today, ladies and gentlemen, I present you with the way.

1) Derive a class from the WSE generated stub and insert the following code:

 

protected override System.Net.WebRequest GetWebRequest(Uri uri) 
{
   SoapWebRequest wr = (SoapWebRequest)base.GetWebRequest(uri);
   Type t = wr.GetType();
   FieldInfo fi = t.GetField("_request", BindingFlags.Instance | BindingFlags.NonPublic); 
   HttpWebRequest h = (HttpWebRequest)WebRequest.Create(uri);
   CustomWebRequest cwr = new CustomWebRequest(h);
   cwr.StreamAcquired+=new StreamHandler(cwr_StreamAcquired);
   fi.SetValue(wr, cwr); return wr;
} 
public event StreamHandler StreamAcquired; 
private void cwr_StreamAcquired(object sender, Stream s)
{
   if(StreamAcquired != null)
   {
      StreamAcquired(sender, s);
   }
}
2) Add the following class to your solution:
   public delegate void StreamHandler(object sender, Stream s); 
   public class CustomWebRequest : WebRequest 
   {
      public CustomWebRequest(WebRequest wrapped)
      {
         _wrapped = wrapped;
      }
      WebRequest _wrapped;
      public override void Abort()
      {
         _wrapped.Abort ();
      }
      public override IAsyncResult BeginGetResponse(AsyncCallback callback, object state) { return _wrapped.BeginGetResponse (callback, state); }
      public override string ConnectionGroupName { get { return _wrapped.ConnectionGroupName; } set { _wrapped.ConnectionGroupName = value; } }
      public override string ContentType { get { return _wrapped.ContentType; } set { _wrapped.ContentType = value; } }
      public override long ContentLength { get { return _wrapped.ContentLength; } set { _wrapped.ContentLength = value; } }
      public override IAsyncResult BeginGetRequestStream(AsyncCallback callback, object state) { return _wrapped.BeginGetRequestStream (callback, state); }
      public override ICredentials Credentials { get { return _wrapped.Credentials; } set { _wrapped.Credentials = value; } }
      public override Stream EndGetRequestStream(IAsyncResult asyncResult) { return _wrapped.EndGetRequestStream (asyncResult); }
      public override WebResponse EndGetResponse(IAsyncResult asyncResult) { return _wrapped.EndGetResponse (asyncResult); }
      MemoryStream _dummyStream;
      public override Stream GetRequestStream()
      {
         _dummyStream = new MemoryStream();
         return _dummyStream;
      }
      int _bytesWritten = 0;
      public int BytesWritten
      {
         get { return _bytesWritten; }
      }
      int _totalBytes = 0;
      public int TotalBytes { get { return _totalBytes; } }
      public override WebResponse GetResponse()
      {
         byte[] data = _dummyStream.ToArray();
         ((HttpWebRequest)_wrapped).AllowWriteStreamBuffering = false; 
         ((HttpWebRequest)_wrapped).ContentLength = data.Length;

         Stream s = _wrapped.GetRequestStream ();
         try
         {
            if(StreamAcquired != null) StreamAcquired(this, s);
            int chunkSize = 4092;
            _totalBytes = data.Length;



            for(int i = 0; i < data.Length; )
            {
               int len = Math.Min(chunkSize, data.Length-(i));
               s.Write(data, i, len);
               i+=len; _bytesWritten+=len;
            }
            return _wrapped.GetResponse ();
         }
         finally
         {
            s.Close();

         }
      } 
      public override WebHeaderCollection Headers { get { return _wrapped.Headers; } set { _wrapped.Headers = value; } }
      public override string Method { get { return _wrapped.Method; } set { _wrapped.Method = value; } }
      public override bool PreAuthenticate { get { return _wrapped.PreAuthenticate; } set { _wrapped.PreAuthenticate = value; } }
      public override IWebProxy Proxy { get { return _wrapped.Proxy; } set { _wrapped.Proxy = value; } }
      public override Uri RequestUri { get { return _wrapped.RequestUri; } }
      public override int Timeout { get { return _wrapped.Timeout; } set { _wrapped.Timeout = value; } }
      
      public event StreamHandler StreamAcquired; 
      private void cwr_StreamAcquired(object sender, Stream s)
      {
         if(StreamAcquired != null)
         {
            StreamAcquired(sender, s);
         }
      }
   }
3) In your calling form / dialog / whatever, attach to the StreamAcquired member of your new class before calling. The sender will contain your "CustomWebRequest" object, which will provide you with progress while the request is being sent to the server. Add some thready goodness and you will have nice updating progress bars to keep your users entertained while they wait.

3 Comments

  • Yeah, apparently the skin isn't liking the PRE tag very much... I had to manually insert breaks to make it look half-way decent.

  • It's been a while since you posted anything at all! I know it's hard to find time for blogging but you're one of my favorite voices. Come on back!

  • [URL=http://www.poscali.cn/tim-cellulari] tim cellulari [/URL] tim cellulari [URL=http://www.poscali.cn/carta-geografica] carta geografica [/URL] carta geografica [URL=http://www.poscali.cn/simona-ventura-nuda] simona ventura nuda [/URL] simona ventura nuda [URL=http://www.poscali.cn/test-concorso] test concorso [/URL] test concorso [URL=http://www.poscali.cn/filmati-porno] filmati porno [/URL] filmati porno [URL=http://www.poscali.cn/calciatori-nudi] calciatori nudi [/URL] calciatori nudi [URL=http://www.poscali.cn/ciaoamigos-it] ciaoamigos it [/URL] ciaoamigos it [URL=http://www.poscali.cn/ranma-1-2] ranma 1 2 [/URL] ranma 1 2 [URL=http://www.poscali.cn/www-film-pornogratis-donne-mature-it] www film pornogratis donne mature it [/URL] www film pornogratis donne mature it [URL=http://www.poscali.cn/italiacercami-com] italiacercami com [/URL] italiacercami com [URL=http://www.poscali.cn/google-hearth] google hearth [/URL] google hearth [URL=http://www.poscali.cn/filmati-di-sesso] filmati di sesso [/URL] filmati di sesso [URL=http://www.poscali.cn/alessandra-mastronardi-nuda] alessandra mastronardi nuda [/URL] alessandra mastronardi nuda [URL=http://www.poscali.cn/www-giochi-gratis-it] www giochi gratis it [/URL] www giochi gratis it [URL=http://www.poscali.cn/google-fr] google fr [/URL] google fr [URL=http://www.poscali.cn/valentino-rossi-shop] valentino rossi shop [/URL] valentino rossi shop [URL=http://www.poscali.cn/trenitalia-it] trenitalia it [/URL] trenitalia it [URL=http://www.poscali.cn/incontri-italia] incontri italia [/URL] incontri italia [URL=http://www.poscali.cn/giocare-online] giocare online [/URL] giocare online [URL=http://www.poscali.cn/roma-calzature] roma calzature [/URL] roma calzature [URL=http://www.poscali.cn/pics-porno] pics porno [/URL] pics porno [URL=http://www.poscali.cn/racconti-cuckold] racconti cuckold [/URL] racconti cuckold [URL=http://www.poscali.cn/audio-divx] audio divx [/URL] audio divx [URL=http://www.poscali.cn/www-eva-henger-it] www eva henger it [/URL] www eva henger it [URL=http://www.poscali.cn/italia-chiamami-com] italia chiamami com [/URL] italia chiamami com [URL=http://www.poscali.cn/futurama-xxx] futurama xxx [/URL] futurama xxx [URL=http://www.poscali.cn/www-cartoon-network-com] www cartoon network com [/URL] www cartoon network com [URL=http://www.poscali.cn/donne-che-cagano] donne che cagano [/URL] donne che cagano [URL=http://www.poscali.cn/video-pompino] video pompino [/URL] video pompino [URL=http://www.poscali.cn/escort-forum-it] escort forum it [/URL] escort forum it [URL=http://www.poscali.cn/geogle-earth] geogle earth [/URL] geogle earth [URL=http://www.poscali.cn/www-lady-sonia-com] www lady sonia com [/URL] www lady sonia com [URL=http://www.poscali.cn/www-mediaset-premium-it] www mediaset premium it [/URL] www mediaset premium it [URL=http://www.poscali.cn/castello-di] castello di [/URL] castello di [URL=http://www.poscali.cn/player-wmv] player wmv [/URL] player wmv [URL=http://www.poscali.cn/plug-in-nero] plug in nero [/URL] plug in nero [URL=http://www.poscali.cn/sex-chat] sex chat [/URL] sex chat [URL=http://www.poscali.cn/kiss-the-girl-ashley-tisdale] kiss the girl ashley tisdale [/URL] kiss the girl ashley tisdale [URL=http://www.poscali.cn/video-porno-sesso-gratis-gay] video porno sesso gratis gay [/URL] video porno sesso gratis gay [URL=http://www.poscali.cn/teen-sex] teen sex [/URL] teen sex [URL=http://www.poscali.cn/iomilu-com] iomilu com [/URL] iomilu com [URL=http://www.poscali.cn/ranma-hentay] ranma hentay [/URL] ranma hentay [URL=http://www.poscali.cn/nomi] nomi [/URL] nomi [URL=http://www.poscali.cn/naked-girls] naked girls [/URL] naked girls [URL=http://www.poscali.cn/ampland-it] ampland it [/URL] ampland it [URL=http://www.poscali.cn/www-calendari-it] www calendari it [/URL] www calendari it [URL=http://www.poscali.cn/sesso-donna-matura] sesso donna matura [/URL] sesso donna matura [URL=http://www.poscali.cn/gogle-it] gogle it [/URL] gogle it [URL=http://www.poscali.cn/arcaton-com] arcaton com [/URL] arcaton com [URL=http://www.poscali.cn/latino-online] latino online [/URL] latino online [URL=http://www.poscali.cn/wwww-google] wwww google [/URL] wwww google [URL=http://www.poscali.cn/mondo-convenienza] mondo convenienza [/URL] mondo convenienza [URL=http://www.poscali.cn/sito-porno-gratis] sito porno gratis [/URL] sito porno gratis [URL=http://www.poscali.cn/infermiere-porche] infermiere porche [/URL] infermiere porche [URL=http://www.poscali.cn/italiacercami] italiacercami [/URL] italiacercami [URL=http://www.poscali.cn/www-goole-com] www goole com [/URL] www goole com [URL=http://www.poscali.cn/playstation-gratis] playstation gratis [/URL] playstation gratis [URL=http://www.poscali.cn/traduzioni-on-line] traduzioni on line [/URL] traduzioni on line [URL=http://www.poscali.cn/film-porno] film porno [/URL] film porno [URL=http://www.poscali.cn/gioco-scaricare] gioco scaricare [/URL] gioco scaricare

Comments have been disabled for this content.