/// <summary>
        /// The Get Affinity Group Properties operation returns the system
        /// properties associated with the specified affinity group.  (see
        /// http://msdn.microsoft.com/en-us/library/windowsazure/ee460789.aspx
        /// for more information)
        /// </summary>
        /// <param name='affinityGroupName'>
        /// The name of the desired affinity group as returned by the name
        /// element of the List Affinity Groups operation.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// The Get Affinity Group operation response.
        /// </returns>
        public async Task <AffinityGroupGetResponse> GetAsync(string affinityGroupName, CancellationToken cancellationToken)
        {
            // Validate
            if (affinityGroupName == null)
            {
                throw new ArgumentNullException("affinityGroupName");
            }

            // Tracing
            bool   shouldTrace  = CloudContext.Configuration.Tracing.IsEnabled;
            string invocationId = null;

            if (shouldTrace)
            {
                invocationId = Tracing.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("affinityGroupName", affinityGroupName);
                Tracing.Enter(invocationId, this, "GetAsync", tracingParameters);
            }

            // Construct URL
            string url = new Uri(this.Client.BaseUri, "/").ToString() + this.Client.Credentials.SubscriptionId + "/affinitygroups/" + affinityGroupName;

            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;

            try
            {
                httpRequest            = new HttpRequestMessage();
                httpRequest.Method     = HttpMethod.Get;
                httpRequest.RequestUri = new Uri(url);

                // Set Headers
                httpRequest.Headers.Add("x-ms-version", "2013-03-01");

                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        Tracing.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                    if (shouldTrace)
                    {
                        Tracing.ReceiveResponse(invocationId, httpResponse);
                    }
                    HttpStatusCode statusCode = httpResponse.StatusCode;
                    if (statusCode != HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false), CloudExceptionType.Xml);
                        if (shouldTrace)
                        {
                            Tracing.Error(invocationId, ex);
                        }
                        throw ex;
                    }

                    // Create Result
                    AffinityGroupGetResponse result = null;
                    // Deserialize Response
                    cancellationToken.ThrowIfCancellationRequested();
                    string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                    result = new AffinityGroupGetResponse();
                    XDocument responseDoc = XDocument.Parse(responseContent);

                    XElement affinityGroupElement = responseDoc.Element(XName.Get("AffinityGroup", "http://schemas.microsoft.com/windowsazure"));
                    if (affinityGroupElement != null)
                    {
                        XElement nameElement = affinityGroupElement.Element(XName.Get("Name", "http://schemas.microsoft.com/windowsazure"));
                        if (nameElement != null)
                        {
                            string nameInstance = nameElement.Value;
                            result.Name = nameInstance;
                        }

                        XElement labelElement = affinityGroupElement.Element(XName.Get("Label", "http://schemas.microsoft.com/windowsazure"));
                        if (labelElement != null)
                        {
                            string labelInstance = TypeConversion.FromBase64String(labelElement.Value);
                            result.Label = labelInstance;
                        }

                        XElement descriptionElement = affinityGroupElement.Element(XName.Get("Description", "http://schemas.microsoft.com/windowsazure"));
                        if (descriptionElement != null)
                        {
                            string descriptionInstance = descriptionElement.Value;
                            result.Description = descriptionInstance;
                        }

                        XElement locationElement = affinityGroupElement.Element(XName.Get("Location", "http://schemas.microsoft.com/windowsazure"));
                        if (locationElement != null)
                        {
                            string locationInstance = locationElement.Value;
                            result.Location = locationInstance;
                        }

                        XElement hostedServicesSequenceElement = affinityGroupElement.Element(XName.Get("HostedServices", "http://schemas.microsoft.com/windowsazure"));
                        if (hostedServicesSequenceElement != null)
                        {
                            foreach (XElement hostedServicesElement in hostedServicesSequenceElement.Elements(XName.Get("HostedService", "http://schemas.microsoft.com/windowsazure")))
                            {
                                AffinityGroupGetResponse.HostedServiceReference hostedServiceInstance = new AffinityGroupGetResponse.HostedServiceReference();
                                result.HostedServices.Add(hostedServiceInstance);

                                XElement urlElement = hostedServicesElement.Element(XName.Get("Url", "http://schemas.microsoft.com/windowsazure"));
                                if (urlElement != null)
                                {
                                    Uri urlInstance = TypeConversion.TryParseUri(urlElement.Value);
                                    hostedServiceInstance.Uri = urlInstance;
                                }

                                XElement serviceNameElement = hostedServicesElement.Element(XName.Get("ServiceName", "http://schemas.microsoft.com/windowsazure"));
                                if (serviceNameElement != null)
                                {
                                    string serviceNameInstance = serviceNameElement.Value;
                                    hostedServiceInstance.ServiceName = serviceNameInstance;
                                }
                            }
                        }

                        XElement storageServicesSequenceElement = affinityGroupElement.Element(XName.Get("StorageServices", "http://schemas.microsoft.com/windowsazure"));
                        if (storageServicesSequenceElement != null)
                        {
                            foreach (XElement storageServicesElement in storageServicesSequenceElement.Elements(XName.Get("StorageService", "http://schemas.microsoft.com/windowsazure")))
                            {
                                AffinityGroupGetResponse.StorageServiceReference storageServiceInstance = new AffinityGroupGetResponse.StorageServiceReference();
                                result.StorageServices.Add(storageServiceInstance);

                                XElement urlElement2 = storageServicesElement.Element(XName.Get("Url", "http://schemas.microsoft.com/windowsazure"));
                                if (urlElement2 != null)
                                {
                                    Uri urlInstance2 = TypeConversion.TryParseUri(urlElement2.Value);
                                    storageServiceInstance.Uri = urlInstance2;
                                }

                                XElement serviceNameElement2 = storageServicesElement.Element(XName.Get("ServiceName", "http://schemas.microsoft.com/windowsazure"));
                                if (serviceNameElement2 != null)
                                {
                                    string serviceNameInstance2 = serviceNameElement2.Value;
                                    storageServiceInstance.ServiceName = serviceNameInstance2;
                                }
                            }
                        }

                        XElement capabilitiesSequenceElement = affinityGroupElement.Element(XName.Get("Capabilities", "http://schemas.microsoft.com/windowsazure"));
                        if (capabilitiesSequenceElement != null)
                        {
                            foreach (XElement capabilitiesElement in capabilitiesSequenceElement.Elements(XName.Get("Capability", "http://schemas.microsoft.com/windowsazure")))
                            {
                                result.Capabilities.Add(capabilitiesElement.Value);
                            }
                        }
                    }

                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }

                    if (shouldTrace)
                    {
                        Tracing.Exit(invocationId, result);
                    }
                    return(result);
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }
        /// <summary>
        /// The Get Affinity Group Properties operation returns the system
        /// properties associated with the specified affinity group.  (see
        /// http://msdn.microsoft.com/en-us/library/windowsazure/ee460789.aspx
        /// for more information)
        /// </summary>
        /// <param name='affinityGroupName'>
        /// Required. The name of the desired affinity group as returned by the
        /// name element of the List Affinity Groups operation.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// The Get Affinity Group operation response.
        /// </returns>
        public async System.Threading.Tasks.Task <Microsoft.WindowsAzure.Management.Models.AffinityGroupGetResponse> GetAsync(string affinityGroupName, CancellationToken cancellationToken)
        {
            // Validate
            if (affinityGroupName == null)
            {
                throw new ArgumentNullException("affinityGroupName");
            }

            // Tracing
            bool   shouldTrace  = CloudContext.Configuration.Tracing.IsEnabled;
            string invocationId = null;

            if (shouldTrace)
            {
                invocationId = Tracing.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("affinityGroupName", affinityGroupName);
                Tracing.Enter(invocationId, this, "GetAsync", tracingParameters);
            }

            // Construct URL
            string url     = "/" + (this.Client.Credentials.SubscriptionId != null ? this.Client.Credentials.SubscriptionId.Trim() : "") + "/affinitygroups/" + affinityGroupName.Trim();
            string baseUrl = this.Client.BaseUri.AbsoluteUri;

            // Trim '/' character from the end of baseUrl and beginning of url.
            if (baseUrl[baseUrl.Length - 1] == '/')
            {
                baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
            }
            if (url[0] == '/')
            {
                url = url.Substring(1);
            }
            url = baseUrl + "/" + url;
            url = url.Replace(" ", "%20");

            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;

            try
            {
                httpRequest            = new HttpRequestMessage();
                httpRequest.Method     = HttpMethod.Get;
                httpRequest.RequestUri = new Uri(url);

                // Set Headers
                httpRequest.Headers.Add("x-ms-version", "2014-05-01");

                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        Tracing.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                    if (shouldTrace)
                    {
                        Tracing.ReceiveResponse(invocationId, httpResponse);
                    }
                    HttpStatusCode statusCode = httpResponse.StatusCode;
                    if (statusCode != HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
                        if (shouldTrace)
                        {
                            Tracing.Error(invocationId, ex);
                        }
                        throw ex;
                    }

                    // Create Result
                    AffinityGroupGetResponse result = null;
                    // Deserialize Response
                    cancellationToken.ThrowIfCancellationRequested();
                    string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                    result = new AffinityGroupGetResponse();
                    XDocument responseDoc = XDocument.Parse(responseContent);

                    XElement affinityGroupElement = responseDoc.Element(XName.Get("AffinityGroup", "http://schemas.microsoft.com/windowsazure"));
                    if (affinityGroupElement != null)
                    {
                        XElement nameElement = affinityGroupElement.Element(XName.Get("Name", "http://schemas.microsoft.com/windowsazure"));
                        if (nameElement != null)
                        {
                            string nameInstance = nameElement.Value;
                            result.Name = nameInstance;
                        }

                        XElement labelElement = affinityGroupElement.Element(XName.Get("Label", "http://schemas.microsoft.com/windowsazure"));
                        if (labelElement != null)
                        {
                            string labelInstance = TypeConversion.FromBase64String(labelElement.Value);
                            result.Label = labelInstance;
                        }

                        XElement descriptionElement = affinityGroupElement.Element(XName.Get("Description", "http://schemas.microsoft.com/windowsazure"));
                        if (descriptionElement != null)
                        {
                            string descriptionInstance = descriptionElement.Value;
                            result.Description = descriptionInstance;
                        }

                        XElement locationElement = affinityGroupElement.Element(XName.Get("Location", "http://schemas.microsoft.com/windowsazure"));
                        if (locationElement != null)
                        {
                            string locationInstance = locationElement.Value;
                            result.Location = locationInstance;
                        }

                        XElement hostedServicesSequenceElement = affinityGroupElement.Element(XName.Get("HostedServices", "http://schemas.microsoft.com/windowsazure"));
                        if (hostedServicesSequenceElement != null)
                        {
                            result.HostedServices = new List <AffinityGroupGetResponse.HostedServiceReference>();
                            foreach (XElement hostedServicesElement in hostedServicesSequenceElement.Elements(XName.Get("HostedService", "http://schemas.microsoft.com/windowsazure")))
                            {
                                AffinityGroupGetResponse.HostedServiceReference hostedServiceInstance = new AffinityGroupGetResponse.HostedServiceReference();
                                result.HostedServices.Add(hostedServiceInstance);

                                XElement urlElement = hostedServicesElement.Element(XName.Get("Url", "http://schemas.microsoft.com/windowsazure"));
                                if (urlElement != null)
                                {
                                    Uri urlInstance = TypeConversion.TryParseUri(urlElement.Value);
                                    hostedServiceInstance.Uri = urlInstance;
                                }

                                XElement serviceNameElement = hostedServicesElement.Element(XName.Get("ServiceName", "http://schemas.microsoft.com/windowsazure"));
                                if (serviceNameElement != null)
                                {
                                    string serviceNameInstance = serviceNameElement.Value;
                                    hostedServiceInstance.ServiceName = serviceNameInstance;
                                }
                            }
                        }

                        XElement storageServicesSequenceElement = affinityGroupElement.Element(XName.Get("StorageServices", "http://schemas.microsoft.com/windowsazure"));
                        if (storageServicesSequenceElement != null)
                        {
                            result.StorageServices = new List <AffinityGroupGetResponse.StorageServiceReference>();
                            foreach (XElement storageServicesElement in storageServicesSequenceElement.Elements(XName.Get("StorageService", "http://schemas.microsoft.com/windowsazure")))
                            {
                                AffinityGroupGetResponse.StorageServiceReference storageServiceInstance = new AffinityGroupGetResponse.StorageServiceReference();
                                result.StorageServices.Add(storageServiceInstance);

                                XElement urlElement2 = storageServicesElement.Element(XName.Get("Url", "http://schemas.microsoft.com/windowsazure"));
                                if (urlElement2 != null)
                                {
                                    Uri urlInstance2 = TypeConversion.TryParseUri(urlElement2.Value);
                                    storageServiceInstance.Uri = urlInstance2;
                                }

                                XElement serviceNameElement2 = storageServicesElement.Element(XName.Get("ServiceName", "http://schemas.microsoft.com/windowsazure"));
                                if (serviceNameElement2 != null)
                                {
                                    string serviceNameInstance2 = serviceNameElement2.Value;
                                    storageServiceInstance.ServiceName = serviceNameInstance2;
                                }
                            }
                        }

                        XElement capabilitiesSequenceElement = affinityGroupElement.Element(XName.Get("Capabilities", "http://schemas.microsoft.com/windowsazure"));
                        if (capabilitiesSequenceElement != null)
                        {
                            result.Capabilities = new List <string>();
                            foreach (XElement capabilitiesElement in capabilitiesSequenceElement.Elements(XName.Get("Capability", "http://schemas.microsoft.com/windowsazure")))
                            {
                                result.Capabilities.Add(capabilitiesElement.Value);
                            }
                        }

                        XElement createdTimeElement = affinityGroupElement.Element(XName.Get("CreatedTime", "http://schemas.microsoft.com/windowsazure"));
                        if (createdTimeElement != null && string.IsNullOrEmpty(createdTimeElement.Value) == false)
                        {
                            DateTime createdTimeInstance = DateTime.Parse(createdTimeElement.Value, CultureInfo.InvariantCulture);
                            result.CreatedTime = createdTimeInstance;
                        }

                        XElement computeCapabilitiesElement = affinityGroupElement.Element(XName.Get("ComputeCapabilities", "http://schemas.microsoft.com/windowsazure"));
                        if (computeCapabilitiesElement != null)
                        {
                            ComputeCapabilities computeCapabilitiesInstance = new ComputeCapabilities();
                            result.ComputeCapabilities = computeCapabilitiesInstance;

                            XElement virtualMachinesRoleSizesSequenceElement = computeCapabilitiesElement.Element(XName.Get("VirtualMachinesRoleSizes", "http://schemas.microsoft.com/windowsazure"));
                            if (virtualMachinesRoleSizesSequenceElement != null)
                            {
                                computeCapabilitiesInstance.VirtualMachinesRoleSizes = new System.Collections.Generic.List <string>();
                                foreach (XElement virtualMachinesRoleSizesElement in virtualMachinesRoleSizesSequenceElement.Elements(XName.Get("RoleSize", "http://schemas.microsoft.com/windowsazure")))
                                {
                                    computeCapabilitiesInstance.VirtualMachinesRoleSizes.Add(virtualMachinesRoleSizesElement.Value);
                                }
                            }

                            XElement webWorkerRoleSizesSequenceElement = computeCapabilitiesElement.Element(XName.Get("WebWorkerRoleSizes", "http://schemas.microsoft.com/windowsazure"));
                            if (webWorkerRoleSizesSequenceElement != null)
                            {
                                computeCapabilitiesInstance.WebWorkerRoleSizes = new System.Collections.Generic.List <string>();
                                foreach (XElement webWorkerRoleSizesElement in webWorkerRoleSizesSequenceElement.Elements(XName.Get("RoleSize", "http://schemas.microsoft.com/windowsazure")))
                                {
                                    computeCapabilitiesInstance.WebWorkerRoleSizes.Add(webWorkerRoleSizesElement.Value);
                                }
                            }
                        }
                    }

                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }

                    if (shouldTrace)
                    {
                        Tracing.Exit(invocationId, result);
                    }
                    return(result);
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }