and stepping through the execution in the same way you would any other Visual Studio project.
workinG with features
Features are primarily targeted at SharePoint
Administrators and provide them with a way to
manage related items. Every time you create an
item in a SharePoint project it is added to a Feature.
Features are stored under the Features node in
your SharePoint project. Visual Studio includes a
Feature Designer (shown in Figure 24-19), which
is displayed when you double-click a Feature.
The Feature Designer allows you to set a title and
description for the Feature that will be displayed
in SharePoint. You can also set the scope of the
Feature to an entire server farm, all web sites in a
site collection, a specific web site, or all web sites
in a web application.
You can choose to include or exclude certain
items in a Feature with the Feature Designer. For
example, in Figure 24-19, all SharePoint items in
the project except for MyWorkflow have been included in the Feature. If you have more than one
Feature in a project, you can also set dependencies that ensure one Feature cannot be activated
unless another Feature has been.
fiGure 24-18
fiGure 24-19
530 .
chaPter 24 ShArepoinT
In SharePoint, Administrators can activate or deactivate Features using the Manage Site Features
or Site Collection Features administration screens under Site Actions . Site Settings (see
Figure 24-20).
fiGure 24-20
PackaGinG and dePloyMent
SharePoint provides a custom packaging format called Windows SharePoint Package (WSP). WSP
files can contain Features, site definitions, templates and application pages, and additional required
assemblies. WSP files are created in the bin/debug or bin/release folder when you build a
SharePoint solution with Visual Studio. The WSP file can then be installed on a remote SharePoint
server by an administrator.
When you create a SharePoint project, a package definition file is also created in the project under
the Packages node. The package definition file describes what should go into the WSP file. Visual
Studio includes a Package Designer and Packaging Explorer tool window to assist with building
packages. If you double-click the package file it opens the file with these design tools. Figure 24-21
shows a package file that includes an application page and a single Feature.
When you press F5 in a SharePoint project, Visual Studio is saving you a whole lot of time by
automatically deploying all of the items in your project to the local SharePoint installation. The
deployment steps are specified under a SharePoint-specific project property page, shown in
Figure 24-22. To display this property page, right-click the project in the Solution Explorer and
select Properties.
Packaging and Deployment .
531
fiGure 24-21
fiGure 24-22
532 .
chaPter 24 ShArepoinT
You can specify a command-line program or script to run before and after Visual Studio deploys
the solution to the local SharePoint installation. The actual deployment steps are specified as a
deployment configuration. Double-click the configuration in the Edit Configurations list to display
the list of deployment steps. Figure 24-23 shows the default deployment configuration.
fiGure 24-23
Finally, you can right-click a project in the Solution Explorer and select Retract to remove the
SharePoint components from the local SharePoint installation.
suMMary
In this chapter you learned how to build solutions for Microsoft SharePoint 2010. The development
tools in Visual Studio 2010 allow you to easily develop Web Parts, workflows, custom lists, and
complete web applications that run under SharePoint’s rich hosting environment.
This chapter has just scratched the surface of what is possible with SharePoint 2010 development.
If you are interested in diving deeper into this topic, visit the SharePoint Developer Center
at http://msdn.microsoft.com/sharepoint, the SharePoint Dev Wiki at http://www
.sharepointdevwiki.com, or pick up a copy of Professional SharePoint 2010 Development
by Tom Rizzo, Reza Alirezaei, Jeff Fried, and Paul Swider.
25
Windows azure
what’s in this chaPter?
.
Understanding Windows Azure
.
Building, testing, and deploying applications using Windows Azure
.
Storing data in Windows Azure tables, blobs, and queues
.
Using SQL Azure from your application
.
Understanding the AppFabric
Over the past couple of years, the adoption of cloud computing has really taken off with
Google, Amazon, and a host of other providers entering the market. Microsoft’s approach
mirrors their own approach to desktop, mobile, and server computing, in so far as they are
offering a development platform on top of which both ISVs and Microsoft itself can build
great software. Without going into a formal definition of Cloud Computing, it is important to
recognize that you might choose to run your application in the cloud for a number of reasons.
These include the need for high availability, the ability to scale to meet the demand for your
application, and of course, cost reduction.
This chapter is broken into three sections that cover the Windows Azure Platform, SQL
Azure, and the AppFabric. The Windows Azure Platform hosts your web application, allowing
you to dynamically vary the number of concurrent instances running. It also provides storage
services in the form of tables, blobs, and queues. SQL Azure provides a true database service
hosted in the cloud. Finally, you can use the AppFabric to authenticate users, control access to
your application and services, and simplify the process of exposing services from within your
organization.
534 .
chaPter 25 WindoWS Azure
the windows azure PlatforM
As with most Microsoft technologies, getting started with the Windows Azure platform is as easy as
creating a new application, building it, and then running it. You will notice that there is a new node
in the New Project dialog entitled Cloud Service, which has a single project template, also called
Cloud Service, as shown in Figure 25-1.
fiGure 25-1
After selecting the Cloud Service project template you are prompted to add one or more Roles to
your application. An Azure project can be broken into different Roles based on the type of work
they are going to do and whether they accept user input. Simply put, Web Roles can accept user
input via an inbound connection (for example, http on port 80), whereas Worker Roles cannot. A
typical scenario would consist of a Web Role that is used to accept data. This may be a web site
or a web service of some description. The Web Role would hand off the data, for example, via a
queue, to a Worker Role, which would then
carry out any processing that is to be done.
This separation means that the two tiers can
be scaled out independently, improving the
elasticity of the application.
In Figure 25-2, both an ASP.NET Web
Role and a Worker Role have been added to
the cloud services solution by selecting the
role and clicking the right arrow button.
Clicking the edit symbol on the selected
role allows you to rename the role before
clicking OK to complete the creation of
your application. fiGure 25-2
The Windows azure Platform .
535
As you can see in Figure 25-3, the application created consists of
a project for each role selected (CloudFront and CloudWorker,
respectively) and an additional project, FirstCloudApplication, that
defines the list of roles and other information about your Azure
application.
The CloudFront project is essentially just an ASP.NET web application
project. In fact, if you right-click this project and select Set as Startup
Project you can run this project as with any normal ASP.NET project.
On the other hand, the CloudWorker project is simply a class library
with a single class, WorkerRole, which contains the entry point for the
worker.
To run your Azure application, make sure the FirstCloudApplication
project is set as the Startup Project and then press F5 to start
debugging. If this is your first time running an Azure application you
will notice a dialog appears that initializes the Development Storage.
This process takes a minute or two to complete; once done you will
notice that two icons have been added to the Windows taskbar.
The first allows you to control the Development Storage services, a set of three local services that
mirror the table, blob, and queue storage available in the Azure platform. The second allows you to
monitor the Development Fabric, a local replica of the Azure hosting environment in which you can
run, debug, and test your application.
After the Development Storage has been initialized you should notice that the Default.aspx
page of the
CloudFront project launches within the browser. Although you will only see a single browser instance,
in fact, multiple instances of the web role are all running in what’s called the Development Fabric.
the development fabric
In the FirstCloudApplication project are two files that define attributes about your Azure
application. The first, ServiceDefinition.csdef, defines the structure and attributes of the roles
that make up your application. For example, if one of your roles needs to write to the file system you
can stipulate a LocalStorage property, giving the role restricted access to a small amount of disk
space in which to read and write temporary files. This file also defines any settings that the roles will
require at run time. Defining settings is a great way to make your roles more adaptable at run time
without having to rebuild and publish them.
The second file is the ServiceConfiguration.cscfg file, which defines the run time configuration
of the roles. This includes the number of instances of each role that should be running, as well as
any settings that you have defined in the ServiceDefinition file. If you modify values in this
configuration file, for example, changing the count attribute of the Instances element to 4 for
both roles, and re-run your application, it will run with the new configuration values in the local
Development Fabric.
If you right-click the Development Fabric icon in the Windows taskbar and select Show
Development Fabric UI, you will see a hierarchical representation of the running applications within
the Development Fabric, as shown in Figure 25-4. As you drill down into the deployments you can
see the FirstCloudApplication and then the two roles, CloudFront and CloudWorker.
fiGure 25-3
536 .
chaPter 25 WindoWS Azure
fiGure 25-4
Within each of the roles you can see the number of running (green dot) instances, which in Figure 25-4
is 4. In the right pane you can see the log output for each of the running instances. Clicking the title
bar on any of the instances toggles that instance to display in the full pane. The icon in the top-right
corner of each instance indicates the logging level. You can adjust this by right-clicking the title and
selecting the desired value from the Logging Level menu item.
table, blob, and queue storage
So far you have a web role with no content and a worker role that doesn’t do anything. You can
add content to the web role by simply adding controls to the Default.aspx page in the same way
that you would for a normal web application. Start by adding a textbox called JobDetailsText and a
button called SubmitJob. Double-click the button to bring up the code-behind file.
You can pass data between web and worker roles by writing to table (structured data), blob (single
binary objects), or queue (messages) storage. You work with this storage within the Azure platform
via its REST interface. However, as .NET developers this is not a pleasant or efficient coding
experience. Luckily, the Azure team has put together a wrapper for this functionality that makes it
easy for your application to use Windows Azure storage. If you look at the references for both the
Web and Worker Role projects, you will see a reference for Microsoft.WindowsAzure.Storage.dll,
which contains the wrapper classes and methods that you can use from your application.
In the code-behind file for the Default.aspx page, enter the following code, which obtains a queue
reference and then adds a simple message. Note that you may need to add using statements to your
code file where necessary.
c#
protected void SubmitJob_Click(object sender, EventArgs e){
// read account configuration settings
var storageAccount = CloudStorageAccount.
FromConfigurationSetting("DataConnectionString");
The Windows azure Platform .
537
// create queue to communicate with worker role
var queueStorage = storageAccount.CreateCloudQueueClient();
var queue = queueStorage.GetQueueReference("sample");
queue.CreateIfNotExist();
queue.AddMessage(new CloudQueueMessage(this.JobDetailsText.Text));
}
Code snippet Default.aspx.cs
Vb
Protected Sub SubmitJob_Click(ByVal sender As Object,
ByVal e As EventArgs) Handles SubmitJob.Click
’ read account configuration settings
Dim storageAccount = CloudStorageAccount.
FromConfigurationSetting("DataConnectionString")
’ create queue to communicate with worker role
Dim queueStorage = storageAccount.CreateCloudQueueClient()
Dim queue = queueStorage.GetQueueReference("sample")
queue.CreateIfNotExist()
queue.AddMessage(New CloudQueueMessage(Me.JobDetailsText.Text))
End Sub
Code snippet Default.aspx.vb
This code takes the value supplied in the JobDetailsText textbox and adds it to the queue, wrapped
in as a message.
You also need to update the worker role to pop messages off the queue and carry out the appropriate
actions. The following code retrieves the next message on the queue, and simply writes the response
out to the log, before deleting the message off the queue. If you don’t delete the message from the
queue it is pushed back onto the queue after a configurable timeout, so as to ensure all messages are