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;
        }
   
    }

pondelok 9. januára 2012

Share files between projects in solution VS

Sometimes you need to share e.g. config files between projects in one solution.
It's very simple:
- Click Add Existing Item and choose "Add as Link"

And don't worry, it is using relative paths.

Rolling log4net logs by Date

Simple example (max. 180 days backwards):

<appender name="AuditFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="d:\audit_logs\log.txt_" />
    <appendToFile value="true" />
    <rollingStyle value="Date" />
    <param name="maxSizeRollBackups" value="180" />
    <param name="StaticLogFileName" value="false"/>
    <param name="DatePattern" value="dd-MM-yyyy"/>
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%message%newline" />
    </layout>
  </appender>

Unit Test Atributes in commom testing frameworks

I'm rewriting some my old test classes, writed in NUnit to the "new" MS Test.
I was looking for the alternative of TestFixtureTearDown

and Whoa la, I found this very usefull table:


NUnit 2.x MbUnit 2.4 MSTest xUnit.net
[Test] [Test] [TestMethod] [Fact]
[TestFixture] [TestFixture] [TestClass] n/a
[ExpectedException] [ExpectedException] [ExpectedException] Assert.Throws or Record.Exception
[SetUp] [SetUp] [TestInitialize] Constructor
[TearDown] [TearDown] [TestCleanup] IDisposable.Dispose
[TestFixtureSetUp] [TestFixtureSetUp] [ClassInitialize] IUseFixture<T>
[TestFixtureTearDown] [TestFixtureTearDown] [ClassCleanup] IUseFixture<T>
[Ignore] [Ignore] [Ignore] [Fact(Skip="reason")]
n/a [Timeout] [Timeout] [Fact(Timeout=n)]
[Property] n/a [TestProperty] [Trait]
n/a [Row], [RowTest] [DataSource] [Theory], [XxxData]






































































































































utorok 3. januára 2012

How to make generic dictionary case insensitive

Dictionary<string, object> values =
new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);

Very simple!

Ako zistiť aktívne konekcie na SQL Server 2008

V Microsoft SQL Server Management Studiu kliknúť na "Activity monitor" - Procesess.
Poskúmajte aj iné záložky, nájdete tam kopu užitočných informácií.

How to find active session on SQL Server 2008






In the  Microsoft SQL Server Management Studio click on "Activity monitor" - tab Procesess. In the context menu is possible to kill session/process.
Also look on others tabs, you can find many useful informations.



Windows Server 2008 R2 licencing for Virtual and Physical environment

 Windows Server 2008 R2 Standard – A customer licensed with Windows Server 2008 R2 Standard may run one instance of the server software in the physical operating system environment (POSE) and one instance of the server software in a virtual operating system environment (VOSE).  If the customer is running the instance in the VOSE then the instance running in the POSE can only be used to manage the instance of the OS running in the VOSE.

    Windows Server 2008 R2 Enterprise – A customer licensed with Windows Server 2008 R2 Enterprise may run one instance of the server software in the physical operating system environment (POSE) and up to four instances of the server software in the virtual operating system environment (VOSE).  If the customer is running 4 instances in the VOSE then the instance running in the POSE can only be used to manage the 4 instances of the OS running in the VOSE.

    Windows Server 2008 R2 for Datacenter – A customer licensed with Windows Server 2008 R2 Datacenter may run one instance of the server software in the physical operating system environment (POSE) and an unlimited number of instances of the server software in the virtual operating system environment (VOSE).

    Windows Server 2008 R2 for Itanium-based Systems – A customer licensed with Windows Server 2008 R2 for Itanium-based Systems may run one instance of the server software in the physical operating system environment (POSE) and an unlimited number of instances of the server software in the virtual operating system environment (VOSE).

    Windows Web Server 2008 R2 – Windows Web Server 2008 R2 is licensed to a server in the physical operating system environment (POSE).  You can also run as a guest in the virtual operating system environment (VOSE).

    Windows Server 2008 R2 Foundation – Windows Server 2008 R2 Foundation is licensed to a server in the physical operating system environment (POSE).