饭饭TXT > 学习管理 > 《Visual Studio 2010 高级编程(英文出书版)》作者:Nick Randolph/等【完结】 > [Visual.Studio.2010.高级编程].Professional.Visual.Studio.2010.txt

第 80 页

作者:Nick Randolph/等 当前章节:15450 字 更新时间:2026-6-18 14:51

handled at least once even if a worker role dies mid-processing.

c#

private CloudQueue queue;

public override bool OnStart(){

DiagnosticMonitor.Start("DiagnosticsConnectionString");

Microsoft.WindowsAzure.CloudStorageAccount.

SetConfigurationSettingPublisher((configName, configSetter) =>{

configSetter(Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.

GetConfigurationSettingValue(configName));

});

Trace.TraceInformation("Worker entry point called");

// read account configuration settings

var storageAccount = CloudStorageAccount.

FromConfigurationSetting("DataConnectionString");

538 .

chaPter 25 WindoWS Azure

// create queue to communicate with web role

var queueStorage = storageAccount.CreateCloudQueueClient();

var queue = queueStorage.GetQueueReference("sample");

queue.CreateIfNotExist();

return base.OnStart();

}

public override void Run(){

Trace.TraceInformation("CloudWorker entry point called");

while (true){

try{

// Pop the next message off the queue

CloudQueueMessage msg = queue.GetMessage();

if (msg != null){

// Parse the message contents as a job detail

string jd = msg.AsString;

Trace.TraceInformation("Processed {0}", jd);

// Delete the message from the queue

queue.DeleteMessage(msg);

}

else{

Thread.Sleep(10000);

}

Trace.TraceInformation("Working");

}

catch (Exception ex){

Trace.TraceError(ex.Message);

}

}

}

Code snippet WorkerRole.cs

Vb

Private queue As CloudQueue

Public Overrides Function OnStart() As Boolean

DiagnosticMonitor.Start("DiagnosticsConnectionString")

CloudStorageAccount.SetConfigurationSettingPublisher(

Function(configName, configSetter)

configSetter(RoleEnvironment.

GetConfigurationSettingValue(configName)))

Trace.TraceInformation("Worker entry point called")

’ read account configuration settings

Dim storageAccount = CloudStorageAccount.

FromConfigurationSetting("DataConnectionString")

’ create queue to communicate with web role

Dim queueStorage = storageAccount.CreateCloudQueueClient()

queue = queueStorage.GetQueueReference("sample")

queue.CreateIfNotExist()

Return MyBase.OnStart()

End Function

The Windows azure Platform .

539

Public Overrides Sub Run()

Trace.TraceInformation("CloudWorker entry point called.")

Do While (True)

Try

’ Pop the next message off the queue

Dim msg As CloudQueueMessage = queue.GetMessage()

If (msg IsNot Nothing) Then

’ Parse the message contents as a job detail

Dim jd As String = msg.AsString

Trace.TraceInformation("Processed {0}", jd)

’ Delete the message from the queue

queue.DeleteMessage(msg)

Else

Thread.Sleep(10000)

End If

Trace.TraceInformation("Working")

Catch ex As StorageClientException

Trace.TraceError(ex.Message)

End Try

Loop

End Function

Code snippet WorkerRole.vb

You will notice that this code overrides two methods, OnStart and Run. The former is used to load

configuration values and set up local variables for working with Windows Azure storage, whereas

the Run method contains an infinite while loop that continues to process messages off the queue.

Before you can run your modified roles you need to specify the location of the queue storage

that you are going to be using. Though this will eventually be an Azure storage account, during

development you need to specify the details of the local Development Storage. You do this in the

ServiceConfiguration file:

<?xml version="1.0"?>

<ServiceConfiguration serviceName="FirstCloudApplication"

xmlsn="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration">

<Role name="CloudFront">

<Instances count="2" />

<ConfigurationSettings>

<Setting name="DataConnectionString" value="UseDevelopmentStorage=true" />

<Setting name="DiagnosticsConnectionString" value="UseDevelopmentStorage=true" />

<!-- <Setting name="DeploymentConnectionString" value="DefaultEndpointsProtocol=

https;AccountName=[YOUR_ACCOUNT_NAME];AccountKey=[YOUR_ACCOUNT_KEY]" /> -->

</ConfigurationSettings>

</Role>

<Role name="CloudWorker">

<Instances count="2" />

<ConfigurationSettings>

<Setting name="DataConnectionString" value="UseDevelopmentStorage=true" />

<Setting name="DiagnosticsConnectionString" value="UseDevelopmentStorage=true" />

</ConfigurationSettings>

</Role>

</ServiceConfiguration>

540 .

chaPter 25 WindoWS Azure

You will notice that for both the CloudWorker and CloudFront roles, settings for

DataConnectionString and DiagnosticsConnectionString have been defined. In this case, the

value has been set to use the development storage account. When you go to deploy to Windows

Azure, you will need to replace this with a connection string that includes the account name and key,

in the format illustrated by the DeploymentConnectionString. Before these values will be accessible

to your roles you also need to update the ServiceDefinition file to indicate which settings are

defined for each role:

<?xml version="1.0" encoding="utf-8"?>

<ServiceDefinition name="FirstCloudApplication"

xmlsn="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">

<WebRole name="CloudFront" enableNativeCodeExecution="false">

<InputEndpoints>

<!-- Must use port 80 for http and port 443 for https when running in the cloud -->

<InputEndpoint name="HttpIn" protocol="http" port="80" />

</InputEndpoints>

<ConfigurationSettings>

<Setting name="DataConnectionString" />

<Setting name="DiagnosticsConnectionString" />

</ConfigurationSettings>

</WebRole>

<WorkerRole name="CloudWorker" enableNativeCodeExecution="false">

<ConfigurationSettings>

<Setting name="DataConnectionString" />

<Setting name="DiagnosticsConnectionString" />

</ConfigurationSettings>

</WorkerRole>

</ServiceDefinition>

With these changes, try running your Azure application and noting that when you hit the Submit

button you will see a “Processed” message appear in one of the running instances of the worker role

in the Development Fabric UI.

application deployment

Once you have built your Azure application using the Development Fabric and Development

Storage, you will want to deploy it to the Windows Azure Platform. Before doing so you will need to

provision your Windows Azure account with both a hosting and a storage service. Start by going

to http://www.azure.com and signing in using your Live Id to your Windows Azure account. After

logging in, click on the “Go to the Windows Azure Developer portal” link. This opens the Windows

Azure portal, which looks similar to Figure 25-5.

The Windows azure Platform .

541

fiGure 25-5

Click the project name, followed by the New Service button, and then select the type of service you

want to add. The FirstCloudApplication requires both hosting and storage so you will need to add

one of each. Once you have added a Storage Account service you should see a configuration screen

similar to Figure 25-6.

fiGure 25-6

You will need to copy the account information and storage endpoints across into your

ServiceConfiguration file. Once you have done this you can again run your Azure application.

This time it will still run within your local Development Fabric but it will use the Azure storage

instead of the Development Storage. This is a good test to ensure your application will deploy

correctly to the cloud.

Once you have set up your hosting service account you will see a screen similar to Figure 25-7.

542 .

chaPter 25 WindoWS Azure

fiGure 25-7

In Figure 25-7 you can see that you in fact have two environments into which you can deploy:

Production and Staging. As with all good deployment strategies, Azure supports deploying into

Staging and then once you are comfortable, migrating that into Production.

Return to Visual Studio 2010, right-click the FirstCloudApplication project, and select Publish.

This builds your application and generates a deployment package and a configuration file. These are

displayed in a Windows Explorer dialog once completed. Return to Windows Azure and click the

Deploy button under the Staging node (in Figure 25-7). You are prompted to select the deployment

package and configuration file. Once you complete the upload you are returned to the hosting

service page where you will see that the Staging environment has been updated, as in Figure 25-8.

fiGure 25-8

The Windows azure Platform .

543

Unlike other ASP.NET web applications that start running as soon as they are completely deployed,

Azure applications need to be started. You do this by clicking the Run button. The screen will

refresh with all roles stating that they are initializing. Eventually, they will update to Started, at

which point all roles are ready to receive input or do work.

The last stage in this process is to promote what’s running in the Staging environment into

Production. The word “promote” is important because this transition is all handled by an intelligent

router. Because the cut over from one to the other will at some point (depending on how quickly

the router effects the change) be close to instantaneous, there should never be any time at which

someone hitting the site receives a 404 or missing page. To promote Staging into Production, select

the round rotating button situated in between the product and staging areas of the Azure portal.

tuning your application

Over time, demand for your application may vary, or you may need to adjust application settings

specified in the ServiceConfiguration file. You can do this dynamically by clicking the Configure

button (see the Staging deployment in Figure 25-8). Figure 25-9 shows the configuration screen where

you can modify the configuration XML, or upload an alternative configuration file. You should only

modify your staging deployment using this method because you don’t want to affect the running of

your Production deployment. The recommend approach is to start with identical Production and

Staging deployments, modify the Staging configuration, allow it to initialize and start, then switch that

deployment into Production. You can then modify the second deployment so that they are in sync.

fiGure 25-9

544 .

chaPter 25 WindoWS Azure

This screen also allows you to export logs generated by the roles within your application to

a storage account. After copying the logs you will then need to retrieve the logs from the relevant

storage account. You can do this using the CloudDrive sample in the Azure SDK, which can be used

to map a storage account as a local drive that you can query in Powershell.

sql azure

In addition to Azure table, blob, and queue storage, the Windows Azure Platform offers true

relational data hosting in the form of SQL Azure. You can think of each SQL Azure database as

being a hosted instance of a SQL Server 2008 database that is running in high-availability mode.

This means that at any point in time there are three synchronized instances of your database. If one

of these instances fails, a new instance is immediately brought online and the data is synchronized

to ensure the availability of your data.

To create a SQL Azure database, sign into the Windows Azure portal and navigate to the SQL

Azure tab. Once there you can manage your SQL Azure accounts, where you can create and delete

databases. After creating a database you can retrieve the connection string that you need in order

to connect to the database by selecting the database and clicking the Connection String button, as

shown in Figure 25-10.

fiGure 25-10

appfabric .

545

At the time of writing you have a number of ways to interact with a SQL Azure database. Although

SQL Azure is based on SQL Server 2008, a number of limitations exist that prevent most graphical

tools, such as SQL Server Management Studio (Object Browser) and Visual Studio 2010, from

working properly. You can, however, connect a SQL Server Management Studio Query Window

to a SQL Azure database and execute T-SQL statements against your database. Some third-party

tools and Visual Studio 2010 add-ins, such as the SQL Azure Migration Wizard, the SQL Azure

Manager, and the SQL Azure Explorer, are available that can assist with working with SQL Azure.

From your application you can connect to SQL Azure using the connection string retrieved from the

Windows Azure portal page. You can use most frameworks that are based on top of ADO.NET such

as LINQ to SQL, Entity Framework, or simply plain ADO.NET to create, update, read, or delete data

in your SQL Azure database.

aPPfabric

The third component of the Windows Azure Platform is the AppFabric. This in turn is made up of the

Service Bus and the Access Control Service. In an environment where organizations are increasingly

looking to host some or all of their applications in the cloud, significant challenges are posed around

connectivity and security. The AppFabric provides a solution to allow enterprises to connect applications

and unify application security.

service bus

Though most organizations have connectivity to the Internet, connectivity between offices or with

individuals on the road is often the cause of frustration. Increasingly, companies operate behind

one or more firewall devices that not only restrict the flow of traffic but also do network address

目录
设置
设置
阅读主题
字体风格
雅黑 宋体 楷书 卡通
字体大小
适中 偏大 超大
保存设置
恢复默认
手机
手机阅读
扫码获取链接,使用浏览器打开
书架同步,随时随地,手机阅读
首 页 < 上一章 章节列表 下一章 > 尾 页