Chapter 17
Advanced Cloud Programing and Deployment

Wrox.com Code Downloads for this Chapter

The wrox.com code downloads for this chapter are found at www.wrox.com/go/beginningvisualc#2015programming on the Download Code tab. The code is in the Chapter 17 download and individually named according to the names throughout the chapter.

Now that you have spent some time learning about the cloud and cloud programming, let's move forward and write some C# code that is a little more complex than what you did in the previous chapter. In this chapter, you continue exploring both ASP.NET and Microsoft Azure. You will modify the CardLib program so that it runs in the cloud as an ASP.NET Web API and, once deployed, you consume it from an ASP.NET Web Site.

After creating, deploying, and consuming the ASP.NET Web API, you will learn how to scale it. The concept of scaling is important to grasp in the event that the cloud program you create becomes popular. The example in this chapter uses free Microsoft Azure cloud resources. These free resources have low CPU, memory, and bandwidth thresholds and are easily breached under high usage. You will learn how to avoid any suspension of your cloud program due to resource threshold breaches by scaling when appropriate.

Creating an ASP.NET Web API

The Application Programming Interface (API) computer programming concept has been around for many decades and is generally described as a module that contains a set of functions useful for building software programs.

Originally, from a Windows client application perspective, these modules were dynamic linked libraries (.dll) and exposed programmatically accessible interfaces that exposed internal functions to other programs. In such a system, when a consuming program uses an API, it becomes ­dependent on the pattern of the interface. Changes to the interface cause exceptions and failures within the consuming program because the current procedure to access and execute the functions within the module is no longer valid. Once programs become dependent on an interface, it shouldn't be changed and when it is changed the event is commonly referred to as DLL Hell. For more information about DLL Hell, read this article: http://www.desaware.com/tech/dllhell.aspx.

As time moved on and the implementation of Internet and intranet solutions became mainstream, dependencies on technologies such as web services and Windows Communication Foundation (WCF) were made. Both web services and WCF exposed formal contractual interfaces that exposed the functions contained within them to other programs. As opposed to the previously mentioned DLL API where the module exists on the same computer as the one consuming it, the web service and WCF are hosted on a web server. As a result of being hosted on an Internet or intranet web server, access to the web interface is no longer confined to a single computer and is possible from any device, from any place with an Internet or networked intranet connection.

Recall from the previous chapter where the analysis of the cloud optimized stack took place. From the discussion you learned that in order to be considered cloud optimized, a program must have a small footprint, be able to handle high throughput, and be cross-platform enabled. An ASP.NET Web API is based on the ASP.NET MVC (Model, View, Controller) concept, which aligns directly with the new cloud optimized stack definition. If you have created and/or used web services or WCF in the past, you will see how much simpler and compact an ASP.NET Web API is in comparison. If you have never used either, take my word for it: It is.

In the following Try It Out, you will create an ASP.NET Web API that deals a hand of cards.

Now that the ASP.NET Web API is created, move on to the next section to learn about deployment and then consumption of the Web API.

Deploying and Consuming an ASP.NET Web API on Microsoft Azure

There are numerous options for deploying your Web App to the Microsoft Azure platform. One of the most popular methods is via a local Git repository or a public Git repository hosted on GitHub. Both the local and public Git repositories provide capabilities for version control, which is a very useful feature. Having version control lets the developer and release manager know what specific changes have been made, when they were made, and by whom. In the event that there are problems or unexpected exceptions when the binaries are compiled or the changes are deployed to the live environment, it is easy to find who to contact about it. Other deployment platforms that can be integrated into Microsoft Azure include Team Foundation Services, CodePlex, and Bitbucket, for example.

As the code in the previous Try It Out is a standalone project that is not contained in a version control repository, the deployment is performed directly within the IDE, in this case Visual Studio 2015. Additional methods for deploying a solution not contained in a source code repository include, for example, Web Deploy (msdeploy.exe) and FTP.

Complete the following Try It Out to deploy an ASP.NET Web API to a Microsoft Azure Web App.

You might consider deploying this ASP.NET Web Site to the Microsoft Azure platform using the acquired knowledge from the previous Try It Out. For example, simply right-click the Ch17Ex02 solution, select Publish Web App, and follow the publish wizard. Creating a Web App called ­"handofcards-consumer" would then be accessible from http://handofcards-consumer.azurewebsites.net/. As both the ASP.NET Web API and the Microsoft Azure Blob Container are accessible on the Internet from any place in the world, placing the ASP.NET Web Site on Azure would result in the same outcome (getting a hand of cards).

Over time, if either the consumer or the API become popular and begin receiving many requests, running the Web Apps in FREE mode would likely result in a resource threshold breach that renders the resources unusable. This would not be ideal. In the next section, you learn how to scale an ASP.NET Web API running as a Web App on the Microsoft Azure platform so that users and customers can access your responsive web resource when required.

Scaling an ASP.NET Web API on Microsoft Azure

Scaling to meet the requirements of your users used to be a very tedious, time-consuming, and expensive activity. Historically, when a company wanted to increase server capacity to support more traffic, it required the acquisition, assembly, and configuration of physical hardware into a data center. Then, once the hardware was on the network, it was handed over to the application owners to install and configure the operating system, the required components, and the application source code. The time required to perform such tasks resulted in companies installing a lot of physical capacity to manage peak time usage; however, during times of non-peak usage, the extra capacity simply went unused and sat idle, which is a very expensive and non-optimal way to allocate resources.

A better approach is to use cloud platforms, like Microsoft Azure, that provide the ability to optimally utilize physical resources to scale up, down, and out during the times when the resources are required. When you need physical resources like CPU, disk space or memory, you scale up or out to meet the demands, and when the demand for your cloud-hosted services reduces, you can scale back down and save your financial resources for use with other projects and services.

The remainder of this chapter illustrates how to scale an ASP.NET Web API based on CPU demand and during a specific time frame.

The auto scaling feature is very useful for managing unexpected peaks of usage and requests to your Web App. However, if you already know when your customers or users interact with your Web App, you can plan ahead and have the additional instances available slightly before they are actually needed. The benefit is that instead of a gradual increase or decrease of instances based on CPU usage, you can scale immediately to the number of CPUs and and amount of memory required during only that specific timeframe. For example, if you know that your marketing department is running a campaign during the month of October, you can schedule additional resources to be available during that month. By having the required resources available and warmed up, you can avoid any delay in getting them allocated for use by your users or customers. Perform the steps described in the following Try It Out to see how.

When you create a schedule for scaling your Web App, a name, start date, start time, end date, and end time are required. With this information, the Microsoft Azure platform manages the number of available instances, which are virtual machines that serve requests to your Web App during the configured time frame. It is possible to create numerous schedules, each having its own number of instances and scale settings. Simply create the schedule, save it, and when it's needed select it from the schedule drop down, and the resources will become available as expected.

image What You Learned in this Chapter

Topic Key Concepts
ASP.NET Web API An ASP.NET Web API is an Internet or intranet interface that exposes methods for consumption from external programs.
Deploying to the cloud Use tools like Visual Studio, WebDeploy, Git, or FTP to deploy your program to the cloud.
Consuming a Web API An ASP.NET Web API returns the output of the method in a JSON file. Use the Newtonsoft.Json class library to parse and use its content.
Scaling in the cloud Microsoft Azure Web Apps let you auto scale based on a defined schedule or CPU usage. Being able to scale up when you need more of a resource and down when it is no longer needed is one of the most valuable benefits of the cloud.
..................Content has been hidden....................

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