Windows Phone 7 Deve Launch
# Tuesday, July 02, 2013

I create a virtual machine every now and then and even less have to resize the hard drive.  Below is my mental notes jotted down so I can easily remember how to do what I normally do with virtual machines using Oracle VirtualBox 4.2.14 for windows host.

The notes are raw and by no means a complete list and/or an exact step by step tutorial.  I figured, if I find it helpful so will someone else. Enjoy!

First, the manual.

Creating a virtual machine

The below sample creates a new W2008R2 Server and is assumed to be on a Windows host machine.

  • Select New button in VirtualBox Manager, give it a name, type is MS Windows & Version is Windows 2008 (64bit).
  • Give it the amount of RAM you want.
  • For Hard drive there are three options, do not add, create, use an existing.
    • Create a new HD is likely what you will want unless you downloaded an existing virtual hard drive file. If the latter is true select use an existing.
  • For creating a hard drive, select VHD (Microsoft's version) OR VDI (VirtualBoxes version) or VMDK (VMWare version).
    • There are tools to convert each to the other. Choose what you believe is the best for your situation.
  • Select Dynamically allocated for hard drive type.
  • Choose the size you feel you need to be able to adequately run the box on. Be sure to consider the OS size, updates and all of the extras you need on the machine. The default is 25GB which mean the hard drive can grow to 25GB at most. For a windows OS this is a little on the small size.
    • The default location for the virtual machine file is under your user profile (c:\users\<profile>\) VirtualBox VMs\<virtual machine name>.
    • You can change this on the size wizard page by selecting the folder icon and specifying where you want it.
      • This does not effect snapshot location. See snapshots below for more.

The Windows 2008R2 OS before updates is around 7GB but after all the updates through 7/1/2013 puts it at 24GB. A hard drive around 40-50GB may be more appropriate.

Network Configuration

There are several ways to setup the network for the virtual machine. The choice effects how the machine is or is not seen on a network.

The most common for a developer station is either host-only or bridged.

  • Host-only means the VM is only visible to the host machine. This is a great option if you don't need any external resources for the VM.
  • Bridged - The VM will use the NIC card on the host to connect with network resources. The bridge creates it's own MAC serving as a virtual NIC card.

For more in depth review see the manual.

General settings

Under the advanced tab you can enable sharing of the clipboard between host and guest as well as drag and drop.

You can only make changes to settings for the VM when it is shutdown. If it isn't, you can view the settings but everything will be greyed out.

For more on settings see the manual.

Installing OS

After the image is created, you'll need to install the OS. You can do this with an ISO and/or an actual install disk.

To select this, select your VM in the Virtual Manager, select settings, select the storage item & in the controller IDE select either the CD/DVD drive on your host machine or choose the ISO to run against.

Now start the virtual machine and follow your normal procedures for install/post install configuration needs/instructions to get your VM ready to go.

Install Guest Additions

After the OS is installed and you are in the VM, from the Devices menu select Install Guest Additions and follow the wizard to install.

Basic commands

"Host Key" - default is the right ctrl key.

Sending ctrl+alt+del - host key + del

You can cycle through screen sizes:

host key + f - full screen

host key + c - scale

host key + a - allow you to resize...

More on special characters.

Snapshots

VirtualBox can save the VM at it’s current state with snapshots. This is a great way to save off the VM at points in time that you may want to go back to. One great example would be to snapshot the VM before you do a major update on it. If something goes wrong you can go back to a snapshot.

Snapshots are created by right clicking on the VM and selecting snapshot.

Snapshots are stored separately from the main virtual hard drive. The default is under your user profile in VirtualBox VMs\<VMName>\snapshots. <VMName> is the name you gave the VM.

You can change where snapshots are stored under the General settings, advanced tab.

Cloning

Cloning copies a VM. You can also clone from a snapshot. Right click on the VM and/or in the snapshot view and select clone.

Resizing the virtual machine hard drive

Assumed using Oracle VirtualBox 4.2.14.

You can resize a virtual hard drive if it is dynamically allocated. Fixed drives can not be resized*.

If you used snapshots you also need to clone your drive and merge all of the snapshots thus loosing the snapshots first. :-(

From the command line run VBoxmanage

vboxmanage modifyhd "path\virtualharddrivename" --resize <megabytes>

<megabytes> is the size you want to resize to, say 40GB would be 40000.

Assuming windows server OS, fire up the VM, go into disk management, right click on the partitioned space (likely your C drive) and select Extend Volume. The newly created unpartitioned space (the difference between your original hard drive size and the newly created size) will be added to your disk.

* You could convert the fixed to dynamic and then resize by cloning the drive to a dynamic first.

By no means am I an expert on VirtualBox.  There may be slicker ways to resize or use VirtualBox. 

Happy Coding! Smile

Posted 07.02.2013  #    Comments [0]  | 
# Friday, May 03, 2013

Today I wasted an hour or two chasing my tail around trying to figure out why a change to one of my ASP.NET MVC Views was not at all showing any changes in my jQuery Mobile version.  I cleared my cache, hit ctrl+F5 a billion times, redeployed to our test server and then again.  I double checked my code, then again.  I would view source and on my mobile version it hadn’t changed.  What is going on?

I noticed another oddity through all this. My page was loading up two versions of jQuery.  One being an older version I know I updated. 

<script src="/Scripts/jquery-1.8.2.js"></script>
<script src="/Scripts/jquery-1.8.3.js"></script>

Yet, it was loading. Huh? I’m confused.

What’s Happening

Then it hit me. Bundling in ASP.NET MVC looks for whatever version I have in the scripts folder to generate the tag since my syntax is:

bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"));
    Does this mean I have the older version along with the newer version on my test server. 

folder

Inspecting the test server revealed this to be the case.  I then went to my view folder for the mysterious view that wasn’t changing and found a very old .mobile.cshtml file that we ditched a while back once we were able to consolidate our normal and mobile view on one page.

So how did this happen.  We’re a small shop and use Web Publish built into Visual Studio to deploy to our servers.  I looked at the file publish options and the "Remove additional files at destination" was unchecked.

publishOptions

Leaving this unchecked means that any file you removed from your project if on the destination machine will not be removed. 

Delete some views, update your jQuery nuget packages and publish without this checked and the old items will remain on your deployed environment.

A simple check of the box and all is well again.

Luckily, our production server deploy settings were correct so we didn’t have to worry about some nasty little bug in production due to this. Smile

Happy Coding!

Posted 05.03.2013  #    Comments [0]  | 
# Wednesday, May 01, 2013

All of the applications I put into production use ELMAH to track unhandled exceptions.  In one our new releases of an application I noticed a semi-rare bug occurring through these logs.

Validation Failed

Validation Failed

System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities.

I initially figured something extraordinary occurred for this to happen. Then it happened again and then another time. Now I felt it was time to give this error a more thorough review.  Something less then extraordinary was likely happening.

I use data annotations on my database model since I’m using Entity Framework Code First (EF CF) and in my view models.  I was a little concerned that some nefarious input was being entered that the client side unobtrusive JavaScript was missing as well as in my server side checks.

As it has almost always turned out when I suspect something like this, the true answer is something much simpler and sometimes even a bit embarrassing.

Here’s the offending view model / with the EF CF model.  Can you catch the issue?

//View Model
public class MyCourseViewModel
{
    [Display(Prompt = "Yada Yada Yada College")]
    [Required]
    public string College{ get; set; }
    [Display(Name = "Course Title", Prompt = "Intro to Psychology")]
    [Required]
    public string Name { get; set; }
    [Display(Name = "Course Prefix & Number", Prompt = "PSY 101")]
    [Required]
    public string CourseNumber{ get; set; }
//…more view model fields…. } //Database Model... public class MyDatabaseModel{ [StringLength(150)] public string College{ get; set; } [Required] [StringLength(150)] public string Name{ get; set; } [Required] [StringLength(25)] public string CourseNumber { get; set; } }

Mismatched Annotations

In my database model I set maximum lengths for my strings but I didn’t for my view model.  Enter the case where a user enters a value longer than the allowable amount and you have the client side & server side checks all pass on the view model but then an error occurs at the database model level since you’ve exceeded your length.

Takeaway

Make sure you at least have matched or created a more stringent rule in your view models as you do in your database models.  A simple oversight on my part and definitely not intentional.

Logging

Takeaway number two: Log, log, and log.  Then be sure to review those logs not just ignore them.  Take action when you begin to see a pattern.

Dealing with exceptions

Adding ELMAH to my MVC projects has been a breeze and has offered a great amount of insight into unforeseen bugs.  I often don’t react to the first new bug that ELMAH reports.  I instead look to see if it’s a pattern and when one emerges I then put it on my list to deal with.

Happy Coding! Smile

PS – I realize a better test setup would have been able to prevent this bug from ever making it into the wild. A unit test would have even missed this but not an integration test. Some extra food for thought on how testing can save your bacon…. Mmm… bacon.

Posted 05.01.2013  #    Comments [0]  |