Feb
6
2013
First off, I am no closer to being a pro at the 5 areas above then an average ice skater is a pro at hockey. Please don’t come here looking for guidance or best practice, but make use of the direction and ideas. One more disclaimer, my writing style below is personalized and is pretty much how my brain runs in the tech world. I tend to use a lot of parentheses when I type as a way to jump thoughts or give a little more detail.
My company was kind enough to provide me with an MSDN subscription, which I took advantage of last Friday evening. Granted it is to be used for work, but I am the kind of woman who is curious, full of ideas, and who likes to learn things on my own, and so I used it for “training”. In my head I have a killer idea for a Windows 8 Store App, but before this idea could ever become more I had to understand how things work. I had already setup Virtual Box VM of Windows 8 and installed Visual Studio 2012 on it to learn the tool (I wanted to be prepared when we finally upgrade in the office). I followed this site to set it up, http://betanews.com/2012/03/01/install-windows-8-on-oracle-virtual-box/.
I started with the solution. I know I wanted a Windows 8 Store App, so I surfed the web to find a walkthrough tutorial to kick start me. Of course I only got through the first page and started to lose my focus (due to surging creativity, this happens all the time). Part 1: Create a “Hello, world” app. What I ended up with was a solution with a running app that asked for a name and spit back some greeting text. My next challenge was to wire this up to a database.
I didn’t have a database, nor SQL Express installed. This is where Azure came in to play (go Cloud). I know this guy (Hi David), who LOVES Azure, and I wanted to see why he loves it. This led me to creating an Azure account (go free trial). This was simple enough to setup. I took a few moments in the Azure management tool seeing all the options available. This had change since last time I watched David demo it at a Code Camp, but I easily found the option to setup a new SQL database.
I then used the databases Management Portal to create a table and add data. I created the table with a key, Name, and FavoriteColor columns. I put in the names from my family, husband, daughter, son, and myself with our favorite colors.
Database done!
Now how do I tie them together? I’ve almost always developed in a multi-tier architect. The data layer (database), middle tier/business layer (WCF), and interface (ASP.NET/WPF). My first thought was to use WCF and Entity Framework. I’ve done this plenty of times in the past, so it’s easy for me to setup. I started with Entity first by adding a Class Library project to my Windows 8 app solution. I added the ADO Model with the connection string to the Azure database I had setup (which kindly prompted me to ensure Azure allowed my local IP address, yeah firewall). I brought in my one table into the model.
Next I added a WCF project to my solution and created a simple method that would take a name and return a color. I tested the method with VS’s WCF Test Client and retrieved the correct response.
public string GetFavoriteColor(string name) { var entity = new GMappEntities(); var row = entity.TestTables.Where(q => q.Name.Contains(name)).FirstOrDefault(); if (row == null) { return string.Empty; } return row.FavoriteColor; }
Accessing the Azure database programmatically, check!
Now for the tricky part. I knew that the Windows 8 App would most likely run more like Silverlight, where it would make Async calls (remember I haven’t worked in this area before), but I had no idea on the syntax. I tried on my own to add the new WCF Service to the Store App, but was unprepare to have only “GetFavoriteColorAsync” methods and no “GetFavoriteColorCompleted” events to tie it too. With lacking knowledge I took the next logical step and surfed the web. It took me a few sites to point me in the a path, but no real flat out “here’s how you do it, lady.” (or my search patterns sucked). My search results lead me into using WCF Data Services Tools for Windows Store Apps. I began experimenting by creating a Data Service request in an ASP.NET project (I didn’t really like this idea, but that’s because mostly I don’t understand it that well). I pretty much messed up on the calls and just gave up on it. I switched back to trying to get my WCF site to work. After more web surfing I found references to “async and await”. Await isn’t something I’ve used before (or I don’t remember using it). I dug deeper on what ‘await’ was used for, how, and why. The code began to come together and I successfully entered Traven (my boy's name) and got back Green.
private void Button_Click(object sender, RoutedEventArgs e) { GetFavoriteColor(nameInput.Text); }
private async void GetFavoriteColor(string text) { var client = new WcfTestService2.ServiceTestClient(); Task<string> getStringTask = client.GetFavoriteColorAsync(nameInput.Text); greetingOutput.Text = await getStringTask; var colorBrush = new SolidColorBrush( ); colorBrush.Color = TranslateColor(greetingOutput.Text); greetingOutput.Foreground = colorBrush; greetingLabel.Text = "Your favorite color is: "; }
The first call to Azure took about 7-10 seconds, but the subsequent calls were about 0-1.
Access Azure through WCF into the Windows 8 App, done!
This is when my six-year-old son pulled my sleeve and said “Mom, when are you going to stop working, I want to play Minecraft with you.” (Yes, it is obvious that he is my boy). I decided to introduce Traven to my little program. I demonstrated the functionality of my little app that allows you to enter a name and get back a favorite color. I showed him how I could add more names, so we put in his friends name and set orange as his favorite color. Traven thought this was pretty cool and I still had his attention, which I thought was extremely cool. Then he said “Mom, shouldn’t the words be the color.” He stayed with me for the next fifteen minutes while we worked out setting the foreground color based on the text color from the database.
When dad finally came home from work, Traven bounced out of our office to tell him all about the color program we made. Learning all of this was awesome, but sharing it with my six-year-old was absolutely awesome (aka priceless, but with more excitement).
Feed the brain people and share!
I also found these sites helpful:
http://bhrnjica.net/2011/09/26/consuming-wcf-in-windows-8-metro-style-app/
http://www.c-sharpcorner.com/uploadfile/UrmimalaPal/creating-a-windows-phone-7-application-consuming-data-using-a-wcf-service/
http://www.techrepublic.com/blog/programming-and-development/my-first-windows-8-application/5273