Join Michael Lehman for an in-depth discussion in this video Cloud Notes app: Connecting to Windows Azure, part of Using Windows Azure with Windows Store Apps.
We're going to build up the changes to notes model, changes our app from Plain Ol' Notes to cloud notes by doing this incrementally. First we're going to implement insert. Then we're going to implement update, make sure that that works. Then we'll implement delete. And then finally we'll go out and implement the query logic inside load data to load our data from our, quote-unquote, cloud storage on our local Windows Azure storage emulator. So we begin to do this by creating a method that we're going to call connect to Azure.
Right here, public async, because we're going to call the window's Azure APIs which are always sync, void ConnectToAzure. As part of our process, we're going to create some storage credentials, a Cloud storage account. And then a table client. We're going to be using those properties throughout the rest of our code, so we're going to create the properties up here at the top so that we can use them in multiple methods, so we need, first of all, a private CloudTableClient, tableClient, and once again we should get our favorite red squiggly.
Right click, Resolve and select Using Windows Azure Storage Table. We also need a private CloudTable for our table, and since we already have our Using here for Windows Azure Storage Table. We're good to go. Let's come back down to Connect to Azure and put some code in. So the first thing we want to do is to say, var credentials equals new Storage Credentials. Then we're going to pass in a couple of strings. Now once again, right-click, come up to Resolve, and include Microsoft.WindowsAzure.Storage.Auth.
And now we've got to put the right values in here. So I'm going to do a little cutting and pasting from a previously updated version of this app, so that I don't have to type in the extremely long Base64 key. You can find the Base64 key and the name of the storage account in one of the blogins where it's linked to from the links file. So the storage account is called devstoreaccount1. That's the only account that's available in the Windows Azure storage emulator so that's all you need to do there. Now, in terms of the key, I'm going to come back over here and create another version of Visual Studio and open a version that already has all the changes made to it and copy out that particular line.
So we're going to update line 43 there to have this nice, long Base64 key. That's described in that blog entry. If you want to connect up your app to a real live Windows Azure cloud service you're going to change both the name of the account here and you're also going to change the key. And the next thing we're going to do is create a cloud storage account, and as part of that process there's also three URI's that get included in that. You'll also need to change those to point to your real Windows Azure instance.
So once again for the purpose of simplicity, I'm going to copy the stuff out and then I'll go through and explain what's in there. So I'm going to copy lines 62 through 65 here, and come back in and paste them into our new code here at line 44. And let's talk about what's in here. We're going to create a new cloud storage account, again we've gotta resolve that reference, and that reference comes from Microsoft.WindowsAzure.Storage, because it's not part of the table logic, it's just part of the overall storage API. As you can see that the cloud storage account constructor here has storage credentials and three URIs.
As part of its constructor signature. Now, in our case, for each one of these URIs, we're passing in a non secure HTTP link. Because we're talking to our local machine. And our local machine is always 127.0.0.1. And each one of these things uses a unique port. In the case again of the storage emulator that's 10,000, 10,001, and 10,002. And you can see that after the location of your server, you also have to put your account name. So, that again, if you were connecting this up to a live as your instance, you would change this, account name here on line 43.
And you would change both this part of the URI, the address of your server, and this part, the name of your account, for each one of these to match your real live Windows Azure instance. Now that we've got credentials, we need to create a cloud table client. So far we haven't actually talked to Windows Azure at all. We're just preparing a collection of objects in a hierarchy that's appropriate for invoking the windows as your table APIs. So, the next thing we need is a table client. So, we're going to say table client is equal to count.create cloud table client.
As you can see, you could've created a blob client or a cue client, but in our case, we're going to do a table client cause that's where we're going to put or data. That's all we need. The last thing we need is to get access to our table so we're going to say table equals table client.GetTableReference and here we passed the name of the table so we're going to call this notes data. So we've done everything we need to do to get set up to go call the Windows Azure table API. And all we have to do now is say await table.CreateIfNotExistsAsync. And you notice this doesn't have any parameters because we've done all the work beforehand.
We've created on line 43 there, a storage credentials object. On line 46, we created a storage account using those credentials. On line 51, we created a table client from the account. And on line 52, we created a table reference from the client. And finally, the last step is to call await table.CreateIfNotExistsAsync. Before we run this, let's go to the server explorer and see what we've got. We'll come to Windows Azure. Storage. Open that up. Development, open that up. Tables, and you can see there's no triangle next to it.
That means there's no tables existing currently in our Windows Azure storage emulator. When we run this now, we're not going to see any different behavior of our app. But we will be able to go to the server explorer and see that our table's been created. When you run a Windows Store app, as I'm sure you're already familair with, you get to choose where it's going to run. In our case, we're going to run this on the simulator. When you drop this down, you know you can select simulator, local machine or remote machine. So we're going to go ahead and run this on the simulator and all that should happen is that it should operate exactly as it did before, meaning we're going to see three precant notes that come from our xml file but when we go to the server explore after we've run it we should see our new notes data table inside our windows Azure development storage emulator.
So, let's give it a shot. First it builds, then deploys, and then fires up the simulator and runs our Plain Ol' Notes app, and there are pre-canned, plain old notes, but now, we'll go ahead and come back to Visual Studio and press Stop, and come to the Server Explorer, then we come down here to Storage, Development. Tables and we see that we still don't have a triangle. Why didn't we get any new tables the answer is we created this great method ConnectToAzure, but we didn't call it from anywhere, so let's come down here to LoadData, which is where we're always going to come when we first start the app, and let's call ConnectToAzure and run it again.
This time it's actually going to connect to Azure and create our table. Now we come back, click Stop. You notice it still doesn't show us that we have any tables. But we can right click here and select Refresh. Now we've got a triangle. Now we open up, there's our notes data table. If we double click here You'll see that we don't have any entities all we have is an empty schema with a partition key, a row key and a time stamp. That's fine for now we're going to go ahead and close this and now we'll move on to implementing insert so that we can actually add something inside our Plain Ol' Notes application now quickly becoming Cloud notes and actually insert it into the database.
- What is Windows Azure?
- Creating Azure tables
- Inserting, updating, and deleting data in tables
- Uploading a blob to Windows Azure storage
- Using shared access signatures
- Implementing mobile services
- Adding push notifications