štvrtok 16. februára 2012

An error (The request was aborted: The request was canceled.) occurred while transmitting data over the HTTP channel.

I got this error when transmitting large file (byte array) to WCF service.

I double checked my web.config,

Code Snippet
  1. <bindings>
  2.       <basicHttpBinding>
  3.         <binding
  4.           name="BasicWithMtom"
  5.           maxReceivedMessageSize="2621440"
  6.           messageEncoding="Mtom">
  7.           <readerQuotas maxArrayLength="2621440"/>
  8.         </binding>
  9.       </basicHttpBinding>
  10.     </bindings>

but sizing was good enough.

The problem is, that the WCF service is hosted on IIS and  you need to configure quotas on two separated places!!

Add this:

Code Snippet
  1. <system.webServer>
  2.   <httpRuntime maxRequestLength="2621440" />
  3. </system.webServer>


Everything is working now..

Have a nice day!

pondelok 6. februára 2012

Favorite source code files in Visual Studio

Today I was looking for something like Bookmarks in Firefox or Favorites in IE, but right in the Visual Studio. I found, that it is not native function, but....

Visual Studio Extension called Favorite Documents is exactly, what i was looking for.

Enjoy:

Favorite Documents

Have a nice day!

C# Code to HTML Converter

I used it in my last post.

It's Visual Studio 2010 Extension called Copy as HTMl.

Very simple to use - CTRL - C CTRL - V

Copy as HTML

List of objects to Dictionary

Example of using extension method  ToDictionary.
Maybe you can found this my code  useful.

The first is converting list to dictionary with key-selector.
The second extracts only two atributes (key, val)

Code Snippet
  1. class People
  2.     {
  3.         public string userID { get; set; }
  4.         public string name { get; set; }
  5.         public string phone { get; set; }
  6.         public string email { get; set; }
  7.     }
  8.         
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.            List<People> users = new List<People>();
  14.  
  15.            //p.Add()...
  16.  
  17.  
  18.            Dictionary<string, People> temp =
  19.                               users.ToDictionary(c => c.userID);
  20.  
  21.            Dictionary<string, string> eMails =
  22.                         users.ToDictionary(c => c.userID, c => c.email);
  23.          
  24.         }
  25.     }

pondelok 16. januára 2012

Recycle application pool from command line

Simple working command:

%systemroot%\system32\inetsrv\appcmd  recycle apppool   {POOL_NAME}

If not working, try before "Run as Administrator" the command prompt or .bat file.


Enjoy!

štvrtok 12. januára 2012

Example of compressing and decompressing byte array in C# using Gzip algorithm

public static class Zip
    {
        public static byte[] Decompress(byte[] zippedData)
        {
            byte[] decompressedData = null;
            using (MemoryStream outputStream = new MemoryStream())
            {
                using (MemoryStream inputStream = new MemoryStream(zippedData))
                {
                    using (GZipStream zip = new GZipStream(inputStream, CompressionMode.Decompress))
                    {
                        zip.CopyTo(outputStream);
                    }
                }
                decompressedData = outputStream.ToArray();
            }

            return decompressedData;
        }

        public static byte[] Compress(byte[] plainData)
        {
            byte[] compressesData = null;
            using (MemoryStream outputStream = new MemoryStream())
            {
                using (GZipStream zip = new GZipStream(outputStream, CompressionMode.Compress))
                {
                    zip.Write(plainData, 0, plainData.Length);                   
                }
                //Dont get the MemoryStream data before the GZipStream is closed
                //since it doesn’t yet contain complete compressed data.
                //GZipStream writes additional data including footer information when its been disposed
                compressesData = outputStream.ToArray();
            }

            return compressesData;
        }

    }


And MS Test


 [TestClass]
    public class ZipTEST
    {
        [TestMethod]
        public void TestMethod1()
        {
            string text = "Hello World";
   
            byte [] data = System.Text.Encoding.UTF8.GetBytes(text);

             byte [] compressedData = Zip.Compress(data);
           
            byte [] decompressedData = Zip.Decompress(compressedData);



            string textAfterComDecompress = System.Text.Encoding.UTF8.GetString(decompressedData);

            Assert.AreEqual<string>(text, textAfterComDecompress);               
        }
    }







Generic object dumper with optimized performance

I have actually created this helper class, using for generating logs.
It's using caching and reflection, so the performance impact shouldn't be very high.

Maybe you''ll find this code snippet useful.

/// <summary>
/// Use only  for primitive objects (next version is planning)
/// </summary>
public static class ObjectDumper
    {
        private static Dictionary<Type, System.Reflection.PropertyInfo[]> properties;


         static ObjectDumper()
        {
            properties = new Dictionary<Type, System.Reflection.PropertyInfo[]>();
        }

        public static string DumpDictionary<T, U>(Dictionary<T, U> dictionary, string format)
        {
            StringBuilder result = new StringBuilder();

            foreach (KeyValuePair<T, U> keyVal in dictionary)
            {
                result.AppendFormat(format, keyVal.Key, keyVal.Value);
            }

            return result.ToString();
        }

        /// <summary>
        /// Generic object dumper with optimized performance
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        public static string DumpObject<T>(T obj)
        {
            StringBuilder result = new StringBuilder();
            System.Reflection.PropertyInfo[] pi = GetPropertyInfo(typeof(T));
            result.Append("\n");

            object value;
            for (int i = 0; i < pi.Length; i++)
            {
                value = pi[i].GetValue(obj, null);

                if (value == null)
                    result.AppendFormat("\n{0} = NULL", pi[i].Name);
                else
                {
                    if(pi[i].DeclaringType.IsArray)
                        result.AppendFormat("\n{0} = is array...", pi[i].Name);
                    else
                        result.AppendFormat("\n{0} = {1}", pi[i].Name, value.ToString());
                }
            }

            return result.ToString();
        }

        /// <summary>
        /// Caching logic
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        private static System.Reflection.PropertyInfo[] GetPropertyInfo(Type t)
        {
            System.Reflection.PropertyInfo[] result = null;
            if (properties.TryGetValue(t, out result) == false)
            {
                result = t.GetProperties();
                properties.Add(t, result);
            }

            return result;
        }
   
    }