DPS907 notes – Tue Nov 12

Topics that support the programming assignment. Windows Azure deployment.

.

Is my data up-to-date?

Your web service will be used by different kinds of apps – mobile, web, desktop, etc.

If an app keeps a copy of some of the data from your web service, how can the app determine if it has up-to-date data?

One technique is to use a “rowversion” SQL type in the database table. Read the linked document before continuing.

How can we use this technique? Well, simply decide what entities should be covered by the technique.

Then, in the entity class, create a property named “RowVersion”; its data type is byte[]. In addition, it should be read-only, so customize the property definition to include only the getter (and remove the setter).

Finally, in the “…List” and other fully-configured view model classes, include the RowVersion property. (The reason for including it in the “…List” view model class is to give the requestor the data they need to make a decision about the validity of their locally-stored data. A “…List” view model class is bandwidth-friendly.)

In a mobile device app, for example, that app’s code would simply compare its own locally-stored value for that object with the value received in the response. If they don’t match, the app would (on its own timetable) update its locally-stored object if it has received a fully-configured view model, or make another request for up-to-date data.

.

Handling exceptions, again

Today’s code example, “ExceptionFilter”, introduces you to a good way to handle and log general exceptions in your app.

You would add this capability part-way through your development, so that you can effectively test.

Inspiration for this topic was taken from the Exception Handling in ASP.NET Web API article.

.

Windows Azure platform

Windows Azure is a global cloud platform for hosting your web apps and web services.

Your programming assignment will be deployed on Windows Azure, so that it will be publicly available.

Anyone can get a free 30-day trial. As a student in this course, you can also get a free 150-day “academic pass”.

You must have a Microsoft Account to use the Windows Azure platform. Visit the Microsoft Account web site for more information, and/or to sign up for a Microsoft Account.

.

Windows Azure academic pass

WindowsAzureGetStartedPDFTo get started, read the Get started with Windows Azure for students document.

You will receive a “promotion code”, which will enable you to request an academic pass.

After you submit your request, you will receive an acknowledgement message. Then, later (could be a few minutes, hours, or a day or so), you will receive your login information.

.

Windows Azure SDK for Visual Studio 2012

The College computers are already configured with the software you need to deploy an app to Windows Azure.

However, on your own computer, you must install the Windows Azure SDK for Visual Studio 2012. Visit this page to get started:

http://www.windowsazure.com/en-us/downloads/?sdk=net

Windows Azure SDK for VS

.

Note: The email that comes from the Microsoft has an old link to the SDK. Do not use that link.

.

Windows Azure management portal

Manage your Windows Azure resources by using the management portal.

Your academic pass enables you to create ten (10) web sites, and two (2) databases.

  • A database can be used by more than one web site
  • In addition, you can also create a web site with its own “free 20MB SQL database” (only one per student however)

These instructions may be edited to improve their quality. 

.

Create a web site with a database

Get started by creating a web site in the Windows Azure management portal. Choose the “custom create” option:

  • The web site name should include part of your name, and part of the web site purpose
  • For example, a web site for the programming assignment could be named “peter-assignment.azurewebsites.net”
  • Choose the US West region
  • Choose the “Create a new SQL database” option
  • In the “DB CONNECTION STRING NAME” field, enter “DataContext”

Continue with the database configuration:

  • Choose a database name that could be reusable with more than one of your web sites
  • Write down (or save) the database admin credentials; these will be different from your Microsoft Account credentials
  • As above, choose the US West region

The portal will create your resources in the next few minutes, and will notify you when your web site is ready. Your web site will include one simple HTML document, named “hostingstart.html”. It will open in a browser.

.

Configure the “Help” page functionality

In your app, the “Help” page functionality requires a configuration change. Do the following:

  1. In Solution Explorer, show all files
  2. In the App_Data folder, right-click the XmlDocument.xml file, and choose “Include In Project”
  3. Now, right-click the XmlDocument.xml file again, and choose “Properties”
  4. In the “Copy to Output Directory” setting, choose “Copy Always”
  5. In Solution Explorer, you can toggle the ‘show all files’ if you wish

.

Configure Code First Migrations

Windows Azure web sites that use a database MUST be configured with Code First Migrations.

As an overview, there are two configuration tasks that must be done to your app: 1) configure Code First Migrations, and 2) move your store initializer code.

Configure Code First Migrations

Assuming that your project does not yet include the Code First Migrations configuration, open the Package Manager Console (it can be found on the TOOLS menu in Visual Studio 2012).

Run the command “enable-migrations”. It will create a Configuration.cs source code file, and will also create a source code file that ends with “…InitialCreate.cs”. If that’s what you see, then you’re done this task.

Move your store initializer code

In your WebApiApplication class file (Global.asax.cs), disable/comment out the statement that runs the store initializer code.

Next, in your store initializer class, you have code that creates new objects. You must move that to the “Seed” method in the Configuration.cs source code file.

Wrap that code in an “if” test. You know that a new database will be empty. Therefore, check whether whether an entity collection is empty (e.g. context.Programs.Count() == 0), and if so, run the data creation code.

.

Get your web site’s “publish profile”

Windows Azure Management Portal Web Site PropertiesIn the Windows Azure management portal, select your web site, which opens a property sheet.

You will need a “publish profile”. This is a digital document that enables you to use Visual Studio to publish – deploy – an app (and its database/persistence configuration) to a Windows Azure web site.

Locate the link, and click to download. Save it in the folder that holds your ‘solution’ file. (This folder also has a folder with your project files, and the “packages” folder.) 

.

Publish your app

In Visual Studio’s Solution Explorer, right-click your project item, and choose “Publish…”. This is a four-panel dialog that needs to be configured. The left side has links to the four panels. Start with the “Profile” panel.

Profile panel

Import a publish profile, by selecting the item that you just downloaded above.

Windows Azure Publish 1 Profile

Connection

This panel is filled in with the settings in the publish profile.

Click the “Validate Connection” button, and ensure that you can continue.

Windows Azure Publish 2 Connection

Settings

The “Databases” area shows your project’s connection strings. For the DataContext connection string:

  1. Ensure that the connection string name is filled in; you may have to select it
  2. Ensure that the “Execute Code First Migrations…” check box is checked/selected

Windows Azure Publish 3 Settings

Preview

Use this panel to preview then publish your project.

Click the “Start Preview” button, and after a few seconds, all your project files are shown. On future publishing updates, the list will be smaller.

Click the “Publish” button. The first time you publish your web site, it takes awhile, because there’s about 50MB or so of content to upload. On future publishing updates, the process is faster.

Windows Azure Publish 4 Preview

After your app has been published, it opens in a web browser. It’s now ready for the public to use. You can get to it with Fiddler or any other web API client.

.

Publish an updated version of your app

After you make more changes to your app, you can publish an updated version of your app.

It’s simple:

In Visual Studio’s Solution Explorer, right-click your project item, and choose “Publish…”.

The “Publish Web” dialog appears, showing the “Preview” panel.

Click the “Start Preview” button, and after a few seconds, a list of updated project files are shown.

Click the “Publish” button.

.

.

.

.

.

.

.

.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: