Download.microsoft.com



Hands-On Lab

Windows Server AppFabric Cache:

Lab 6 – HA Locking and Callbacks

Lab version: 1.0.0

Last updated: 7/27/2010

[pic]

Contents

Overview 3

Starting Materials 3

Exercise 1: Using Callbacks 4

Task 1 – Creating the cache for this lab 4

Task 2 – Using Callbacks 5

Summary 14

Overview

Windows Server AppFabric offers cache notifications that allow your applications to receive asynchronous notifications when various cache operations occur on the cache cluster. Cache notifications also provide automatic invalidation of locally cached objects.

.

Setup

You must perform the following steps to prepare your computer for this lab:

1. Complete the Development Environment Setup lab.

2. To simplify the process of opening the Windows Server AppFabric Caching Labs, we have provided a utility called LabStarter that you should run as the first step in any lab.

3. To use it, run LabStarter.exe from the %InstallFolder%\Assets directory, click the Caching tab, and click the button corresponding to the Lab exercise you wish to open.  This will open the desired solution in Visual Studio for you automatically.

Exercises

This Hands-On Lab comprises the following exercises:

Exercise 1: Using Callbacks

.

Estimated time to complete this lab: 30 minutes.

Starting Materials

This Hands-On Lab includes the following starting materials:

• Visual Studio solutions. The lab provides the a Visual Studio solution that you can use as starting point for the exercises in the begin folder.

. Note: Inside each exercise folder, you will find an end folder containing a solution with the completed lab exercise.

Exercise 1: Using Callbacks

Task 1 – Creating the cache for this lab

. Note: If you have already started your Cache and have PowerShell open from the previous lab, you can skip this task.

4. To verify the installation and start the cache host, open PowerShell from Start | Windows Server AppFabric | Caching Administration Windows PowerShell

1. Execute the Use-CacheCluster cmdlet to bring the current machine’s cluster configuration into the context of your PowerShell session.

. PowerShell

. Use-CacheCluster

2. Execute the Get-CacheHost cmdlet to see the state of your cache cluster.

. PowerShell

. Get-CacheHost

3. You should see something that looks similar to Figure 1. Note that the Service Status is DOWN.

. [pic]

. Figure 1

. Get-CacheHost cmdlet execution results

. If the Service Status is UP, skip to Task 2. If the Service Status is DOWN start the cache host by using the Start-CacheHost cmdlet. Start-CacheHost requires two parameters: the host name and the port number. Set the value of the Get-CacheHost cmdlet to a variable named $myhost, and then use the $myhost.HostName and $myhost.PortNo properties as the input to Start-CacheHost.

. PowerShell

. $myhost = Get-CacheHost

. Start-CacheHost $myhost.HostName $myhost.PortNo

4. Your PowerShell command screen should look similar to Figure 2. Your Service Status should now be UP.

. [pic]

.

. Figure 2

. Starting the cache host

.

Task 2 – Using Callbacks

In this exercise, you will use the AppFabric Cache callback functionality to be notified when items in the cache are modified.

5. Create a new cache for this lab named Lab6Cache by typing the following PowerShell command and pressing Enter. Note – you need to create this cache with notifications enabled!

. PowerShell

. New-Cache Lab6Cache –NotificationsEnabled true

6. Open Visual Studio 2010 by going to Start | All Programs | Microsoft Visual Studio 2010 | Microsoft Visual Studio 2010.

7. Choose File | New | Project.

8. In the New Project dialog, pick Visual C# | Windows | Console Application. Specify CacheCallbacks as the name of the project and %AppFabricTrainingKit%\Labs\cLab06\Ex2\Begin as the Location. See Figure 3.

. [pic]

. Figure 3

. New Project dialog

9. Click OK to create the project.

10. Right-click on the CacheCallbacks project in the Solution Explorer and select Properties.

11. In the Properties window under Application, change the Target Framework to .NET Framework 4. See Figure 4

. [pic]

. Figure 4

. Changing the Framework version

12. Click Yes on the confirmation dialog.

. Note: The reason you need to do this is that the distributed cache assemblies depend on assemblies that aren’t part of the Client Profile.

13. Right-click on the CacheCallbacks project in the Solution Explorer and select Add Reference

14. You need to add a reference to Microsoft.ApplicationServer.Caching.Client.dll and Microsoft.ApplicationServer.Caching.Core.dll.

15. In the Add Reference dialog box, click on the Browse tab. Navigate up 4 levels to the \Source directory, then down into .\Assets\AppFabricCacheDlls.Click on the Browse tab. In the File name textbox type C:\Windows\System32\AppFabric and press Enter. Note - if you are using a 64-bit system – you must type %windir%\SysNative\AppFabric and press Enter get to the correct directory.

16. Select Microsoft.ApplicationServer.Caching.Client.dll and Microsoft.ApplicationServer.Caching.Core.dll, then click OK. See Figure 5

. [pic]

. Figure 5

. Add Reference dialog

17. Open the Program.cs by double-clicking on the file in the Solution Explorer.

18. Add a using statement for Microsoft.ApplicationServer.Caching under all the other using statements.

. C#

. using Microsoft.ApplicationServer.Caching;

19. Inside of the method named Main, add code that uses the DataCacheFactory to connect to the local machine’s cache host using the Environment.MachineName variable and port 22233. Get the Cache named Lab6Cache.

. C#

. var config = new DataCacheFactoryConfiguration();

. config.Servers = new List {

. new DataCacheServerEndpoint(Environment.MachineName, 22233)

. };

.

. var factory = new DataCacheFactory(config);

. var cache = factory.GetCache("Lab6Cache");

.

20. Hit CTRL+F5 to build and run the application at this point to ensure your code is correct and you can connect to the cache.

21. Now add code that adds a callback delegate for the cache level callback for Lab6Cache:

. C#

. cache.AddCacheLevelCallback(

. DataCacheOperations.AddItem|DataCacheOperations.ReplaceItem,

. ( cacheName, regionName, key, version, cacheOperation, nd)=>

. {

. if (cacheOperation == DataCacheOperations.AddItem)

. {

. Console.ForegroundColor = ConsoleColor.Green;

. Console.WriteLine(

. "New item added to cache {0} \t region {1} \t key {2}",cacheName, regionName,key);

. Console.ResetColor();

. }

. if (cacheOperation == DataCacheOperations.ReplaceItem)

. {

. Console.ForegroundColor = ConsoleColor.Red;

. Console.WriteLine("Item modified cache {0} \t region {1} \t key {2}", cacheName, regionName, key);

.

. Console.ResetColor();

. }

. });

. Console.WriteLine("Waiting for callbacks...");

. Console.ReadLine();

22. Hit CTRL+F5 to build and run the application.

23. Use Windows Explorer to go to %AppFabricTrainingKit%\Labs\cLab06\Source\Assets\AppFabricCacheApp\ and start AppFabricCacheApp.exe.

24. When the Open File dialog appears, navigate to .\AppFabricCache\Resources\AppFabricCacheApp, pick AppFabricLabs.Interfaces.dll, and press Open.

. [pic]

. Figure 6

. Pre loading assemblies for the cache tool

. Note: To allow viewing and editing of the objects in the cache, this tool needs to be able to load the assembly that contains the cached object type.

25. Navigate in the tree in the left of the application to find Lab6Cache. See Figure 7

. [pic]

. Figure 7

. Picking Lab6Cache

26. While Lab6Cache is selected, click Add | New Region. See Figure 8

. [pic]

. Figure 8

. New Region Menu Item

27. Expand the Lab6Cache node in the tree view and highlight NewRegion1. Click Add | New Item.

. [pic]

. Figure 9

. Add New Item

28. When the TypePicker dialog appears, select CacheItemModel from the type list and then close the dialog (using the X button in the upper-right corner).

. [pic]

. Figure 10

. Type picker

29. Change the Key value in the property grid to Lab6Test, and then click File | Save current object (make sure that the item is selected in the tree). See Figure 11

. [pic]

. Figure 11

. Saving the object

30. Highlight Lab6Cache in the tree view and select File | Exercise Cache.

31. Go back to the console application. Wait for notifications to display.

. [pic]

. Figure 12

. Callbacks

. Note: This may take some time; the actual timing of callback is indeterminable.

.

Summary

In this lab, you used the Cache callback mechanism. This allows your applications to receive notification when changes to Cache object occur. You created an application that listens for DataCache callback events. You used a different application to add and update items in the cache, and saw how your application was notified.

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download