Getting Started with Windows Server AppFabric Cache

I struggled today to find a good “Getting started with Windows Server AppFabric Cache” tutorial – either my search fu failed me or it simply doesn’t exist.  Nevertheless, I was able to piece together the information I needed to get started.

I recommend you break this up into three steps:

  • Installing Windows Server AppFabric
  • Configuring Windows Server AppFabric Cache
  • Testing Windows Server AppFabric Cache with Sample Apps

I think this article will serve as a good tutorial on getting started, and we can refer back to it as the basis for more advanced scenarios.

Installing Windows Serve AppFabric

  1. Get the Web Platform Installer.
  2. Once it is installed and opened, select Options.
    Options
  3. Under Display additional scenarios select Enterprise.
    Enterprise
  4. Now you’ll see an Enterprise tab.  Select it, and choose Windows Server AppFabric.  Click Install.  This will start a multi-step process for installing Windows Server AppFabric (which in my case required two reboots to complete).
    Windows Server AppFabric

Configuring Windows Server AppFabric Cache

  1. Open the Windows Server AppFabric Configuration Wizard (Start –> Windows Server AppFabric –> Configure AppFabric).
  2. Click Next until you reach the Caching Service step.  Check Set Caching Service configuration, select SQL Server AppFabric Caching Service Configuration Store Provider for the configuration provider, and click Configure.
    Set Caching Service
  3. Check Create AppFabric Caching Service configuration database, confirm the Server name, and specify a Database name. Click OK.
    Specify Database
  4. When asked if you want to continue, click Yes.
    Configuration Database confirmation
  5. You will receive confirmation that your database was created and registered.
    Success
  6. On the Cache Node step, confirm the selected port nodes.
    Cache Node ports
  7. You will be asked to continue and apply the configuration settings; select Yes.
    Continue
  8. On the last step you’ll click Finish.
  9. Open up an elevated Windows PowerShell window.
  10. Add the Distributed Cache administration module
    Import-Module DistributedCacheAdministration
  11. Set the context of your Windows PowerShell session to the desired cache cluster with Use-CacheCluster.  You can run this without parameters to use the connection parameters provided during configuration.
    Use-CacheCluster
  12. Grant your user account access to the cache cluster as a client.  Specify your user and domain name.
    Grant-CacheAllowedClientAccount domain\username
  13. Verify your user account has been granted access.
    Get-CacheAllowedClientAccounts
  14. Start the cluster.
    Start-CacheCluster

Testing Windows Server AppFabric Cache with Sample Apps

  1. Grab a copy of the Microsoft AppFabric Samples, which are a series of very short examples.
  2. Extract the samples locally.
  3. Open up CacheSampleWebApp.sln (..\Samples\Cache\CacheSampleWebApp).
  4. Right-click the CreateOrder.aspx file and select Set As Start Page.
  5. Hit F5 to start the solution.
  6. Confirm that the cache is functioning by creating a sample order, getting the sample order, and updating the sample order.

Once you accomplish these three steps, you’ll have the basis for building more complex caching solutions.

I hope this helps!

Updated Windows Azure AppFabric SDK

Windows Azure AppFabricToday the Windows Azure AppFabric team released an update to the SDK.  This release is what’s called a QFE (quick fix engineering) update that’s intended to address bugs or breaking issues.  To my knowledge, this is the first QFE for the Windows Azure AppFabric.  In this case, the QFE fixed one bug:

Adding a Ws2007HttpRelayBinding endpoint that uses TransportWithMessageCredential security mode to a ServiceHost that also exposes a local MEX endpoint (not visible through the Service Bus) using the mexHttpsBinding, causes the local MEX endpoint to stop working. Invoking the MEX endpoint results in an InvalidOperationException.

If you are not experiencing this bug, there is no need for you to download the new version of the SDK.

Here’s the post from the Windows Azure AppFabric team blog.

Using the Expression Encoder SDK to encode lots of videos

I spent a good deal of time this weekend importing hours and hours family videos off our Mini DV cassettes.  Lots of fun, and LOTS of video!  Based on the size of these files, I was quite close to running out of room on my Windows Media Center.  So, I decided to encode the files as WMVs.  Huge size reduction with very little quality loss.

I decided to use Microsoft Expression Encoder 3 – a great tool.  The best part is that there’s an SDK and set of assemblies that you can use in your own applications.

Note: if you are using a 64-bit machine, be sure to set the platform target of your application to x86, or else you will get compilation errors from the Encoder assemblies.

Below you’ll find the application I wrote.  Let me explain my goals:

  • Multi-thread the application to encode more than one video at a time.
  • Leverage the multitude of cores in my machine.
  • Limit the number of threads (I chose the core count as a baseline).
  • Use source video and audio source to reduce quality lose.

In order to do this, I had to do two things: 1) find a way to pass in the file name into thread, and 2) keep track of the number of threads and limit them to the number of cores in the machine.

I spent a bit of time looking for a good approach.  In the end, I chose to use the delegate ParameterizedThreadStart, which takes a parameter of type object.  This way, I can create a thread using an instance of this delegate instead of just ThreadStart, and the overload to Thread.Start allows me to specify a value that is passed to this new thread.  (Be careful, though, as it only accepts a single parameter (although it can be a collection) and isn’t type-safe.)  Additionally, with this approach I was able to leverage a counter, and sleep whenever the counter is going to exceed the number of cores in my machine.

Here’s all the code.  For this to function, I imported the following assemblies (yes, you need Expression Encoder 3):

  • Microsoft.Expression.Encoder
  • Microsoft.Expression.Encoder.Types
  • Microsoft.Expression.Encoder.Utilities
  • WindowsBase
// This method is used to look-up the core the thread is using
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetCurrentProcessorNumber();
 
static string inputFolder = @"C:\temp\Videos";
static string outputFolder = @"C:\temp\OutputVideo";
static int count;
static int maxNum;
 
static void Main(string[] args)
{
    // Start the counter at zero
    count = 0;
    // Grab the processor count
    maxNum = Environment.ProcessorCount;
    // Iterate through the AVI files
    foreach (var fileName in System.IO.Directory.GetFiles(inputFolder, "*.avi"))
    {
        // Sleep/wait for a core to free up
        while (count > (maxNum - 1))
        {
            Thread.Sleep(500);
        }
        // Increment the counter
        count++;
        // Create the thread with the delegate
        Thread t = new Thread(new ParameterizedThreadStart(EncodeFile));
        // Start the thread, passing in the file name
        t.Start(fileName);
    }
}
 
public static void EncodeFile(object ofileName)
{
    string fileName = (string)ofileName;
    MediaItem mediaItem = new MediaItem(fileName);
    mediaItem.OutputFormat = new WindowsMediaOutputFormat();
    // Use source video profile if available
    if (mediaItem.SourceVideoProfile != null)
    {
        mediaItem.OutputFormat.VideoProfile = mediaItem.SourceVideoProfile;
    }
    else
    {
        mediaItem.OutputFormat.VideoProfile = new AdvancedVC1VideoProfile()
        {
            Size = mediaItem.MainMediaFile.VideoStreams[0].VideoSize,
            Bitrate = new ConstantBitrate(1000)
        };
    }
    // Use source audio profile if available
    if (mediaItem.SourceAudioProfile != null)
    {
        mediaItem.OutputFormat.AudioProfile = mediaItem.SourceAudioProfile;
    }
    else
    {
        mediaItem.OutputFormat.AudioProfile = new WmaAudioProfile();
    }
    // Create a job and the media item for the video we wish to encode.
    Job job = new Job();
    job.MediaItems.Add(mediaItem);
    // Set up the progress callback function
    job.EncodeProgress
        += new EventHandler<EncodeProgressEventArgs>(OnProgress);
    // Set up the completed callback function
    job.EncodeCompleted
        += new EventHandler<EncodeCompletedEventArgs>(job_EncodeCompleted);
    // Set the output directory and encode
    job.OutputDirectory = outputFolder;
    // Do not create a job subfolder
    job.CreateSubfolder = false;
    // Encode
    job.Encode();
}
 
static void job_EncodeCompleted(object sender, EncodeCompletedEventArgs e)
{
    // Decrement the counter
    count--;
}
 
static void OnProgress(object sender, EncodeProgressEventArgs e)
{
    // Write out information
    Console.WriteLine(
        count.ToString() + " : " +
        GetCurrentProcessorNumber().ToString() + " : " +
        System.Threading.Thread.CurrentThread.ManagedThreadId.ToString() + " : " +
        e.Progress + " : " +
        e.CurrentItem.ActualOutputFileName);
}

 

Good stuff.

I’m running it from the console, so you will have to make some modifications if you want it to run in a more sophisticated application.  Works for me, though – I just start it up at the end of the day.  Here you can see the information that’s written out to the console (note the variety of cores leveraged):

Console Output

It’s fun to see my machine working this hard.  Every core is pegged.

Pegged Cores

Hope someone finds this useful.  Anyone see a better way to approach this?

Article: Tips for Migrating Your Applications to the Cloud

I had the great pleasure of co-authoring an article on application migration to Windows Azure for MSDN Magazine with my friend George Huey, creator of the SQL Azure Migration Wizard.  This article stems from our work helping dozens of customers – both small and big – migrate their existing applications to run in the cloud.

Tips for Migrating Your Applications to the Cloud

(Ours is the second down on the left.)

While this article is by no means exhaustive, we did try to explain some of the common patterns and scenarios we faced when helping customers migrate their applications into Windows Azure.  I hope you find it valuable.

Use social web providers in less than 5 minutes

Significant Access Control Service (ACS) updates released to AppFabric LABS environment today.  Take a look at the Windows Azure AppFabric team blog for the formal announcement.  I’d rather show you a quick demo.

Quick summary of new capabilities in the Access Control Service:

  • Integration with Windows Identity Foundation (WIF) and tooling
  • Out-of-the-box support for popular web identity providers including: Windows Live ID, OpenID, Google, Yahoo, and Facebook
  • Out-of-the-box support for Active Directory Federation Server v2.0
  • Support for OAuth WRAP, WS-Trust, and WS-Federation protocols
  • Support for the SAML 1.1, SAML 2.0, and Simple Web Token (SWT) token formats
  • Integrated and customizable Home Realm Discovery that allows end-users to choose their identity provider
  • An OData-based Management Service that provides programmatic access to ACS configuration
  • A Web Portal that allows administrative access to ACS configuration

Try this out yourself.  Use Visual Studio, install Windows Identity Foundation, and go to https://portal.appfabriclabs.com/.

For detailed information and a more verbose walkthrough (i.e. including explanations), listen to Justin Smith’s walkthrough and interview.