public void AttachStorageAccountToMediaServiceAccount(MediaServicesAccount accountInfo, AttachStorageAccountRequest storageaccount)
        {
            try
            {
                var clientCert = GetClientCertificate();
                stringBuilderLog.AppendLine("..Called GetClientCertificate");

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(string.Format("{0}/{1}/services/mediaservices/Accounts/{2}/StorageAccounts", Endpoint, SubscriptionId, accountInfo.AccountName));
                request.Method      = "POST";
                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(clientCert);
                stringBuilderLog.AppendLine("..HttpWebRequest built");
                stringBuilderLog.AppendLine("..HttpWebRequest Request Uri: " + request.RequestUri.ToString());

                string jsonString;

                using (MemoryStream ms = new MemoryStream())
                {
                    DataContractJsonSerializer serializer
                        = new DataContractJsonSerializer(typeof(AttachStorageAccountRequest));

                    serializer.WriteObject(ms, storageaccount);
                    stringBuilderLog.AppendLine("..memorystream serialized");

                    jsonString = Encoding.Default.GetString(ms.ToArray());

                    stringBuilderLog.AppendLine("..jsonString created");
                }

                using (Stream requestStream = request.GetRequestStream())
                {
                    var requestBytes = System.Text.Encoding.ASCII.GetBytes(jsonString);
                    stringBuilderLog.AppendLine("..jsonString to bytes");

                    requestStream.Write(requestBytes, 0, requestBytes.Length);
                    stringBuilderLog.AppendLine("..requestStream.Write");

                    requestStream.Close();
                    stringBuilderLog.AppendLine("..requestStream.Close");
                }

                using (var response = (HttpWebResponse)request.GetResponse())
                {
                    if (response.StatusCode == HttpStatusCode.NoContent)
                    {
                        stringBuilderLog.AppendLine("..Response NoContent Status code - The primary key was regenerated.");
                    }

                    stringBuilderLog.AppendLine("..Response Status Code: " + response.StatusCode.ToString());
                    stringBuilderLog.AppendLine("..Response Status Description: " + response.StatusDescription);
                    stringBuilderLog.AppendLine("..Response Uri: " + response.ResponseUri);
                    stringBuilderLog.AppendLine("..Response Server: " + response.Server);
                    stringBuilderLog.AppendLine("..Response ContentType: " + response.ContentType);
                    stringBuilderLog.AppendLine("..Response ContentLength: " + response.ContentLength);
                    stringBuilderLog.AppendLine("..Response ContentEncoding: " + response.ContentEncoding);
                }
            }
            catch (Exception ex)
            { throw ex; }
        }
        private void DoAttachAnotherStorageAccount()
        {
            AttachStorage form = new AttachStorage(_credentials);

            if (form.ShowDialog() == DialogResult.OK)
            {
                ManagementRESTAPIHelper helper = new ManagementRESTAPIHelper(form.GetAzureServiceManagementURL, form.GetCertBody, form.GetAzureSubscriptionID);

                // Initialize the AccountInfo class.
                MediaServicesAccount accountInfo = new MediaServicesAccount()
                {
                    AccountName = _context.Credentials.ClientId,
                    StorageAccountName = _context.DefaultStorageAccount.Name
                };

                AttachStorageAccountRequest storageAccountToAttach = new AttachStorageAccountRequest()
                {
                    StorageAccountName = form.GetStorageName,
                    StorageAccountKey = form.GetStorageKey,
                    BlobStorageEndpointUri = form.GetStorageEndpointURL
                };

                // Call AttachStorageAccountToMediaServiceAccount to 
                // attach an existing storage account to the Media Services account.
                try
                {
                    helper.AttachStorageAccountToMediaServiceAccount(accountInfo, storageAccountToAttach);
                    TextBoxLogWriteLine("Storage account '{0}' attached to '{1}' account.", form.GetStorageName, _context.Credentials.ClientId);
                }
                catch (Exception ex)
                {
                    // Add useful information to the exception
                    TextBoxLogWriteLine("There is a problem when attaching the storage account.", true);
                    TextBoxLogWriteLine(ex);
                    TextBoxLogWriteLine(helper.stringBuilderLog.ToString());
                }
            }
        }
        public void AttachStorageAccountToMediaServiceAccount(MediaServicesAccount accountInfo, AttachStorageAccountRequest storageaccount)
        {
            try
            {
                var clientCert = GetClientCertificate();
                stringBuilderLog.AppendLine("..Called GetClientCertificate");

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(string.Format("{0}/{1}/services/mediaservices/Accounts/{2}/StorageAccounts", Endpoint, SubscriptionId, accountInfo.AccountName));
                request.Method = "POST";
                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(clientCert);
                stringBuilderLog.AppendLine("..HttpWebRequest built");
                stringBuilderLog.AppendLine("..HttpWebRequest Request Uri: " + request.RequestUri.ToString());

                string jsonString;

                using (MemoryStream ms = new MemoryStream())
                {
                    DataContractJsonSerializer serializer
                            = new DataContractJsonSerializer(typeof(AttachStorageAccountRequest));

                    serializer.WriteObject(ms, storageaccount);
                    stringBuilderLog.AppendLine("..memorystream serialized");

                    jsonString = Encoding.Default.GetString(ms.ToArray());

                    stringBuilderLog.AppendLine("..jsonString created");
                }

                using (Stream requestStream = request.GetRequestStream())
                {
                    var requestBytes = System.Text.Encoding.ASCII.GetBytes(jsonString);
                    stringBuilderLog.AppendLine("..jsonString to bytes");

                    requestStream.Write(requestBytes, 0, requestBytes.Length);
                    stringBuilderLog.AppendLine("..requestStream.Write");

                    requestStream.Close();
                    stringBuilderLog.AppendLine("..requestStream.Close");

                }

                using (var response = (HttpWebResponse)request.GetResponse())
                {
                    if (response.StatusCode == HttpStatusCode.NoContent)
                        stringBuilderLog.AppendLine("..Response NoContent Status code - The primary key was regenerated.");

                    stringBuilderLog.AppendLine("..Response Status Code: " + response.StatusCode.ToString());
                    stringBuilderLog.AppendLine("..Response Status Description: " + response.StatusDescription);
                    stringBuilderLog.AppendLine("..Response Uri: " + response.ResponseUri);
                    stringBuilderLog.AppendLine("..Response Server: " + response.Server);
                    stringBuilderLog.AppendLine("..Response ContentType: " + response.ContentType);
                    stringBuilderLog.AppendLine("..Response ContentLength: " + response.ContentLength);
                    stringBuilderLog.AppendLine("..Response ContentEncoding: " + response.ContentEncoding);
                }
            }
            catch (Exception ex)
            { throw ex; }
        }