Tuesday, February 25, 2014

Getting Started with NoSQL and MongoDB

Journey to NoSQL Land

Recently, I decided to starting playing with NoSQL and to break away from my traditional rational database model. The two biggest benefits for using NoSQL, oppose to RDBMS, are increased scalability and increased performance. There are many reasons that contribute to these benefits, which are outside the scope of this article, but here's a good place to start:


MongoDB is a NoSQL document-oriented database type system. Why did I choose MongoDB over the other NoSQL systems available? Mainly because it's considered the most popular NoSQL database system and it integrates with .NET really well (which is right up my alley).

Installing MongoDB for Windows

To begin, you can download the MongoDB Windows distribution from their download site - it will download as a .zip package. Make your life easier by extracting the folder to your desired location and rename the folder to something simple. E.g. C:\mongodb\
Extract zip package to desired location
Next, let's set up our folder structure to store the MongoDB databases by creating a directory named "data", and a subdirectory named "db". E.g. C:\data\db\
Create a directory name "data" and a subdirectory named "db"
Congrats, you have installed MongoDB and have began your journey to NoSQL Land! Now let's start using MongoDB.

Getting Started with MongoDB

First, we need to create a connection that will be used between the MongoDB client and the MongoDB service to use the MongoDB databases. This can be accomplished by opening the command prompt and going to the "bin" folder from the extracted MongoDB zip package, and then executing the MongoDB service.
 cd C:\mongodb\bin
 mongod.exe -dbpath "C:\data\db"
Your firewall will block the connection; although, you should be prompted for access in which you can grant access for Private networks.
Grant access for Private networks
This will open the corresponding port needed to make the connection available for a MongoDB client. The connection is ready when you see the following line:
 [initandlisten] waiting for connections on port 27017
Connection is ready for a MongoDB client
We are ready to create a database and add some content! Open the MongoDB client shell with the following command:
Once the shell opens, a connection has been made between the shell client and the service as you can see below in the highlighted text:
Connection made between client and service
To familiarize yourself with some of the commands, type "help" to see a list of commands. Type "shows dbs" to see a list of your databases (by default there should be a test and local database). Let's add some content to the test database with the following command:
 use test 
 db.test.save({ someStringField: "String value", someIntField: 23, someObjectField: {objectField1: "Some value", objectField2: "Some value again"}})
Now let's check to see if the document was stored by executing the following command:
The content entered previously should be returned along with the document's corresponding id. Below is a screenshot of the expected results and some other screenshots of other queries that you can execute using the MongoDB shell.
Example of executing MongoDB shell commands
Second example of executing MongoDB shell commands
That's it! We've officially made it to NoSQL land using MongoDB.

Creating the MongoDB Windows Service

One more thing I want to cover is creating the MongoDB Windows service. This eliminates the step of executing the MongoDB service (mongod.exe command), creating the listener for the service.
To create the MongoDB Windows service, we have to create a subdirectory named "log" in the MongoDB directory we extracted from the beginning. Afterwards, we have to create a config file that points to the log folder path. This can be done in the command prompt with the following commands:
 md c:\mongodb\log 
 echo logpath=c:\mongodb\log\mongo.log -logappend > c:\mongodb\mongod.cfg 
Create "log" subdirectory and config file
Now we can create and start our service with the following commands within the MongoDB "bin" directory:
 cd c:\mongodb\bin 
 mongod.exe -dbpath "c:\data\db" -config "c:\mongodb\mongod.cfg" -logpath "c:\mongodb\log\mondo.log" -install 
 net start MongoDB 
Create and start the MongoDB Windows service
Verify the MongoDB service exists and was started in services.msc. If everything was configured correctly, you should be able to execute commands from the MongoDB client without issues and there should be some logging created in the "log" folder.
Mongo DB Windows service created and started
Next, I will be using MongoDB's GridFS model for storing large documents such as pictures, movies, audio etc. similar to a blob storage.

Happy Coding!!


Thursday, November 21, 2013

Building a Windows 8.1 Developer Environment in Windows Azure

This article was based on the article Step-by-Step: Building a FREE Windows 8 App Dev Lab in the Cloud with Windows Azure Virtual Machines by Keith Mayer. His article pertains to Windows 8 app development in Windows Server 2012; I was able to use the same process to develop Windows 8.1 apps in Windows Server 2012 R2.

No Windows 8 or 8.1, No Problem

With Windows 8/8.1, there is a lot of excitement floating around for developers that want to get their feet wet in Windows 8 app development. However, if you plan on building these apps you will need either Visual Studio 2012 on a Windows 8 platform or Visual Studio 2013 on Windows 8.1 platform.
If you plan on building Windows 8.1 apps, you will need Visual Studio 2013. Please note that VS 2013 only builds Windows 8.1 apps, not 8, and VS 2012 only builds Windows 8, not 8.1 as far as I know.
Now what if you don't have Windows 8.1, or in my case, you just upgraded to Windows 8.1 and your hard drive crashed? Then you're SOL Jon...since this is a blog about Microsoft Technologies, I took this as an opportunity to play with Windows Azure's Virtual Machines service a bit.

Setting up the Virtual Machine

By using VMs in Microsoft's cloud environment, we don't have to use our own computer resources, where as with a VHD image on your box, you're allocating space on your crashed hard drive, eating away at your pathetic 1gb of memory, etc. There are better things to use that space for...like your 80's movies collection.
Back on topic, we want to use a VM to create a Windows 8.1 image, but not so fast -- you can't run VMs in Azure with Windows Desktop OSs, only Server OSs. Surprise, surprise
As a workaround, we can develop Windows 8.1 apps in Windows Server 2012 R2 (or Windows 8 apps in Windows Server 2012) with a few tweaks. Let's get started:
First, we'll need to create a VM with a Windows Server 2012 R2 image:
In your Windows Azure Portal, click +New button -> Compute -> Virtual Machine -> From Gallery
Create New Virtual Machine From Gallery
Select Windows Server 2012 R2 Datacenter on the "Choose an Image" window.
Select Windows Server 2012 R2 Datacenter Image
Complete the prompts that follow to complete your Virtual Machine configuration.
I set my Virtual Machine Size to Large (4 cores, 7GB memory). I haven't had any problems at all, but I would recommend at least a Medium (2 cores, 3.5GB memory) size to prevent any frustration and keyboard smashing from lagging.
Once completed, your VM will begin provisioning to finish preparing the image and VM for your use. In the meantime, make yourself a milkshake...you deserve it for taking a step into a new big world. The VM's status will display as "Running" when ready for use.

Setting up Windows Server 2012 R2 for Development Use

Out of the box, you won't be able to develop Windows 8.1 apps in Windows Server 2012 R2. It's not only because you haven't installed VS 2013 yet, but without a little tweaking you won't be able get a Developer Licenses on Windows Server 2012 R2 because they aren't available for the Server OSs. Please note, that this work around is for development purposes only. If you are planning to publish your app, please do so by building and publishing on an actual Windows 8/8.1 platform. Back to the tweaking:
Okay, we want to connect to our newly built VM. To do so -- in your Windows Azure Portal click the "Virtual Machines" tab -> Highlight the VM -> Click Connect
Connect to new VM when Running
When prompted any Remote Desktop Connection Security Warning, allow the connection/validate the certificate. When prompted for credentials, enter the same one used when creating the VM.

Create New User for Development

You must create a separate user account for developing, because the Built-In Administrator account can't be used to build code in Visual Studio.
To create this developer user account, do the following:
After you've logged on, open Server Manager (if not opened already) -> Click Tools in the upper-right corner of the application -> Select "Computer Management"
In the "Computer Management" screen -> Expand "Local Users and Groups" -> Right-click "Users" -> Select "New User..."
When creating your user, make sure the "User must change password at next logon" and "Account is disabled" options are NOT checked. Click Create then Close.
Adding New User
Now let's add the new user account to the Admin group so it will have the right permissions.
In the "Computer Management" screen -> Expand "Local Users and Groups" -> Select "Groups" -> Right-click "Administrators" group -> Select "Add to Group..." -> Click "Add" in the properties window -> Add the new user and Click Ok to apply the changes.
Adding User to Administrators Group

Configure Services

Open Server Manager -> Click Tools in the upper-right corner of the application -> Select "Services"
In the "Services" screen -> Double-click "Windows Audio" service -> For Startup Type, Select "Automatic" -> Start Service -> Click Ok
In the "Services" screen -> Double-click "Windows Audio Endpoint Builder" service -> For Startup Type, Select "Automatic" -> Start Service -> Click Ok
Windows Audio and Windows Audio Endpoint Builder Services

Change IE Enhanced Security Configurations

In order to use IE as you typically would in a Desktop OS, we need to turn off the IE Enhanced Security. To do so:
Open Server Manager -> Click "Local Server" in the left navigation panel -> Click "IE Enhanced Security Configuration" on the right side of the page -> Set "Administrators" and "Users" options to "Off" -> Click Ok
Turning off the IE Enhanced Security

Update Registry for IE

To open the Registry Editor, search for "Regedit" and select the application.
In the "Registry Editor" application -> Navigate to the following path location: 
Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main
Double-click "ApplicationTileImmersiveActivation" -> In edit window, set "Value Data" to 1 -> Select the "Decimal" option -> Click Ok
Updating registry for IE

Activate Desktop Experience Feature

In order to have the OS operate similar as Windows 8.1 and have the look and feel, we must activate the "Desktop Experience" feature for the Windows Server OS.
Open Server Manager -> Click "Manage" in the upper-left corner of the application -> Select "Add Roles and Features"
In the following windows, click Next until you get to the "Features" window. Here we will:
Expand "User Interfaces and Infrastructure (Installed)" -> Check the "Desktop Experience" checkbox -> When prompted, Click Add Features -> Click Next then Install -> After completion, restart the VM from your Windows Azure Portal
Installing Desktop Experience Feature
Restart the VM

Install Microsoft Visual Studio 2013 and Windows 8.1 SDK

When logging back into your VM, use the new user account you created in the previous steps (you can use this account from here on out. You will not be able to debug/compile using the Built-in Administrator account).
Keep in mind, if you will be developing Windows 8.1 apps, you need VS 2013. For developing Windows 8 apps, you need VS 2012.
Install VS 2013 however you like, I used VS 2013 Ultimate edition but you can use the Express edition also. After the installation of VS 2013, you will need to restart your VM again.
After restarting and logging back in with your new user account, download and install the Windows 8.1 SDK from here:

Windows 8.1 SDK

Obtain Developer License for App Development

When launching VS 2013 for the first time, you should be prompted to "Get a developer license..." If so, accept and enter your Microsoft Account credentials to obtain your developer license. If you're not prompted, when the VS home page loads create a new Windows Store application. After the solution loads, click "Project" in the toolbar -> Highlight "Store" -> Select "Acquire Developer License..."
Acquire Developer License in Project
License Obtained
Now you can "officially" start developing a Windows 8.1 app without a Windows 8.1 OS. Use this Windows 8.1 in C#/VB Tutorial provided by Microsoft to get started.
Building Windows 8.1 App
Executing Windows 8.1 App on Windows Server 2012 R2

Final Tips: Saving Compute Charges in Azure and VS Online

As long as your VM is running, it is accumulating compute charges. If you want to save your money, and prevent unnecessary charges when not using the VM, shut down the VM when not in use.
Make sure to shut down your VM using the Windows Azure Portal to prevent compute charges. If you shut down from within the VM, the VM will be "Stopped" but still accumulate compute charges. Shut down in the Windows Azure Portal, and make sure the status is "Stopped (Deallocated)" to prevent compute charges, otherwise you'll pay the price. 
If you are familiar with Team Foundation Server, you know it is a great collaboration tool and you'll love this. Visual Studio now offers a hosted TFS environment online called VS Online. A great use of this is having the collaboration aspect, if needed, but also having an online repository at your disposal. So after you get your laptop back with a new working hard drive...you can grab your project and source code as you please from VS Online. There are many other benefits to using VS Online, check it out.
TFS is now VS Online
Happy Coding!!