Wednesday, January 25, 2012

Manual GZipStream Compression / Decompression

 public MemoryStream GZipCompress(MemoryStream mStreamOrg)
         {
             try
             {

                 byte[] buffer = mStreamOrg.ToArray();

                 MemoryStream ms = new MemoryStream();
                 // Use the newly created memory stream for the compressed data.
                 GZipStream compressedzipStream = new GZipStream(ms, CompressionMode.Compress, true);
                 Console.WriteLine("Compression");
                 compressedzipStream.Write(buffer, 0, buffer.Length);
                 // Close the stream.
                 compressedzipStream.Close();
                 Console.WriteLine("Original size: {0}, Compressed size: {1}", buffer.Length, ms.Length);

                 // Reset the memory stream position to begin decompression.
                 ms.Position = 0;
                 return ms;

             } // end try
             catch
             {
                 return null;
             }
            
         }

         private  void GZipDeCompress(MemoryStream compressedMemoryStream, long actualLength)
         {
            
             GZipStream zipStream = new GZipStream(compressedMemoryStream, CompressionMode.Decompress);
             Console.WriteLine("Decompression");
             byte[] decompressedBuffer = new byte[actualLength];
             // Use the ReadAllBytesFromStream to read the stream.
             int totalCount = ReadAllBytesFromStream(zipStream, decompressedBuffer);
             Console.WriteLine("Decompressed {0} bytes", totalCount);

            string result = Encoding.UTF8.GetString(decompressedBuffer);
             MemoryStream msTemp = new MemoryStream(Encoding.UTF8.GetBytes(result));

             var serializer = new System.Runtime.Serialization.DataContractSerializer(typeof(EntityObj));


             EntityObj deserializedObj =
                 (EntityObj)serializer.ReadObject(msTemp);



             zipStream.Close();
         }

 public static int ReadAllBytesFromStream(Stream stream, byte[] buffer)
         {
             // Use this method is used to read all bytes from a stream.
             int offset = 0;
             int totalCount = 0;
             //while (true)
             //{
             //    int bytesRead = stream.Read(buffer, offset, 100);
             //    if (bytesRead == 0)
             //    {
             //        break;
             //    }
             //    offset += bytesRead;
             //    totalCount += bytesRead;
             //}
             totalCount = stream.Read(buffer, offset, buffer.Length);
             return totalCount;
         }

Tuesday, January 24, 2012

Manual Serializaion / Deserialization of Datacontract classes

In the example given below, we will first serialize an object in memory and then deserializeit.

MemoryStream mStream = new MemoryStream();


-- Seriailize an Object

public  void WriteObject(ClassTest objTest)
         {
           
             DataContractSerializer ser =
                 new DataContractSerializer(typeof(ClassTest ));
             ser.WriteObject(mStream, objTest);
            
         }

-- Deserialize the object in memory

 public void ReadObject(ClassTest objTest)
         {
            
             DataContractSerializer ser = new DataContractSerializer(typeof(ClassTest ));

            string result = Encoding.UTF8.GetString(mStream.GetBuffer(), 0, (int)mStream.Position);
             MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(result));

             EntityObj deserializedObj =
                 (EntityObj)ser.ReadObject(ms);
             mStream.Close();
            
         }

Monday, January 23, 2012

DataContractSerializer Error in Deserialization : There was an error deserializing the object of type The maximum nametable character count quota (16384) has been exceeded while reading XML data.

Below is an example of Datacontract deserialization of an object

 public static void ReadObject(ClassTest tstObj)
         {
             FileStream fs = new FileStream(@"D:\" + tstObj.Name + ".xml", FileMode.Open);

             XmlDictionaryReader reader =
                 XmlDictionaryReader.CreateTextReader(fs, new XmlDictionaryReaderQuotas());
            
             DataContractSerializer ser = new DataContractSerializer(typeof(ClassTest ));

             // Deserialize the data and read it from the instance.
             ClassTest deserializedTest =
                 (ClassTest )ser.ReadObject(reader, true);
             reader.Close();
             fs.Close();
            
         }

To overcome the error just change the highlighted line to below:

XmlDictionaryReaderQuotas dictQuota = new XmlDictionaryReaderQuotas();
             dictQuota.MaxNameTableCharCount = int.MaxValue;
             dictQuota.MaxArrayLength = int.MaxValue;
             dictQuota.MaxStringContentLength = int.MaxValue;
             XmlDictionaryReader reader =
                 XmlDictionaryReader.CreateTextReader(writer, dictQuota);


Wednesday, January 11, 2012

ASP.Net : Treeview Find method, What is Key ?

Treeview control in Asp.net provides  a find method to find a collection of nodes based on given key.

Like : TreeView1.Find(key , searchAllChildren); 

Or

TreeView1Nodes[0].Nodes.Find(key , searchAllChildren); 


For search based on key while populating the treeview you need to set name of all nodes, Node name will be treated as key while Find.











Setup for installing Asp.net Windows service

Please follow the link below to create a setup for installing windows service without using installutil and command prompt.

http://support.microsoft.com/kb/816169

Saturday, January 7, 2012

Some Facts about WCF

  1. WCF considers "https://www.domainname.com" and "www.domainname.com" as two separate URLs. So if you have permissions or have some settings for the one mentioned above then it will not work for the other !! strange but true .....

ASP.Net : Implement LDap Authentication

Below is a  working example to implement LDAP Authntication.

This demo contains a class named "LdapAuthentication" . this contains all functions to implement the authentication.



Click here to download the sample



Thursday, January 5, 2012

WCF error while communicating large data : Failed to allocate a managed memory buffer of 268435456 bytes. The amount of available memory may be low.

This error generally comes when communicating a lot of data , say 200, 300 MBs or above.

To avoid this error set transfermode attribute to "streamed" rather than "Buffered" (default value)

like below:


<basicHttpBinding>
  <binding name="HttpStreaming" maxReceivedMessageSize="67108864"
           transferMode="Streamed"/>
</basicHttpBinding>
<!-- an example customBinding using Http and streaming-->
<customBinding>
  <binding name="Soap12">
    <textMessageEncoding messageVersion="Soap12WSAddressing10" />
    <httpTransport transferMode="Streamed" maxReceivedMessageSize="67108864"/>
  </binding>
</customBinding>
 
transfermode should be set both at client and server sides.
for more information please see: http://msdn.microsoft.com/en-us/library/ms789010.aspx