ky us

ky us

  • NA
  • 1
  • 0

GZIP soapextension

May 23 2008 9:13 AM

I am working on soapextension that compresses soap message.
I am stuck a only one stage now. When I get response from webservice....on client at -BeforeDeserialize stage when I try to copy original or old stream to temp stream so that I can de compress it I get Stream dose not support seek operation.

Has any one come across smilar issue ...that while altering stream you could not capture it properly when client recieves it from server

debugging

This code will create new file for input and output each stage. You can see that till that point message is getting compressed and de compressed properly.      Sort file by Date created

 

 

 

using System;

using System.Collections.Generic;

using System.Text;

using System.Web;

using System.Web.Services;

using System.Web.Services.Protocols;

using System.IO;

using System.IO.Compression;

using System.Xml;

using System.Threading;

namespace GZIPSoapExtension

{

public class GZIPSoapExtension : SoapExtension

{

protected Stream OriginalStream = null;

protected Stream NewStream = null;

protected String Mode = null;

public override object GetInitializer(Type WebServiceType)

{

return "C:\\" + WebServiceType.FullName + ".log";

}

public override object GetInitializer(LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute)

{

return null;

}

public override void Initialize(object initializer)

{

}

public override Stream ChainStream(Stream stream)

{

OriginalStream = stream;

NewStream = new MemoryStream();

return NewStream;

}

public override void ProcessMessage(SoapMessage message)

{

Mode = "";

switch (message.Stage)

{

case SoapMessageStage.BeforeSerialize:

Mode = "BeforeSerialize";

break;

case SoapMessageStage.AfterSerialize:

Mode = "AfterSerialize";

AfterSerializeHandler(message);

break;

case SoapMessageStage.BeforeDeserialize:

Mode = "BeforeDeserialize";

BeforeDeserializeHandler(message);

break;

case SoapMessageStage.AfterDeserialize:

Mode = "AfterDeserialize";

break;


}

}

public void BeforeDeserializeHandler(SoapMessage message)

{


WriteToLog("BeforeDeserializeHandler -- BeforeDecompress", message, OriginalStream);

Stream ms = new MemoryStream ();


Copy(OriginalStream, ms);

ms.Position = 0;

ms = DeCompressData(ms);

ms.Position = 0;

NewStream.Position = 0;

Copy(ms, NewStream);

ms.Position = 0;

NewStream.Position = 0;

WriteToLog("BeforeDeserializeHandler -- After Decompress", message, NewStream);

ms.Position = 0;

NewStream.Position = 0;

}

public void AfterSerializeHandler(SoapMessage message)

{


WriteToLog("AfterSerializeHandler -- before Compress" ,message, NewStream);

MemoryStream ms = new MemoryStream();

NewStream.Position = 0;

Copy(NewStream, ms);

NewStream.Position = 0;

ms.Position = 0;

ms= CompressData(ms);

ms.Position = 0;

NewStream.Position = 0;

Copy(ms, OriginalStream);

ms.Position = 0;

WriteToLog("AfterSerializeHandler -- After Compress", message, ms);


}

protected void Copy(Stream from, Stream to)

{

from.Position = 0;

int bytesRead;

byte[] buffer = new byte[2];

BinaryReader reader = new BinaryReader(from);

BinaryWriter writer = new BinaryWriter(to);

do

{

bytesRead = reader.Read(buffer, 0, buffer.Length);

writer.Write(buffer, 0, bytesRead);

} while (bytesRead > 0);

writer.Flush();

}

public MemoryStream CompressData(MemoryStream source)

{

if (source == null) return null;

MemoryStream ms = new MemoryStream();

GZipStream compressedzipStream = null;

try

{

byte[] buffer = new byte[source.Length];

source.Position = 0;

source.Read(buffer, 0, buffer.Length);

compressedzipStream = new GZipStream(ms, CompressionMode.Compress, true);

compressedzipStream.Write(buffer, 0, buffer.Length);

}

finally

{

compressedzipStream.Close();

}

return ms;

}

public MemoryStream DeCompressData(Stream s)

{

if (s == null) return null;

s.Position = 0;

GZipStream gs = new GZipStream(s, CompressionMode.Decompress);

BinaryReader reader = new BinaryReader(gs);

MemoryStream result = new MemoryStream();

BinaryWriter writer = new BinaryWriter(result);

int bytesRead;

byte[] buffer = new byte[2];

try

{

do

{

bytesRead = reader.Read(buffer, 0, buffer.Length);

writer.Write(buffer, 0, bytesRead);

} while (bytesRead > 0);

writer.Flush();

result.Position = 0;

}

finally

{

}

return result;

}

public void WriteToLog(String Source ,SoapMessage message, Stream s)

{

Thread.Sleep(1000);

int bytesRead;

String MsgType = null;

byte[] buffer = new byte[2];

if (message.GetType() == typeof(SoapServerMessage)) MsgType = "ServerMessage";

if (message.GetType() == typeof(SoapClientMessage)) MsgType = "ClientMessage";

//It logs message in new file. Just to make it easy to debug compressed message.

FileStream fs = new FileStream("C:\\" + MsgType + "-" + Source + "-" + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + ".txt", FileMode.Append, FileAccess.Write);

try

{


BinaryReader reader = new BinaryReader(s);

BinaryWriter writer = new BinaryWriter(fs);

do

{

bytesRead = reader.Read(buffer, 0, buffer.Length);

writer.Write(buffer, 0, bytesRead);

} while (bytesRead > 0);

writer.Flush();

}

finally

{

//if (swr != null) swr.Close();

if (fs != null) fs.Close();

}

}

}

[AttributeUsage(AttributeTargets.Method)]

public class GZIPSoapExtensionAttribute : SoapExtensionAttribute

{

private int _Priority = 1;

public override int Priority

{

get

{

return _Priority;

}

set

{

_Priority = value;

}

}

public override Type ExtensionType

{

get { return typeof(GZIPSoapExtension); }

}

}

}