Lập trình .net 4.0 và visual studio 2010 part 62 pps

11 290 0
Lập trình .net 4.0 và visual studio 2010 part 62 pps

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

CHAPTER 16    411 Windows Azure Availability: Framework 3.5+ The research company Gartner defines cloud computing as: “A style of computing where massively scalable IT-related capabilities are provided as a service” http://www.gartner.com/it/page.jsp?id=707508 Windows Azure is Microsoft’s entry into the cloud-computing arena and competes with offerings from established heavyweights such as Amazon and Google. Windows Azure applications are created using standard .NET technologies and developers will be glad they will not need to learn many additional techniques to get up and running. The complexity of the Azure infrastructure is hidden from the developer and managed by Microsoft, allowing you to concentrate on developing your application without having to worry about maintaining the platform your application runs on. Once your application’s development is complete, you can then deploy it to Windows Azure, taking as much or as little of Microsoft’s massively powerful infrastructure as you need. Unlike traditional hosting, you can instantly scale up the infrastructure your application runs on to meet demand. Hosting applications and services within the cloud environment poses a number of challenges to the developer, such as how to authenticate requests, store data, and route messages, so in addition to the core Azure offering, Microsoft offers a number of services or building blocks to assist you with conquering these problems. WARNING: AZURE IS WORK IN PROGRESS At the time of writing, Windows Azure was not finalized, so functionality and screenshots may differ upon release. This chapter was written with Windows Azure Tools VS2010 Beta 2. CHAPTER 16  WINDOWS AZURE 412 Azure Overview Microsoft’s cloud computing offering can be divided into a number of stand-alone modules or services: • Windows Azure Platform • Microsoft .NET Services • SQL Azure (formally SQL Data Services) • Live Services Books could be written on each of these areas (and will be) so in this chapter we will be looking only at the core building block services to get you started. We will be looking at • Windows Azure Service Platform • Azure Storage And we will be taking a brief look at • SQL Azure • Microsoft .NET Services RANDOM TRADEMARK FACT In 2007 computing manufacturer Dell tried to trademark the term “cloud computing.” http://tarr.uspto.gov/servlet/tarr?regser=serial&entry=77139082 Architecture Windows Azure is made up of a huge number of connected virtualized servers running Windows 2008 64-bit edition and a hypervisor (at the simplest level, a hypervisor allows multiple instances of an operating system to run on a single machine at the same time) optimized for Windows Azure and based on Hyper V technology. This collection of servers is referred to as the fabric, in that its usage should appear seamless to the developer. Currently there are two Azure data centers in North America with plans to open additional centers in Chicago (USA) and Dublin (Ireland). An individual server runs a number of virtual machines, which, in turn, host a number of roles (or instances of an Azure application). Each Azure instance has dedicated an individual virtual machine, which does not have access to the other virtual machines for security and resource allocation purposes. Individual servers are connected by the fabric controller and Windows Agent (see Figure 16-1). The fabric controller additionally provisions, deploys, and monitors service health. It is important to note that an instance of Azure is never accessed directly but routed through a load balancer. When you request more than one role instance, Windows Azure physically allocates them in the safest, most redundant manner possible. This means your application will run on a server in a different rack and subnet than other roles you may have. CHAPTER 16  WINDOWS AZURE 413 Figure 16-1. Azure architecture Will I Be Able to Install My Own Version of Windows Azure? Currently no, although a number of people have made this request. Arguably you may be missing one of the major benefits of cloud computing—outsourcing infrastructure management. Do, however, expect to see some of the advances on the Azure platform, such as the hypervisor and virtualization, creeping their way into Windows Server in the future. Before You Begin Although you can work through many of the examples locally, it’s more fun, if you can, to deploy them to the cloud—even if your application just prints out “Hello Azure”! So before you do anything else, sign up for an Azure account at http://www.microsoft.com/windowsazure/account/.  TIP MSDN subscriptions now come with Azure time. CHAPTER 16  WINDOWS AZURE 414 Installation If you are working with Visual Studio 2008, you will need to download and install the Azure SDK and Tools. At the time of writing, when you create a new Cloud Service project in VS2010 for the first time, it will download the latest version of the Azure tools. Web Roles The first type of application we will create is called a web role. In Windows Azure the term web role refers to an application that is accessible over HTTP (which in our example is an ASP.NET application) although it is also possible to create other types such as ASP.NET MVC, Delphi, PHP, Ruby, or even a C++ application (through a CGI web role). It’s also probably worth mentioning that Azure applications operate in full trust. Hello Azure We will now create and deploy a very simple application that reads a value from a configuration file and prints it to the screen with the current time and date. 1. Load up Visual Studio. 2. Create a new Windows Azure Cloud Service project called Chapter16.HelloAzure. 3. Visual Studio will now ask you what roles you want to create within your project (Figure 16-2). Figure 16-2. Adding a web role to project 4. Select ASP.NET Web Role and click the > button to add it into your solution. 5. Right-click on the role in the right-hand pane, select Rename on the context menu, and call it Chapter16.WebRole. 6. Click OK. CHAPTER 16  WINDOWS AZURE 415 7. Visual Studio will now create two projects within the solution (Figure 16-3): Chapter16.HelloAzure Chapter16.WebRole Figure 16-3. Azure project layout Chapter16.WebRole This is a standard ASP.NET project that references some Azure-specific functionality. Chapter16.HelloAzure The Chapter16.HelloAzure project describes our application to Windows Azure and allows us to configure it for the Azure platform. We will use the output from this project when we deploy our application. Chapter16.HelloAzure contains two (annoyingly similarly named) configuration files you will not have come across before: • ServiceDefinition.csdef • ServiceConfiguration.cscfg CHAPTER 16  WINDOWS AZURE 416 ServiceDefinition.csdef ServiceDefinition.csdef is responsible for • Describing your application's requirements • Defining configurable settings that your application will use (the actual values are defined in ServiceConfiguration.cscfg) • Configuration settings applicable to all instances of your application ServiceConfiguration.cscfg ServiceConfiguration.cscfg is responsible fors responsible for • Defining the values of your configuration settings for each role • Determining the number of instances of your application to create Azure and Configuration Settings Most applications have some element of configuration. In our example application, we will define a configurable value that we will be read in the Page_Load event with the RoleManager.getConfigurationSetting() method. 1. Select the Chapter16.HelloAzure project and open ServiceDefinition.csdef. The contents should look something like this: <?xml version="1.0" encoding="utf-8"?> <ServiceDefinition name="Chapter16.HelloAzure" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition"> <WebRole name="Chapter16.WebRole"> <InputEndpoints> <InputEndpoint name="HttpIn" protocol="http" port="80" /> </InputEndpoints> <ConfigurationSettings> <Setting name="DiagnosticsConnectionString" /> </ConfigurationSettings> </WebRole> </ServiceDefinition> 2. Add the following inside the ConfigurationSettings element to tell Azure that we will be creating a setting called Message: <Setting name="Message"/> 3. Your ServiceDefinition.csdef file should now look like the following: <?xml version="1.0" encoding="utf-8"?> <ServiceDefinition name="Chapter16.HelloAzure" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition"> <WebRole name="Chapter16.WebRole"> <InputEndpoints> <InputEndpoint name="HttpIn" protocol="http" port="80" /> </InputEndpoints> CHAPTER 16  WINDOWS AZURE 417 <ConfigurationSettings> <Setting name="DiagnosticsConnectionString" /> <Setting name="Message"/> </ConfigurationSettings> </WebRole> </ServiceDefinition> 4. We will now define the actual value of this setting, so open ServiceDefinition.cscfg and add a new setting inside the ConfigurationSettings element: <Setting name="Message" value="Hello Azure"/> 5. While we are working with ServiceDefinition.cscfg, find the element that reads <Instances count="1"/> and change it to <Instances count="5"/> 6. Changing the instances count tells Azure to create five instances of our application and simulates scaling our application to use five Azure nodes (you will need to set this back before deployment depending on your pricing structure). This setting can be easily amended online; note how easy it is to quickly scale up your application depending on demand. Microsoft recently announced Azure supports an API that allows you to do this programmatically. Your ServiceDefinition.cscfg should now look like <?xml version="1.0"?> <ServiceConfiguration serviceName="Chapter16.HelloAzure" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration"> <Role name="Chapter16.WebRole"> <Instances count="5" /> <ConfigurationSettings> <Setting name="DiagnosticsConnectionString" value="UseDevelopmentStorage=true" /> <Setting name="Message" value="Hello Azure"/> </ConfigurationSettings> </Role> </ServiceConfiguration> Open Default.aspx.cs and enter the following code: using Microsoft.WindowsAzure.ServiceRuntime; protected void Page_Load(object sender, EventArgs e) { string GreetingString = "" + RoleEnvironment.GetConfigurationSettingValue("message"); Response.Write(GreetingString + " at " + DateTime.Now.ToString()); } 7. Press F5 to run the application and you should see the greeting value we defined output to the screen with the current time and date. CHAPTER 16  WINDOWS AZURE 418 Logging and Debugging When running your Azure applications locally, you can make full use of standard Visual Studio debugging facilities. However, when applications are deployed to the cloud, debugging and logging support is a bit limited at the time of writing. At the time of writing the logging APIs are in a state of flux (http://blogs.msdn.com/windowsazure/ archive/2009/10/03/upcoming-changes-to-windows-azure-logging.aspx) so expect the final version to have performance monitoring features and integration with Azure storage (see the following). Note that the RoleManager.WriteToLog() method that was present in preview versions has been removed. Testing Azure Applications We have now finished our application's development, so we need to test it. Development would be very slow if we had to deploy to the cloud each time to test it, so Microsoft provides a local version of Azure called the development fabric that simulates how our applications will function in the cloud. Before we can run Azure our application, we will need to create the development storage database (which is just a SQL Server database). This seems to be used for deployment and testing of Azure applications. It can also be quite useful for debugging Azure storage issues (discussed later in the chapter). Creating Development Storage To create development storage, open the Windows Azure SDK command prompt (on the Windows menu under the Windows Azure SDK v1.0 folder) and then enter the following command replacing INSTANCENAME with the name of your SQL Server instance (if you don’t want to use an instance just enter a dot to refer to the machine itself): DSInit /sqlinstance:INSTANCENAME After you press return, the DSInit utility will start creating the development storage database (Figure 16-4): Figure 16-4. Creation of development storage CHAPTER 16  WINDOWS AZURE 419 Now press F5 to run your application and you should see an exciting screen like Figure 16-5: Figure 16-5. Hello Azure application Well done—you have created your first Azure application—but don’t close the web browser window just yet. Take a look at the Windows taskbar (you may have to click Show hidden icons if you are using Windows 7) where there will be a small blue Windows Azure flag showing. Left-clicking on this will show you the current Azure storage and development fabric status (Figure 16-6). Figure 16-6. Azure storage Now right-click on the blue flag and notice how you can shut down the development storage and fabric here as well. This time, however, select the option to show the development fabric UI, and you should see a screen similar to Figure 16-7: CHAPTER 16  WINDOWS AZURE 420 Figure 16-7. Development Fabric UI The window is split into two panes. On the left-hand side is a tree structure that allows you to view details of the service and individual web roles, while over on the right is the logging output from the various Azure instances. Service Details Node Click the Service Details node to show you details of where your service is running. Chapter16.HelloAzure Node Right-click on the Chapter16.HelloAzure node and you will see options for starting, suspending, and restarting the services. You can further configure the project restart configuration by right-clicking and selecting Settings. Chapter16.WebRole Node Right-click the web role node and you will see options for clearing the logs and changing the logging level. Left-clicking the web role node will expand it to show all instances of the application running, which are represented by a number of green globes. The black screens on the left show the output from the individual nodes. . subscriptions now come with Azure time. CHAPTER 16  WINDOWS AZURE 41 4 Installation If you are working with Visual Studio 200 8, you will need to download and install the Azure SDK and Tools Microsoft .NET Services RANDOM TRADEMARK FACT In 200 7 computing manufacturer Dell tried to trademark the term “cloud computing.” http://tarr.uspto.gov/servlet/tarr?regser=serial&entry=7713 908 2. differ upon release. This chapter was written with Windows Azure Tools VS 201 0 Beta 2. CHAPTER 16  WINDOWS AZURE 41 2 Azure Overview Microsoft’s cloud computing offering can be divided into

Ngày đăng: 01/07/2014, 21:20

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan