Update Encoding Reserved Units from .NET

Yes, I know, today I haven’t stopped publishing but there is so much to learn! and these are questions or requests asking me and whenever I have the chance, I like to share with everyone. This morning we saw how can change the reserved units for streaming from .NET now I want to update the encoding units that have reserved:

using System;
using System.Configuration;
using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Text;
namespace ReservedUnits
    class Program
        static void Main(string[] args)
            //1. Get Application Settings
            var ServiceManagementUrl = ConfigurationManager.AppSettings["ServiceManagementUrl"];
            var SuscriptionId = ConfigurationManager.AppSettings["SubscriptionId"];
            var MediaAccountName = ConfigurationManager.AppSettings["MediaAccountName"];
            var CertificateB64 = ConfigurationManager.AppSettings["CertificateB64"];
            var Units = 1;
            //2. Create the request
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(string.Format("{0}/{1}/services/mediaservices/Accounts/{2}/ServiceQuotas", ServiceManagementUrl, SuscriptionId, MediaAccountName));
            request.Method = "PUT";
            request.Accept = "application/json";
            request.ContentType = "application/json; charset=utf-8";
            request.Headers.Add("x-ms-version", "2011-10-01");
            request.Headers.Add("Accept-Encoding: gzip, deflate");
            request.ClientCertificates.Add(new X509Certificate2(Convert.FromBase64String(CertificateB64)));
            //3. Payload
            string json = @"[{""RequestedUnits"": " + Units + @",""ServiceType"":""Encoding""}]";
            //4. Make the call
            using (Stream requestStream = request.GetRequestStream())
                var requestBytes = Encoding.ASCII.GetBytes(json);
                requestStream.Write(requestBytes, 0, requestBytes.Length);
                //5. Get the response
                using (var response = (HttpWebResponse)request.GetResponse())
                    var responseStream = response.GetResponseStream();
                    var reader = new StreamReader(responseStream);

First at all I have to say that this REST API is undocumented and I cannot guarantee that this code will keep running in the future.. I got this information looking the source code from Azure Media Services Explorer Tool.

For this operation doesn’t have a method in the SDK yet, so you have to make the call directly to the REST API. We need the administration URL (https://management.core.windows.net), subscription id, the account name of Azure Media Service we want change and a certificate that is associated with the Azure portal. For this example (not for production), I’ve obtained all this information from the publish settings file. To download yours, simply click on this link and logging with your Microsoft Azure credentials.

Once we have all values, we create the request through WebRequest.Create and configure it with the values shown in the above code. To add the certificate I used new X509Certificate2 (Convert.FromBase64String (CertificateB64) to do the transformation. The payload of the request is in JSON format, simplifying the example, I specified the object into a string text, concatenated with the number of units that we will request.

Finally, we make the call and get the response of the request. The result should be:

Encoding Reserved Units request result

Hope this helps.