DEVELOPING WINDOWS AZURE APPLICATIONS

Windows Azure is not just about services. It is an ever-evolving cloud platform that has a set of tools and SDKs that enable you to get started quickly developing cloud applications.

Getting started with Windows Azure requires the following:

  • Visual Studio (2010 or 2012)
  • Windows Azure SDK and Tools for Visual Studio
  • Windows Azure subscription

You can also integrate different SDKs and tools with Visual Studio 2010 or Visual Studio 2012. The Windows Azure tools and SDK are free downloads. After you download the tools and SDK, you must set up a Windows Azure account. You can sign up for a free 90-day account to get started.


NOTE Remember that the cloud is about pay-per-use: as you use more compute, storage, data, and other service capabilities, the more you’ll be charged per month.

After you download the Windows Azure tools and SDK and create a Windows Azure account, you can use Visual Studio to build and deploy applications to your account. When you’re ready you will be able to test, stage, and ultimately deploy your applications to a production environment. While you’re developing (and especially while you’re developing offline), you can use the emulator — an emulated Windows Azure environment that is installed with the tools — to test your applications locally.

Figure 5-5 illustrates at a high level the process of developing cloud applications. In the diagram, you can see the left side represents a local development environment. Installed in this environment are the tools, SDK, and emulator as well as the .NET and Visual Studio development toolset. (Windows Azure also supports PHP, Java, Node.js, and other types of non-Microsoft technologies through SDKs and community tooling.) You can build and test your applications locally, but at some point you’ll need to deploy these apps to your production account, and which account you choose depends on what licensing structure your company has purchased (for example, basic subscription or pay-per-use account versus enterprise license account). In any case, you deploy into an account using a LiveID (or Microsoft account ID) username and password information. The Windows Azure account has a unique subscription ID and allows you to add co-admins so others can manage the services that are deployed to it. Note in the right side of Figure 5-5 how the cloud environment is hosted in a physical data center (as shown earlier in the chapter); thus your application, service, or data becomes available to others when you deploy into this production environment. You can lock down the applications (using Windows Azure Active Directory technology) and have an authenticated, single sign-on experience, or you can have a fully anonymous-access site or service that is leveraged by your application.

A number of elements must be in place to get started developing, so the following exercise walks you through where you can find the software to set up your development environment.


TRY IT OUT: Setting Up Your Development Environment
To set up your Windows Azure development environment, you first install Visual Studio and then the Windows Azure tools install on top of it.
To install the trial version of Visual Studio:
1. Download and install Visual Studio 2012. At the time of this writing, you could get the trial edition at http://www.microsoft.com/visualstudio/en-us. You have your choice of different SKUs (or Visual Studio versions) available; choose Professional or above.
2. When you click the appropriate SKU, select Run when prompted. Visual Studio asks for a download location and begins installing the product.
To install the Windows Azure SDK and tools:
1. Download and install the Windows Azure tools for Visual Studio 2012. At the time of this writing, you could get the tools at http://www.windowsazure.com/en-us. You can also get the software and tools through the Web PI: http://www.microsoft.com/web/downloads/platform.aspx.
2. Click the Develop link, which takes you to all the available SDKs and tools. Under languages, click .NET, the first option shown in Figure 5-6.
3. Click the Install button, and then select Install with Visual Studio 2012.
4. Select Run when prompted, which invokes the Web Platform installer.
5. Click Install, as shown in Figure 5-7, and then follow the wizard to accept the license and begin the installation process.
How it Works
The installation process is fairly self-explanatory; just follow the wizard. If you want to download a permanent version of Visual Studio, you can visit http://www.microsoft.com/visualstudio/en-us and you should be able to buy the most recent version. If you’re an MSDN subscription holder, you should be able to download Visual Studio 2012 under your subscription license there.

After you’ve installed Visual Studio and the Windows Azure tools/SDK for Visual Studio, you need to sign up for a Windows Azure account. To do this, click the Home link on the main Windows Azure site, click the Free Trial button, and then click the Try it Free∗ button (see Figure 5-8). You’ll be prompted for your LiveID and a wizard will walk you through a sign-up process. After you sign up, you’ll then be able to navigate to your portal and begin creating cloud services, websites, and so on.

Now that you have your development environment up and running and a Windows Azure account ready to go, you’ll want to get hands on with developing for Windows Azure. To do this, the following Try It Out walks you through creating your first Windows Azure application. The application is a simple REST-based Web API project that will return some hard-coded sales data.


TRY IT OUT: Creating Your First Windows Azure Application
To create your first Windows Azure application, perform these steps:
1. Open Visual Studio 2012.
2. Click File ⇒ New Project and select Visual Studio Solution.
3. Right-click the solution and select Add ⇒ New Project.
4. Select Cloud, and provide a name for your project: MyFirstAzureApp.
5. Add a location and click OK (see Figure 5-9).
6. In the New Windows Azure Cloud Service dialog, shown in Figure 5-10, select the ASP.NET MVC 4 Web Role project.
7. Click the right-arrow button to add the Web role to the Azure solution. Click the pencil icon to edit the name of the Web role (MyFirstAzureWebAPI).
8. Click OK.
9. In the New ASP.NET MVC Project dialog, select the Web API option.
10. Leave the default option, and click OK.
How it Works
Visual Studio creates the plumbing for a new Web API project, which includes a Windows Azure (Cloud) project and the Web API (MVC) project. Figure 5-11 highlights four parts of the solution for your benefit. The Cloud project represents a “wrapper” project around the Web API MVC project. The Cloud project contains configuration information and links autodeployment and publishing features for developers to easily deploy their code to Windows Azure. The three other parts are standard MVC (that is, model, view, and controller) components. MVC components process incoming requests, manage user input and interactions, and execute application logic. The controller is typically separate and is created to generate an HTML view of the request. The model is, for example, a class that is used to model incoming or connected data. The view is what is displayed to the user.

The next series of steps in the process of creating an application with Windows Azure entails creating a simple model to represent data. The following exercise walks you through this process.


TRY IT OUT: Creating a Model (MyFirstAzureApp.sln)
Now create a simple model to represent the data (which will be a small set of sales data), and then expose that data as a return value using the default controller. The example uses the default plumbing code as much as possible to keep things straightforward.
1. Right-click the Models folder and select Add ⇒ Class. Name the class Sales and then click Add.
2. Add the following bolded code to the Sales class, which provides you with a small set of properties for your custom Sales object.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
 
namespace MyFirstAzureWebAPI.Models
{
    public class Sales
    {
        public string Company { get; set; }
        public string FY09Sales { get; set; }
        public string FY10Sales { get; set; }
        public string FY11Sales { get; set; }
    }
}
Now that you’ve created a model (or class), you’ll use this class to create a return List collection object that your Web API service will return in JSON format.
3. Expand the Controllers folder and double-click the ValuesController class.
4. Amend the class with the following bolded code (note these are fictional companies and sales figures).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using MyFirstAzureWebAPI.Models; 
 
namespace MyFirstAzureWebAPI.Controllers
{
    public class ValuesController : ApiController
    {
        // GET api/values
        public List<Sales> Get()
        {
            List<Sales> mySales = new List<Sales>(); 
 
              mySales.Add(new Sales
              {
                    Company="Contoso", 
                    FY09Sales="$9,020,398,122,332.00", 
                    FY10Sales="$10,111,309,312,998.00", 
                    FY11Sales="$11,033,990,102,443.00"
              });   
 
              mySales.Add(new Sales 
              {
                    Company="Fabrikam", 
                    FY09Sales="$7,332,444,552,112.00", 
                    FY10Sales="$5,019,132,011,668.00", 
                    FY11Sales="$3,889,940,589,901.00"
              });
 
              mySales.Add(new Sales
              {
                  Company = "Wingtip",
                  FY09Sales = "$9,032,522,000,129.00",
                  FY10Sales = "$9,115,339,990,899.00",
                  FY11Sales = "$9,001,439,321,666.00"
              });
 
              return mySales; 
        
        }
...
 
    }
}
The code that you added uses the Sales object to create a List collection object. When you append the Web API URI with "api/values" it will return the three Sales objects within the List collection as a JSON formatted object.
5. Press F6 to build.
6. When you’ve added and successfully compiled the code, press F5 to debug the cloud application. (The solution uses the local cloud emulator to debug the Web API.)
7. When debugging, you’ll see the ASP.NET default page appear (within the 127.0.0.1 domain). When you append the "api/values" path to the URL (as in Figure 5-12), the Web API service passes back a JSON-formatted object.
How it Works
The MVC templates are a versatile set of ASP.NET Web templates, and the Web API is a specific template that enables you to easily build REST-based services for Windows Azure. You’ll find that Cloud services (such as REST or WCF-based services) will be important to your cloud development efforts for a number of reasons, such as service reuse or application extensibility. In this exercise, you built a REST API with the scaffolding provided by the MVC templates, and the result was a REST service that returned a JSON object.
The JSON object, shown in the following code (with purely fictional data), reflects the List collection you created in the Get method. In essence, this is a small set of data that is returned to you via the REST service call.
[
{
"Company":"Contoso",
"FY09Sales":"$9,020,398,122,332.00",
"FY10Sales":"$10,111,309,312,998.00",
"FY11Sales":"$11,033,990,102,443.00"
},
{
"Company":"Fabrikam",
"FY09Sales":"$7,332,444,552,112.00",
"FY10Sales":"$5,019,132,011,668.00",
"FY11Sales":"$3,889,940,589,901.00"
},
{
"Company":"Wingtip",
"FY09Sales":"$9,032,522,000,129.00",
"FY10Sales":"$9,115,339,990,899.00",
"FY11Sales":"$9,001,439,321,666.00"
}
]
When deployed, the REST service would behave similarly, except the endpoint would be configured to a production URI (for example, http://myapp.cloudapp.net).

Congratulations! You’ve created your first Windows Azure application; that is, a REST-based Web API that returns JSON data. When you deploy it to a production environment, you can use the REST service from a multitude of clients and process the JSON data within your applications — whether it is SharePoint, Windows Phone, Windows 8, or other device/tablet applications.

Even before you deploy your first application to your Windows Azure account, you can use this REST service locally within the cloud emulator environment with other applications. Although you won’t deploy this application to Windows Azure now (you’ll have the opportunity to deploy many different applications to Windows Azure throughout the book), you can right-click the cloud project (MyFirstAzureApp) and select Publish. This invokes the Publish Windows Azure Application dialog, which enables you to walk through a wizard and deploy your application directly to your Windows Azure subscription — which you can then manage within the Windows Azure portal. See Figure 5-13 for the Start page of the Publish Windows Azure Application wizard.

At this point, you should at least have a basic understanding of what Windows Azure is, how to set up the Windows Azure development environment, and the types of applications that you can build using Windows Azure. You might now be asking yourself, “Why all the Windows Azure hubbub?” When paired with SharePoint 2013, Windows Azure becomes important in two ways:

  • It is natively integrated within the SharePoint application development and deployment experience — you use Windows Azure to build and deploy cloud-hosted applications.
  • You can also use Windows Azure in the broader cloud application development experience — just like you could use an array of other Web technologies and standards.

The next section of this chapter covers how SharePoint and Windows Azure integrate both in general and through the two main app models: the Autohosted model and the Provider-hosted model, covered in Chapter 2, “Overview of the SharePoint 2013 App Model.”

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset