Worker Role的代码完成后,编写Web Role的代码,这里由于篇幅,就不一一介绍了,感兴趣的读者可以从http://code.msdn.microsoft.com/windowsazuresamples
下载完整的实例代码。
第三步,设置服务定义文件和服务配置文件
服务定义文件设置如下:
<?xml version="1.0" encoding="utf-8"?>
<ServiceDefinition name="Thumbnails" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">
<WorkerRole name="Thumbnails_WorkerRole">
<ConfigurationSettings>
<Setting name="DataConnectionString" />
<Setting name="DiagnosticsConnectionString" />
</ConfigurationSettings>
</WorkerRole>
<WebRole name="Thumbnails_WebRole">
<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>
</ServiceDefinition>
<ServiceDefinition name="Thumbnails" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">
<WorkerRole name="Thumbnails_WorkerRole">
<ConfigurationSettings>
<Setting name="DataConnectionString" />
<Setting name="DiagnosticsConnectionString" />
</ConfigurationSettings>
</WorkerRole>
<WebRole name="Thumbnails_WebRole">
<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>
</ServiceDefinition>
服务配置文件设置如下:
<?xml version="1.0"?>
<ServiceConfiguration serviceName="Thumbnails" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration">
<Role name="Thumbnails_WorkerRole">
<Instances count="1" />
<ConfigurationSettings>
<!-- Add your storage account information and uncomment this to target Windows Azure storage.
<Setting name="DataConnectionString" value="DefaultEndpointsProtocol=https;AccountName=YourAccountName;AccountKey=YourAccountKey" />
<Setting name="DiagnosticsConnectionString" value="DefaultEndpointsProtocol=https;AccountName=DiagnosticsAccountName;AccountKey=DiagnosticsAccountKey" />
-->
<Setting name="DataConnectionString" value="UseDevelopmentStorage=true" />
<Setting name="DiagnosticsConnectionString" value="UseDevelopmentStorage=true" />
</ConfigurationSettings>
</Role>
<Role name="Thumbnails_WebRole">
<Instances count="1" />
<ConfigurationSettings>
<!-- Add your storage account information and uncomment this to target Windows Azure storage.
<Setting name="DataConnectionString" value="DefaultEndpointsProtocol=https;AccountName=YourAccountName;AccountKey=YourAccountKey" />
<Setting name="DiagnosticsConnectionString" value="DefaultEndpointsProtocol=https;AccountName=DiagnosticsAccountName;AccountKey=DiagnosticsAccountKey" />
-->
<Setting name="DataConnectionString" value="UseDevelopmentStorage=true" />
<Setting name="DiagnosticsConnectionString" value="UseDevelopmentStorage=true" />
</ConfigurationSettings>
</Role>
</ServiceConfiguration>
<ServiceConfiguration serviceName="Thumbnails" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration">
<Role name="Thumbnails_WorkerRole">
<Instances count="1" />
<ConfigurationSettings>
<!-- Add your storage account information and uncomment this to target Windows Azure storage.
<Setting name="DataConnectionString" value="DefaultEndpointsProtocol=https;AccountName=YourAccountName;AccountKey=YourAccountKey" />
<Setting name="DiagnosticsConnectionString" value="DefaultEndpointsProtocol=https;AccountName=DiagnosticsAccountName;AccountKey=DiagnosticsAccountKey" />
-->
<Setting name="DataConnectionString" value="UseDevelopmentStorage=true" />
<Setting name="DiagnosticsConnectionString" value="UseDevelopmentStorage=true" />
</ConfigurationSettings>
</Role>
<Role name="Thumbnails_WebRole">
<Instances count="1" />
<ConfigurationSettings>
<!-- Add your storage account information and uncomment this to target Windows Azure storage.
<Setting name="DataConnectionString" value="DefaultEndpointsProtocol=https;AccountName=YourAccountName;AccountKey=YourAccountKey" />
<Setting name="DiagnosticsConnectionString" value="DefaultEndpointsProtocol=https;AccountName=DiagnosticsAccountName;AccountKey=DiagnosticsAccountKey" />
-->
<Setting name="DataConnectionString" value="UseDevelopmentStorage=true" />
<Setting name="DiagnosticsConnectionString" value="UseDevelopmentStorage=true" />
</ConfigurationSettings>
</Role>
</ServiceConfiguration>
第四步:设置并调试运行项目
设置项目主要是检查windows azure SDK中的Development环境是否已经启动,设置Thumbnails为启动项目,设置Web Role(Thumbnails_WebRole)的Default页面为启动页面。设置完成后,编译项目,编译没有错误后,点击调试|不调试直接运行,打开上传图片的页面,我们上传2张图片后,界面如图9所示:
图9 Thumbnails界面
总结
本文介绍了Windows Azure Storage中的消息队列--Windows Azure Queue,它的主要的应用场景在于Web Role和Worker Role之间进行消息传递。同时从应用程序和数据结构两个方面介绍了Queue的结构,并通过应用场景介绍,生成缩略图的一个应用案例的讲解,使大家对Queue有一个更深入的了解。