Azure Media Services: Modify Streaming Reserved Units from .NET

In order to automate the reserved units for the streaming service, we can use the following code in C#:

[csharp]using Microsoft.WindowsAzure.MediaServices.Client;
using System;
using System.Configuration;
using System.Linq;

namespace StreamingReservedUnits
{
class Program
{
static void Main(string[] args)
{
//0.Nuget: Install-Package windowsazure.mediaservices

//1. Credentials
var AccountName = ConfigurationManager.AppSettings["AccountName"];
var AccountKey = ConfigurationManager.AppSettings["AccountKey"];

//2. Get Context
var context = new CloudMediaContext(AccountName, AccountKey);

//3. Create tasks
for (var index = 0; index < context.StreamingEndpoints.Count(); index++)
{
var endpoint = context.StreamingEndpoints.Skip(index).Take(1).FirstOrDefault();

Console.WriteLine("Endpoint {0} has {1} units", endpoint.Name, endpoint.ScaleUnits);

if (endpoint.State != StreamingEndpointState.Scaling)
endpoint.Scale(0);
}

Console.WriteLine("Streaming endpoint units off!");

Console.ReadLine();
}
}
}
[/csharp]

It’s available an async method for this action as well: ScaleAsync. This is an example to use it:

[csharp]using Microsoft.WindowsAzure.MediaServices.Client;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Threading.Tasks;

namespace StreamingReservedUnits
{
class Program
{
static void Main(string[] args)
{
//0.Nuget: Install-Package windowsazure.mediaservices

//1. Credentials and constants
var AccountName = ConfigurationManager.AppSettings["AccountName"];
var AccountKey = ConfigurationManager.AppSettings["AccountKey"];
var ReservedUnits = 0;

//2. Get Context
var context = new CloudMediaContext(AccountName, AccountKey);

var task = new List<Task>();

//3. Create tasks
for (var index = 0; index < context.StreamingEndpoints.Count(); index++)
{
var endpoint = context.StreamingEndpoints.Skip(index).Take(1).FirstOrDefault();

Console.WriteLine("Endpoint {0} has {1} units", endpoint.Name, endpoint.ScaleUnits);

if (endpoint.State != StreamingEndpointState.Scaling)
{
task.Add(endpoint.ScaleAsync(ReservedUnits));
Console.WriteLine("From {0} units to {1}", endpoint.ScaleUnits, ReservedUnits);
}
}

Task.WaitAll(task.ToArray());

Console.WriteLine("All endpoints updated!");

Console.ReadLine();
}
}
}[/csharp]

Also, If we want to use a Scheduler in order to launch this task every X time, you can use Azure WebJobs following this post.

Thanks to Vito for providing me the vintage version of this code. I only had to update it 😉

Cheers!

Deja un comentario

Este sitio usa Akismet para reducir el spam. Aprende cómo se procesan los datos de tus comentarios.