find a location for property in a new city

Monday 12 September 2011

Azure TableServiceContext does not contain CreateQuery Azure

I have done it loads of times before but this time when trying to call CreateQuery, Add Object, DeleteObject, UpdateObject or SaveChanges on my TableServiceContext I got an error saying "'Microsoft.WindowsAzure.StorageClient.TableStorageServiceContext' does not contain a definition for 'CreateQuery'".

I keep forgetting that as well as including the Microsoft.WindowsAzure.StorageClient assembly I also need to include System.Data.Services.Client.

This is because, although TableStorageServiceContext comes in Microsoft.WindowsAzure.StorageClient.dll, it inherits from DataServiceContext which is part of another assembly, System.Data.Services.Client.dll. So this must be included in you Azure storage project.

Follow britishdev on Twitter

Friday 9 September 2011

Not running in a hosted service or the Development Fabric - Azure

I got an InvalidOperationException with message "Not running in a hosted service or the Development Fabric" while trying to access my Azure site hosted on my local IIS. I have encountered this before and solved it but this time it confused be further so I thought this time I should document it.

The cause of this issue is that you are not running you Compute Emulator or your Storage Emulator. You can get these when you download and install the Windows Azure SDK (currently at v1.4). Once you have these you must have them running and, according to Michael Collier's blog post, they must be running as administrator too.

This can be done in two ways:

  • Going to Start > All Programs > Windows Azure SDK v1.4 > Compute Emulator and run it as administrator (which I can't find how to do).
  • Run the site in debug mode. This will cause the emulators to start running (since Visual Studio is running as administrator). Your IIS hosted site will then also be able to run.

I found the first way didn't work but that was because I didn't know I had to run them as administrator before I read Collier's post. However, after reading it I still can't figure out how to run it as administrator without using Visual Studio.

So usually I hit F5 to start debugging in the 127.0.0.1:{random port} page that starts up. I then close that window and continue using my IIS hosted site now that the emulators are running as administrator.

I have found one further complication though on the odd occasion. It seems that if you attempted to run your site before you had the emulators running, got the error and then started the emulators, you would still get the error. When this happens I found if I restart my site and application pool in IIS manager it will begin to work with the already running emulators correctly.

Conclusion

I now believe that you must have your Azure Compute Emulator(s) running as administrator before your site and application pool start up to run an Azure site successfully in IIS in a development environment.

Follow britishdev on Twitter

Thursday 8 September 2011

MD5 encryption in .NET

There is not set up needed for this one.

MD5 hash encryption

public static string Md5Encryption(string dataToEncrypt)
{
    var bytes = Encoding.Default.GetBytes(dataToEncrypt);
    using (var md5 = new MD5CryptoServiceProvider())
    {
        var cipher = md5.ComputeHash(bytes);
        return Convert.ToBase64String(cipher);
    }
}

Follow britishdev on Twitter

Triple DES encryption and decryption with .NET

TripleDes essentials

Start your class like this:
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Utils
{
 public static class EncryptionHelper
 {
    static private byte[] key = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
                                            15, 16, 17, 18, 19, 20, 21, 22, 23, 24};
    static private byte[] iv8Bit = { 1,2,3,4,5,6,7,8 };

TripleDes Encryption

Add the following method for encrypting with Triple DES:
public static string TripleDesEncryption(string dataToEncrypt)
{
   var bytes = Encoding.Default.GetBytes(dataToEncrypt);
   using (var tripleDes= new TripleDESCryptoServiceProvider())
   {
    using (var ms = new MemoryStream())
    using (var encryptor = tripleDes.CreateEncryptor(key, iv8Bit))
    using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
    {
     cs.Write(bytes, 0, bytes.Length);
     cs.FlushFinalBlock();
     var cipher = ms.ToArray();
     return Convert.ToBase64String(cipher);
    }
   }
}

Triple DES Decryption

The next method will allow decryption of your data
public static string TripleDesDecryption(string dataToDecrypt)
{
   var bytes = Convert.FromBase64String(dataToDecrypt);
   using (var tripleDes= new TripleDESCryptoServiceProvider())
   {
    using (var ms = new MemoryStream())
    using (var decryptor = tripleDes.CreateDecryptor(key, iv8Bit))
    using (var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write))
    {
     cs.Write(bytes, 0, bytes.Length);
     cs.FlushFinalBlock();
     var cipher = ms.ToArray();
     return Encoding.UTF8.GetString(cipher);
    }
   }
}

Follow britishdev on Twitter

AES encryption and decryption in .NET

AES essentials

Start your class like this:
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Utils
{
 public static class EncryptionHelper
 {
    static private byte[] key = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
                                            15, 16, 17, 18, 19, 20, 21, 22, 23, 24};
    static private byte[] iv16Bit = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 };

AES Encryption

Add the following method for encrypting with AES:
public static string AesEncryption(string dataToEncrypt)
{
   var bytes = Encoding.Default.GetBytes(dataToEncrypt);
   using (var aes = new AesCryptoServiceProvider())
   {
    using (var ms = new MemoryStream())
    using (var encryptor = aes.CreateEncryptor(key, iv16Bit))
    using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
    {
     cs.Write(bytes, 0, bytes.Length);
     cs.FlushFinalBlock();
     var cipher = ms.ToArray();
     return Convert.ToBase64String(cipher);
    }
   }
}

AES Decryption

The next method will allow decryption of your data
public static string AesDecryption(string dataToDecrypt)
{
   var bytes = Convert.FromBase64String(dataToDecrypt);
   using (var aes = new AesCryptoServiceProvider())
   {
    using (var ms = new MemoryStream())
    using (var decryptor = aes.CreateDecryptor(key, iv16Bit))
    using (var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write))
    {
     cs.Write(bytes, 0, bytes.Length);
     cs.FlushFinalBlock();
     var cipher = ms.ToArray();
     return Encoding.UTF8.GetString(cipher);
    }
   }
}

Follow britishdev on Twitter