To Zip the messages in Silverlight 4 & WCF

ב-SL4 מיקרוסופט הוסיפה את יכולות ההרחבה ל-SL4. כמו למשל הממשקים : IServiceBehavior , IContractBehavior & IOperationBehvior  כלמור אנחנו מתחילים לקבל כמעט את כל מה שאנחנו מכירים מ-WCF דוט-נט.

בעקבות התוספות האלו כתבתי ZipMessageInspector, השתמשתי ב – ComponentOne Zip library. בשורה התחתונה קבלנו שיפור ענק בביצועים.

הקוד ב-SL:

Code Snippet
  1. public class ZipClientMessageInspector : IClientMessageInspector
  2.     {
  3.         public void AfterReceiveReply( ref Message reply , object correlationState )
  4.         {
  5.             //TODO: UnZip by Header.
  6.             var reader = reply.GetBody<Byte[]>();
  7.             using ( var ms  = new MemoryStream( reader ) )
  8.             using ( var zip = new C1ZStreamReader( ms ) )
  9.             using ( var sr  = new StreamReader( zip ) )
  10.             {
  11.                 var result = sr.ReadToEnd();
  12.                 var xml = XElement.Parse( result );
  13.                 reply = Message.CreateMessage(
  14.                     reply.Version ,
  15.                     reply.Headers.Action ,
  16.                     xml
  17.                     );
  18.             }
  19.         }
  20.         public object BeforeSendRequest( ref Message request , IClientChannel channel )
  21.         {
  22.             //TODO: Zip by Header.
  23.             var reader = request.GetBody<XElement>()
  24.                             .ToString(SaveOptions.DisableFormatting);
  25.             using ( var ms = new MemoryStream() )
  26.             using ( var zip = new C1ZStreamWriter( ms ) )
  27.             using ( var writer = new StreamWriter( zip ) )
  28.             {
  29.                 writer.Write( reader );
  30.                 writer.Flush();
  31.                 var zipresult = ms.ToArray();
  32.                 request = Message.CreateMessage(
  33.                     request.Version ,
  34.                     request.Headers.Action ,
  35.                     zipresult
  36.                     );
  37.             }
  38.             return null;
  39.         }
  40.     }

הקוד ב-צד השרת:

Code Snippet
  1. public class ZipDispatchMessageInspector : IDispatchMessageInspector
  2.     {
  3.         public object AfterReceiveRequest( ref Message request , IClientChannel channel , InstanceContext instanceContext )
  4.         {
  5.            //TODO: UnZip by Header.
  6.             var reader = request.GetBody<byte[]>();
  7.             using( var ms  = new MemoryStream( reader ) )
  8.             using( var zip = new C1ZStreamReader( ms ) )
  9.             using( var sr  = new StreamReader( zip ) )
  10.             {
  11.                 var result = sr.ReadToEnd();
  12.                 var xml = XElement.Parse(result);
  13.                 request = Message.CreateMessage(
  14.                     request.Version,
  15.                     request.Headers.Action,
  16.                     xml
  17.                     );
  18.             }
  19.             return null;
  20.         }
  21.         public void BeforeSendReply( ref Message reply , object correlationState )
  22.         {
  23.             //TODO: Zip by Header.
  24.             var reader  = reply.GetBody<XElement>().ToString(SaveOptions.DisableFormatting);
  25.             using ( var ms = new MemoryStream() )
  26.             using ( var zip = new C1ZStreamWriter( ms ) )
  27.             using ( var sw = new StreamWriter( zip ) )
  28.             {
  29.                 sw.Write( reader );
  30.                 sw.Flush();
  31.                 var zipresult = ms.ToArray();
  32.                 reply = Message.CreateMessage(
  33.                                 reply.Version ,
  34.                                 reply.Headers.Action ,
  35.                                 zipresult
  36.                                 );
  37.             }
  38.         }
  39.     }

ניתן לשפר ולהוסיף עוד את ה-ZIP אך זה לפוסט הבא.

10 Comments

Comments have been disabled for this content.