Windows Azure Media Services .NET SDK 3.0.0.2 release

Posted on

Today Microsoft released version 3.0.0.2  of Windows Azure Media Services .NET SDK which is hosted in nuget.org  http://bit.ly/1gUB34k.

3.0.0.2 is a minor release addressing few bug fixes,code refactoring within sdk code base and splitting scenario e2e tests from unit tests into different solutions.

I want to highlight few changes which recently have been made in 3.0.0.2 and  3.0.0.1  versions which might  be beneficial for you application performance.

 

Using MediaServicesCredentials and how to reuse ACS token to avoid redundant calls to ACS

CloudMediaContext  is a first type which you use to talk with REST api. It encapsulate logic to get access token from Azure ACS service  and provides you easy entry point  to available entity collections to work with.

Prior to 3.0.0.0 every time you create instance of CloudMediaContext  WAMS .NET SDK was sending to REST API 2 requests:

  • request to Windows Azure Access Control Service REST API  to get  authentication token
  • request to Azure Media Services REST API to detect which api endpoint you will be talking going forward based on your account region

To optimize performance in multithreaded, multi instance  scenarios like web farm you want to get a token and end point from REST once and have ability recreate CloudMediaContext based on values which you already got previously.

 

 MediaServicesCredentials mediaServicesCredentials = new MediaServicesCredentials("myaccount", "mykey");
            
 CloudMediaContext cntx = new CloudMediaContext(mediaServicesCredentials);
            
 //At this point token is null since no request issued to ACS yet
 string token = mediaServicesCredentials.AccessToken;
            
 //We are sending Request to ACS and Detecting endpoint only when we first time accessing our entities from collections
 var asset = cntx.Assets.FirstOrDefault();
            
DateTime exprires = mediaServicesCredentials.TokenExpiration;

//You can save token to cache and reassign it later in order to avoid request to ACS
//Example:
// MediaServicesCredentials mediaServicesCredentials = new MediaServicesCredentials("myaccount", "mykey");
// mediaServicesCredentials.AccessToken == token;
            

//You can also force token Refresh earlier. Please note that refresh happen automatically if token has been expired
mediaServicesCredentials.RefreshToken();

 

As you can see from example above newly added constructor allows you to create credential object and have direct access to acs token value and it’s expiration. Once you get these values in session memory or distributed db you will be able to use it in order to recreate MediaServicesCredentials and CloudMediaContext in multiple threads without hitting ACS over and over again. Please note that you need to provide account name and key since these values are used in order to refresh token once it will be expired.

Using OrderBy in LINQ statements

In  3.0.0.2 we fixed issues related to usage of  LINQ queries with OrderBy statements:

Example:

  var asset1 = _mediaContext
                .Assets
                .OrderByDescending(c => c.Created)
                .Where(a => a.Name.Length > 1)
                .Skip(10)
                .Take(5)
                .First();

            var asset2 = _mediaContext
                .Assets
                .Where(a => a.Name.Length > 1)
                .Skip(10)
                .Take(5)
                .First();

            var asset3 = _mediaContext
                .Assets
                .Where(a => a.Name.Length > 1)
                .OrderByDescending(c => c.Created)
                .Skip(10)
                .Take(5)
                .First();

 

For those who forking and running tests

As you might know all WAMS SDK code is hosted in github at https://github.com/WindowsAzure/azure-sdk-for-media-services giving  developers ability to fork, explore and contribute.

Test solution has been splited into 2 separate solutions:

  • Unit tests. Tests which are not require connection to REST API
  • Scenario tests. End 2 end test which are talking with REST API.

Also there were several refactoring within code base to make code more modular and mockable.

Tags: