find a location for property in a new city

Thursday, 10 November 2011

Cannot create database 'DevelopmentStorageDb20110816' in Storage Emulator Azure SDK

I have uncovered this problem before and this time I thought I would spend the time to crack it. When running the Storage Emulator from the Azure SDK or running DSINIT.exe for the first time it needs to initialise; part of this involves creating a new database. This was not running correctly due to a permissions problem which stated "Cannot create database 'DevelopmentStorageDb20110816' : CREATE DATABASE permission denied in database 'master'".

The full report is as follows:

Added reservation for http://127.0.0.1:10000/ in user account MSFT-123\MyUser.
Added reservation for http://127.0.0.1:10001/ in user account MSFT-123\MyUser.
Added reservation for http://127.0.0.1:10002/ in user account MSFT-123\MyUser.

Creating database DevelopmentStorageDb20110816...
Cannot create database 'DevelopmentStorageDb20110816' : CREATE DATABASE permission denied in database 'master'.

One or more initialization actions have failed. Resolve these errors before attempting to run the storage emulator again. These errors can occur if SQL Server was installed by someone other than the current user. Please refer to http://go.microsoft.com/fwlink/?LinkID=205140 for more details.

Grant permissions

The user this program is running as must be a sa with full permissions on the database. If this is not the case you can either change the user or GRANT permissions to the current user.

To change the user run the Windows Azure SDK Command Prompt (as administrator) and type 'DSINIT /?'. This will give you details on how to change user, which is to use the /user: argument.

Alternatively you could GRANT the permissions to your default user like so:

USE master
GRANT CREATE DATABASE TO "MYDOMAIN\MyUser"

Either of these solutions should solve your permissions problem with DSINIT.

Cannot GRANT permissions

Unfortunately my problems went further than this still. I believe this is to do with how many times I had installed a SQL Server Express in the past. How irritating is that?! You own the machine and yet you don't seem to have the permissions that reflect that!

Anyway, the solution to make this work once again was (unfortunately) to uninstall SQL Server Express and reinstall it again. This way you will be the owner of the SQL Server Express database engine and you will be able to create all the databases you wish, including your long awaited Storage Emulator database.

A better way to regain admin access

A better way to regain admin access was pointed out to me by a colleague, Michael Coates. You can either solve this loss of administrator on a SQL server by following this troubleshooting guide from MSDN (this worked for one commenter). Or you can run a batch script that will magically do it for you (this didn't work for one commenter). I have not done either of these so take this advice at your own risk. Remember, my way was to delete and reinstall the server so this cannot be more risky, surely?

Follow britishdev on Twitter

Tuesday, 8 November 2011

Cross database joins in SQL Azure

Currently cross database joins are not supported in SQL Azure. Also you cannot change database mid query so you cannot, for example, put a USE [MyDB] in your query either. As a side note, please vote for it to be a priority feature for the SQL Azure team to develop soon.

So, since cross database joins are not supported at this time you must find a workaround. I will give you two possible solutions I would recommend and you can hopefully choose the one that is best for your application.

Combine your databases

If you have tables that are frequently used together, i.e. they are joined in queries or the rows are inserted in the same transactions, then it would be a good idea to move the similar tables into the same database. This of course eliminates the need to traverse databases. SQL Azure has very recently increased the maximum database size from 50GB to 150GB, which potentially makes this a more viable option than perhaps it once was.

Join your data in your application

Two separate queries could be run on the two separate databases and then these results could be joined within that application. Obvious downsides to this will be the potential for large DB I/O, large network transfer and large memory usage in your app. This is not something to consider if the amount of data that is likely to be returned is large (e.g. 1000+ rows) but it is fine if the data will be manageable.

Conclusion

Personally I would much rather settle for bringing all my similar tables that are likely to be used within the same queries together into one database so there is no longer a need for cross database solutions. This makes for cleaner application code and more efficient use of your resources. However, if this option is not available to you then perhaps the second option may have to be the one you choose.

Surely soon in the future the SQL Azure team will address this issue though and your cross database code can stay clean! Although in fairness cross database querying isn't even available in Entity Framework yet (but still workaround-able) either. I wonder why this is so difficult for Microsoft? Just shows you should always try and combine similar DBs in any database designs where possible.

Follow britishdev on Twitter

Monday, 7 November 2011

"The Web Platform Installer could not start" fix

I was trying to download the Azure SDK version 1.5 and I was experiencing issues preventing the install because the Windows Platform installer could not load up and run properly. It instead opened a error message saying "The Web Platform Installer could not start. Please report the following error on the Web Platform Installer Forum. http:/forums.iis.net/1155.aspx".

The problem was that the Web Platform Installer needed to update but could not seem to manage it. This was causing the error to display and then crash without allowing me to install the original Azure SDK I was actually trying to install.

Microsoft Fix It fixed it

Microsoft Fix It is a tool from Microsoft that automatically finds the particular one you want in this scenario is the Diagnose and fix program installing and uninstalling problems automatically.

To do this download it and use it as follows:

  • When the option to select "Detect problems and let me select the fixes to apply." appears, do that.
  • say you are having a problem with "Installing"
  • After a short wait scroll down to find "Microsoft Web Platform Installer 3.0" (or which ever version number you have) and select that.
  • Next you should click "Yes, try uninstall" then click next and follow the instructions.

This should solve your issue with the Microsoft Web Platform Installer and allow you to install the Azure SDK or whatever it is you're trying to install.

Follow britishdev on Twitter

Wednesday, 26 October 2011

Remote development and staging environments from Google Analytics

I have created a new site which is currently only getting a low amount of traffic. There is a beta version of the site live at the moment but it is still in development too. My problem is that Google Analytics is still tracking page views on my local machine during development because the Google Analytics tracking code is on the local version too.

I do not want any hits that are a result of me testing my development site or staging site to count towards the total page views and unique visits recorded in Google Analytics. Since there are only low amounts of traffic at the moment my development will dramatically skew the analytics results.

I tried a few ways to control the analytics recordings by using filters in the Google Analytics dashboard but made some mistakes which destroyed all tracking before I finally got it right. Whoops... So to make sure you get it right first time here is how to do it.

How to filter page views from your local or development site

  1. Go to you Google Analytics dashboard by clicking on "Analytics Settings"
  2. Click "Filter Manager »"
  3. Click "Add Filter"
  4. Name the filter
  5. Select the "Custom filter" radio button
  6. Select "Include" radio button
  7. Choose "Hostname" as your Filter Field
  8. And type in "^www\.mysite\.com$" as your Filter Pattern (without the quotes)
  9. Select case-sensitivity. I chose "No"
  10. Add your website profile to the selected website profiles
  11. Click "Save Changes" button
And you're done. This will stop any sites like local.www.mysite.com or 127.1.1.1 every affecting your analytics. It will only register when the host name is exactly www.mysite.com. I hope that helps you get it right first time so you don't accidentally take your analytics down like I did!

Follow britishdev on Twitter

Thursday, 20 October 2011

Unable to remove directory. Access to the path 'mswasri.dll' is denied when packaging an Azure project

When trying to build a package using the Azure SDK built into Visual Studio 2010 I sometimes get the error message "Unable to remove directory "bin\Release\CloudPackage.csx\". Access to the path 'mswasri.dll' is denied."

This stopped me from being able to build Azure cloud package ready for deployment. I tried changing my cloud package .csx file to read only but it just changed back. I tried deleting it but it was in use.

This gave me a clue. I run my local copy of my Azure site through my local IIS. I think this was locking the site and thus preventing it from being packaged.

Solution

The way to solve this problem that I found to work best was to find the Application Pool that is running the web application in my local development environment and stop it. This allows me to successfully package my web application ready from deployment to the Azure cloud!

Follow britishdev on Twitter

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