Archive

Posts Tagged ‘C#’

Redgate Saves Me Again:- Source of Memory Leak Found

It won’t be the first, and for sure not the last. Redgate tool belt has a set of tools which come in handy when your at the edge of giving up. I have used the database refactor tool, to make nightmares go away, and schema migrations horrors are in the past. Have read of memory leaks on blogs but had yet faced the challenge of tracking one.

Over the last month, I have seen my windows service which starts and runs threads to handle different jobs increase in memory till 900MB in a period of two weeks. Restarting the service fixed the issue but i had failed to find the reason why.

I should say, I tried a couple of solutions including live debugging on the production server and my dev machine. Each day I tried a “trick” but zilch. Until it hit me, Redgate has a performance and memory profiler. I last used Redgate Performance profiler to hunt down beasts ages ago, in any case the tools are just click, wait, results and aaaaaahhhh ok, fix that. Performance profiler didn’t get me much. Ran the memory profiler for 3 hours taking snapshots every 30 minutes. Luckily by just looking at the results it was obvious who the culprit was. Googled LocalDataStoreElement, and the results where in my favor. In the code I was storing thread specific data using Thread.AllocateDataSlot etc, and from
http://support.microsoft.com/kb/2540745, the issue occurs because the Thread.AllocateDataSlot method in the .NET Framework 4 creates a memory leak. Sigh!!!. Now how to fix it?   Below I quote from the microsoft site:-

A supported hotfix is now available from Microsoft. However, it is intended to correct only the problem that this article describes. Apply it only to systems that are experiencing this specific problem.

To resolve this problem, contact Microsoft Customer Support Services to obtain the hotfix. For a complete list of Microsoft Customer Support Services telephone numbers and information about support costs, visit the following Microsoft website:

Note In special cases, charges that are ordinarily incurred for support calls may be canceled if a Microsoft Support Professional determines that a specific update will resolve your problem. The usual support costs will apply to additional support questions and issues that do not qualify for the specific update in question.

Great that I have the code and I can apply the recommended fix for switching from Thread.AllocateDataSlot to ThreadLocal<T>. For minimal changes to code, I made use of ThreadLocal<IDictionary<string, object>>.  I do appreciate the memory profiler tool for working the charms, if it wasn’t for the scary price tag, I would purchase the tool after the trial ends.

Categories: Programming Tags:

MSBuild And MVC Area Projects using Autofac as IoC

April 22, 2012 2 comments

I have a Web Project Folder structure MainMVCProject\Areas\MyArea in one of my projects. MyArea is a another MVC project. While setting up an MSBuild project, I realised that on building the main project using the task below, the contents of the child area MyArea where being copied to the parent MainMVCProject.

<MSBuild Projects=”$(WebFolder)\MainMVCProject.csproj”
Targets=”ResolveReferences;_CopyWebApplication”
Properties=”WebProjectOutputDir=$(DeployFolder)\www;
OutDir=$(DeployFolder)\www\bin\;Configuration=$(Configuration)” />

At first it was not obvious why, but managed to figure it out. The cause was because I had added a reference of MyArea project to MainMVCProject. The reason in the first place of adding that reference was to simplify the registration of controllers in the MyArea child Area project assembly using Autofac. Removing the reference fixed the MSBuild output but still needed to register the controllers with the Autofac Container.

In my solution I use a plugin architecture used in Umbraco, details here. In combination with a TypeFinder, I was already using a DependencyRegistrar for registering types to the Autofac Container. So I just added an implementation of IDependencyRegistrar in the MyArea child project which registers the controllers in that assembly. In Global.asax Application start method I find all types that do implement IDependencyRegistrar and execute the register method passing the container.

This overall fixed resolving controllers at run-time.

Categories: Programming Tags: ,

AES 128bit Cross Platform (Java and C#) Encryption Compatibility

September 21, 2011 73 comments

It seems quite a minor issue but doing cross platform encryption and decryption specifically AES was a bit a challenge for me. When you  get it working it just seems like no big change to the original code you where working on but you wasted fruitless hours debugging. Just sharing so that another person doesn’t have to waste time wondering why same settings produce a different cipher and going through a number of unhelpful posts where it ” works on my machine”.

I initially used ISO 10126 padding, but that seemed to produce a different cipher in C# and Java. I decided to go with no padding which leads me to the  cryptographic exception: Length of the data to decrypt is invalid. After some head banging I settled with PKCS7

Same encoding of strings has to be used in java and c#, other wise you will end up having different ciphers in java and c#.

The java code below uses a base64 util class from android SDK but you can replace it like with one from apache commons

C# encryption utility

public RijndaelManaged GetRijndaelManaged(String secretKey)
        {
            var keyBytes = new byte[16];
            var secretKeyBytes = Encoding.UTF8.GetBytes(secretKey);
            Array.Copy(secretKeyBytes, keyBytes, Math.Min(keyBytes.Length, secretKeyBytes.Length));
            return new RijndaelManaged
            {
                Mode = CipherMode.CBC,
                Padding = PaddingMode.PKCS7,
                KeySize = 128,
                BlockSize = 128,
                Key = keyBytes,
                IV = keyBytes
            };
        }

        public byte[] Encrypt(byte[] plainBytes, RijndaelManaged rijndaelManaged)
        {
            return rijndaelManaged.CreateEncryptor()
                .TransformFinalBlock(plainBytes, 0, plainBytes.Length);
        }

        public byte[] Decrypt(byte[] encryptedData, RijndaelManaged rijndaelManaged)
        {
            return rijndaelManaged.CreateDecryptor()
                .TransformFinalBlock(encryptedData, 0, encryptedData.Length);
        }

        /// <summary>
        /// Encrypts plaintext using AES 128bit key and a Chain Block Cipher and returns a base64 encoded string
        /// </summary>
        /// <param name="plainText">Plain text to encrypt</param>
        /// <param name="key">Secret key</param>
        /// <returns>Base64 encoded string</returns>
        public String Encrypt(String plainText, String key)
        {
            var plainBytes = Encoding.UTF8.GetBytes(plainText);
            return Convert.ToBase64String(Encrypt(plainBytes, GetRijndaelManaged(key)));
        }

        /// <summary>
        /// Decrypts a base64 encoded string using the given key (AES 128bit key and a Chain Block Cipher)
        /// </summary>
        /// <param name="encryptedText">Base64 Encoded String</param>
        /// <param name="key">Secret Key</param>
        /// <returns>Decrypted String</returns>
        public String Decrypt(String encryptedText, String key)
        {
            var encryptedBytes = Convert.FromBase64String(encryptedText);
            return Encoding.UTF8.GetString(Decrypt(encryptedBytes, GetRijndaelManaged(key)));
        }

Java Encryption Utility

private final String characterEncoding = "UTF-8";
	private final String cipherTransformation = "AES/CBC/PKCS5Padding";
	private final String aesEncryptionAlgorithm = "AES";

	public  byte[] decrypt(byte[] cipherText, byte[] key, byte [] initialVector) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException
	{
		Cipher cipher = Cipher.getInstance(cipherTransformation);
		SecretKeySpec secretKeySpecy = new SecretKeySpec(key, aesEncryptionAlgorithm);
		IvParameterSpec ivParameterSpec = new IvParameterSpec(initialVector);
		cipher.init(Cipher.DECRYPT_MODE, secretKeySpecy, ivParameterSpec);
		cipherText = cipher.doFinal(cipherText);
		return cipherText;
	}

	public byte[] encrypt(byte[] plainText, byte[] key, byte [] initialVector) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException
	{
		Cipher cipher = Cipher.getInstance(cipherTransformation);
		SecretKeySpec secretKeySpec = new SecretKeySpec(key, aesEncryptionAlgorithm);
		IvParameterSpec ivParameterSpec = new IvParameterSpec(initialVector);
		cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
		plainText = cipher.doFinal(plainText);
		return plainText;
	}

	private byte[] getKeyBytes(String key) throws UnsupportedEncodingException{
		byte[] keyBytes= new byte[16];
		byte[] parameterKeyBytes= key.getBytes(characterEncoding);
		System.arraycopy(parameterKeyBytes, 0, keyBytes, 0, Math.min(parameterKeyBytes.length, keyBytes.length));
		return keyBytes;
	}

	/// <summary>
	/// Encrypts plaintext using AES 128bit key and a Chain Block Cipher and returns a base64 encoded string
	/// </summary>
	/// <param name="plainText">Plain text to encrypt</param>
	/// <param name="key">Secret key</param>
	/// <returns>Base64 encoded string</returns>
	public String encrypt(String plainText, String key) throws UnsupportedEncodingException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException{
		byte[] plainTextbytes = plainText.getBytes(characterEncoding);
		byte[] keyBytes = getKeyBytes(key);
		return Base64.encodeToString(encrypt(plainTextbytes,keyBytes, keyBytes), Base64.DEFAULT);
	}

	/// <summary>
	/// Decrypts a base64 encoded string using the given key (AES 128bit key and a Chain Block Cipher)
	/// </summary>
	/// <param name="encryptedText">Base64 Encoded String</param>
	/// <param name="key">Secret Key</param>
	/// <returns>Decrypted String</returns>
	public String decrypt(String encryptedText, String key) throws KeyException, GeneralSecurityException, GeneralSecurityException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, IOException{
		byte[] cipheredBytes = Base64.decode(encryptedText, Base64.DEFAULT);
		byte[] keyBytes = getKeyBytes(key);
		return new String(decrypt(cipheredBytes, keyBytes, keyBytes), characterEncoding);
	}

Hopefully this saves someone time.

Categories: Programming Tags: , ,

Using MongoDB as the Audit and Log data store with a custom tracelistener

This post illustrates how one would save changes to ADO entities and other logging via the enterprise library logging application block to MongoDB. MongoDB is an open source agile document oriented nosql database that supports sharding, map-reduce, indexing on attributes, GridFS etc. MongoDB is fast to setup and fast to scale. The choice to move logging to MongoDB was to make use of running map reduce jobs on the dataset.

To be able to connect to MongoDB process you will have to download the latest C# Driver

To be able to log via the Enterprise Logging Application Block I created a custom trace listener and trace listener configuration. The trace listener configuration was to allow specifying MongoDB connection string and database name. The custom trace listener opens the connection to the MongoDB process and inserts the updates. Every attempted change to the entity is stored. Read more…

Categories: Programming Tags: ,

Editing Excel Sheets using Microsoft.Office.Interop.Excel in C#

I found my self repetitively entering data into an excel sheet and needed to automate it using C#.

First add a reference of Microsoft.Office.Interop.Excel to your project.

To open a work book


using Microsoft.Office.Interop.Excel;
...
...
var m_TemplateFileName = "/path/to/excel/file/to/edit.xls";
var m_InputWorkSheetName = "input";
var application = new ApplicationClass();
var workBook = application.Workbooks.Open(m_TemplateFileName);
...
...

Read more…

Categories: Programming Tags: , ,

Uganda Dot Net User Group meeting for 26th Febuary 2010

Friday 26th Feburay 2010 was my first Uganda Dot Net User Group meeting.

The topic of discussion for the day was NHibernate ORM framework presented by J Marcus (Assistant IT Manager bank of Baroda).

Am not an expert of NHibernate, so it was time not wasted to listen and see how NHibernate can be applied in .Net applications. I use a combination of spring and Hibernate in the java applications I do develop, so I know the hustles of hibernate. Like having to create the mapping files and classes that map to database objects manually. Good enough NetBeans 6.8 comes with a reverse engineering plugin that removes the hustles of having to update and create the mapping classes. Read more…