We will start with a simple Twitter Reader application that reads latest tweets from selected Twitter handle. We'll use Windows Azure Caching to cache last retrieved tweets to improve application performance.
-
Demonstrate basic usage of Windows Azure Storage
We'll add a feature of tracking current hot topics. We'll read mentioned Twitter handles and save statistic data (# of mentions per day) to Windows Azure table storage. Then we'll extend the UI to display the data.
-
Demonstrate how to extend a Cloud Service to use n-Tiered architecture
We'll move hot topic analysis to a backend worker role.
-
Demonstrate how to deploy and scale Cloud Services
We'll show a deployed version of the service and show how to use NewRelic to monitor application performance.
Note: This is a fairly large demo. You can use different versions of the source code to jump forward. TwitterReader-Caching contains caching implementation. TwitterReader-Storage contains both caching and storage implementations. And finally TwitterReader-End contains the completed n-Tier service.
> **Note:** You can optionally enable local cache by uncommenting **localCache** element.
-
Open TwitterFeedController.cs under Controllers folder.
-
Modify getTweets() method
Tweets getTweets(string name) { DataCache cache = new DataCache(); Tweets entries = cache.Get(name) as Tweets; if (entries == null) { entries = TwitterFeed.GetTweets(name); cache.Add(name, entries); } return entries; }
1. Resolve namespace for DataCache.
1. **F5** to launch again.
1. The initial load is just as slow. Refreshing browser yeilds much faster as cache kicks in.
<a name="windows-azure-storage" />
## Windows Azure Storage ##
1. Double-click on **TwitterReader.Cloud\Roles\TwitterReader.Web**.
1. Go to **Settings** tab.
1. Click **Add Setting** link.
1. Enter **StorageConnectionString** as Name; change Type to **Connection String**; Use [**...**] button to enter a connection string.
> **Note:** You can use development storage account.
1. Right-click on **TwitterReader.Web** project and select **Manage NuGet Packages...** menu.
1. Search and insall **WindowsAzure.Storage** package.
1. Add **TwitterFeedController.cs** from **code\Assets** folder to replace the one under **Controllers** folder.
> **Note:** The updated controller contains code for both saving data to table storage and retriving data back. You can set up code snippets if you want to, but for the sake of time proabaly replacing the whole file is easier.
1. Walk through the code. Point out that the new **Index()** method calls out to **updateHotTopics()** when new tweets are read.
1. Navigate to **updateHotTopics()** method. The code runs a regular expression against each tweet, retrieve all mentioned Twitter handles, and save number of mentions (by date) to Windows Azure table storage.
> **Note:** If you have more time, go through more details such as how to get a reference to a table, and how to insert records, etc. Also walk through the definition of TopicEntity, which is defined in **TwitterReader.Lib** roject.
> **Note:** The code here doesn't handle transient errors for simplicity. You should point this out to audience.
1. **[OPTIONAL]** Launch the application after above changes. You can use Server Explorer to browse generated records.
> **Note:** This step is also a sanity check to make sure storage is working before you move to the next step.
1. Add **HotTopics.cshtml** from **code\Assets** folder to **Views\TwitterFeed** folder.
> **Note:** This is the partial view to display hot topics.
1. Open **Views\TwitterFeed\Index.cshtml** and add the following code right above the **script** element:
````HTML
<div>
@{Html.RenderAction("HotTopics");}
</div>
![partial](DEMO-UsingCloudApplicationServices/raw/master/images/partial.png?raw=true)
- F5 to launch the application. Try several different Twitter handles and see screen refreshes.
> **Note:** We are using 2.0 library, which has a different namespace as captured by the Service Bus template.
-
Build to make sure everything is fine.
-
Add a reference to TwitterReader.Lib project.
-
Open App.config file and replace [cache cluster role name] with TwitterReader.Web.
-
Double-click on TwitterReader.Cloud\Roles\TwitterReader.Worker, go to Settings tab, and paste in your Service Bus connection string as the value of Microsoft.ServiceBus.ConnectionString.
-
Click Add Setting link.
-
Enter StorageConnectionString as Name; change Type to Connection String; Use [...] button to enter a connection string.
Note: You can use development storage account.
-
Copy update hot topics region from TwitterReader.Web\Controllers\TwitterFeedController.cs to TwitterReader.Worker\WorkerRole.cs.
Note: This is the part where processing logics are migrated to backend.
-
Edit updateHotTopics() method. Replace
Tweets tweets = getTweets(name);
with
````C#
DataCache cache = new DataCache();
var tweets = cache.Get(name) as Tweets;
-
Now resolve all namespaces and compile.
-
Back in Run() method. Add
updateHotTopics(receivedMessage.GetBody());
before
````C#
receivedMessage.Complete();
> **Note:** Now the worker is ready to process the message. Let's go back to web role to send the message.
-
Back in TwitterReader.Web, add a reference to Windows Azure Service Bus NuGet package.
-
Modify Web.config file and paste in your Service Bus connection as value of Microsoft.ServiceBus.ConnectionString.
-
Open Controllers\TwitterFeedController.cs.
-
Add queue client initialization code
QueueClient mClient; public TwitterFeedController() { mClient = QueueClient.CreateFromConnectionString( CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString"), "ProcessingQueue"); }
1. In **Index()** method, instead of calling **updateHotTopics()** method, call
````C#
mClient.Send(new BrokeredMessage(name));
- F5 to launch the app.
Note: You can set break points in worker role such as in updateHotTopics() method to show the messages coming through.
> **Note:** Mention SSO to the NewRelic portal.
> **Note:** See Appendix for details on setting up NewRelic.
- In application overview, talk about how we can monitor application server.
- Switch to Browser view, talk about how we can monitor client perceived performance as well.
- Click on Map to switch to map view. Show how we can get insights of system topology.
- Click on Transactions, talk about how we can monitor key transactions in the system.
- Click on Read Twitter Feed transaction.
- Select one of the transaction
- talk about how we can drill down to call stacks.
````C#
@Html.Raw(NewRelic.Api.Agent.NewRelic.GetBrowserTimingHeader()) ... @Html.Raw(NewRelic.Api.Agent.NewRelic.GetBrowserTimingFooter())
1. Deploy the application.
1. Once the application is deployed, navigate to the application and perform some operations.
1. In New Relice portal, open Application overview page.
1. In **Web transactions** section, click on **TwitterFeed.Index**.
![webtransactions](DEMO-UsingCloudApplicationServices/raw/master/images/webtransactions.png?raw=true)
1. Click **Track as Key Transactions**.
![trackaskey](DEMO-UsingCloudApplicationServices/raw/master/images/trackaskey.png?raw=true)
1. In the wizard window. Enter **Read Twitter Feed** as transaction name, accept all defaults and click on **Track Key Transaction** to complete the wizard.
![feed](DEMO-UsingCloudApplicationServices/raw/master/images/feed.png?raw=true)