Example #1
0
        /// <summary>
        /// Retrieves the publish settings file (.pubxml) for the given Azure Website.
        /// </summary>
        /// <returns>XML document with the contents of .pubxml, or <c>null</c> if it could not be retrieved.</returns>
        private async Task <XDocument> GetPublishXml(AzureWebSiteInfo webSiteInfo)
        {
            // To build the publish settings request URL, we need to know subscription ID, site name, and web region to which it belongs,
            // but we only have subscription ID and the public URL of the site at this point. Use the Azure Website service to look up
            // the site from those two, and retrieve the missing info.

            IVsAzureServices webSiteServices = new VsAzureServicesShim(NodejsPackage.GetGlobalService(_azureServicesType));

            if (webSiteServices == null)
            {
                return(null);
            }

            var webSiteService = webSiteServices.GetAzureWebSitesService();

            if (webSiteService == null)
            {
                return(null);
            }

            var subscriptions = await webSiteService.GetSubscriptionsAsync();

            var subscription = subscriptions.FirstOrDefault(sub => sub.SubscriptionId == webSiteInfo.SubscriptionId);

            if (subscription == null)
            {
                return(null);
            }

            var resources = await subscription.GetResourcesAsync(false);

            var webSite = resources.OfType <IAzureWebSite>().FirstOrDefault(ws => {
                Uri browseUri;
                Uri.TryCreate(ws.BrowseURL, UriKind.Absolute, out browseUri);
                return(browseUri != null && browseUri.Equals(webSiteInfo.Uri));
            });

            if (webSite == null)
            {
                return(null);
            }

            // Prepare a web request to get the publish settings.
            // See http://msdn.microsoft.com/en-us/library/windowsazure/dn166996.aspx
            string requestPath = string.Format(CultureInfo.InvariantCulture,
                                               "{0}/services/WebSpaces/{1}/sites/{2}/publishxml",
                                               subscription.SubscriptionId,
                                               webSite.WebSpace,
                                               webSite.Name);
            Uri            requestUri = new Uri(((IAzureSubscription)subscription).ServiceManagementEndpointUri, requestPath);
            HttpWebRequest request    = (HttpWebRequest)WebRequest.Create(requestUri);

            request.Method      = "GET";
            request.ContentType = "application/xml";
            request.Headers.Add("x-ms-version", "2010-10-28");

            // Set up authentication for the request, depending on whether the associated subscription context is
            // account-based or certificate-based.
            object context     = subscription.AzureCredentials;
            var    certContext = context as IAzureAuthenticationCertificateSubscriptionContext;

            if (certContext != null)
            {
                var cert = await certContext.AuthenticationCertificate.GetCertificateFromStoreAsync();

                request.ClientCertificates.Add(cert);
            }
            else
            {
                var accountCountext = context as IAzureUserAccountSubscriptionContext;
                if (accountCountext != null)
                {
                    string authHeader = await accountCountext.GetAuthenticationHeaderAsync(false);

                    request.Headers.Add(HttpRequestHeader.Authorization, authHeader);
                }
                else
                {
                    return(null);
                }
            }

            using (WebResponse response = await request.GetResponseAsync())
                using (Stream stream = response.GetResponseStream()) {
                    // There is no XDocument.LoadAsync, but we want the networked I/O at least to be async, even if parsing is not.
                    Stream xmlData = new MemoryStream();
                    await stream.CopyToAsync(xmlData);

                    xmlData.Position = 0;
                    return(XDocument.Load(xmlData));
                }
        }
        /// <summary>
        /// Retrieves the publish settings file (.pubxml) for the given Azure Website.
        /// </summary>
        /// <returns>XML document with the contents of .pubxml, or <c>null</c> if it could not be retrieved.</returns>
        private async Task<XDocument> GetPublishXml(AzureWebSiteInfo webSiteInfo) {
            // To build the publish settings request URL, we need to know subscription ID, site name, and web region to which it belongs,
            // but we only have subscription ID and the public URL of the site at this point. Use the Azure Website service to look up
            // the site from those two, and retrieve the missing info.

            IVsAzureServices webSiteServices = new VsAzureServicesShim(NodejsPackage.GetGlobalService(_azureServicesType));
            if (webSiteServices == null) {
                return null;
            }

            var webSiteService = webSiteServices.GetAzureWebSitesService();
            if (webSiteService == null) {
                return null;
            }

            var subscriptions = await webSiteService.GetSubscriptionsAsync();
            var subscription = subscriptions.FirstOrDefault(sub => sub.SubscriptionId == webSiteInfo.SubscriptionId);
            if (subscription == null) {
                return null;
            }

            var resources = await subscription.GetResourcesAsync(false);
            var webSite = resources.OfType<IAzureWebSite>().FirstOrDefault(ws => {
                Uri browseUri;
                Uri.TryCreate(ws.BrowseURL, UriKind.Absolute, out browseUri);
                return browseUri != null && browseUri.Equals(webSiteInfo.Uri);
            });
            if (webSite == null) {
                return null;
            }

            // Prepare a web request to get the publish settings.
            // See http://msdn.microsoft.com/en-us/library/windowsazure/dn166996.aspx
            string requestPath = string.Format(
                "{0}/services/WebSpaces/{1}/sites/{2}/publishxml",
                subscription.SubscriptionId,
                webSite.WebSpace,
                webSite.Name);
            Uri requestUri = new Uri(((IAzureSubscription)subscription).ServiceManagementEndpointUri, requestPath);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
            request.Method = "GET";
            request.ContentType = "application/xml";
            request.Headers.Add("x-ms-version", "2010-10-28");

            // Set up authentication for the request, depending on whether the associated subscription context is 
            // account-based or certificate-based.
            object context = subscription.AzureCredentials;
            var certContext = context as IAzureAuthenticationCertificateSubscriptionContext;
            if (certContext != null) {
                var cert = await certContext.AuthenticationCertificate.GetCertificateFromStoreAsync();
                request.ClientCertificates.Add(cert);
            } else {
                var accountCountext = context as IAzureUserAccountSubscriptionContext;
                if (accountCountext != null) {
                    string authHeader = await accountCountext.GetAuthenticationHeaderAsync(false);
                    request.Headers.Add(HttpRequestHeader.Authorization, authHeader);
                } else {
                    return null;
                }
            }

            using (WebResponse response = await request.GetResponseAsync())
            using (Stream stream = response.GetResponseStream()) {
                // There is no XDocument.LoadAsync, but we want the networked I/O at least to be async, even if parsing is not.
                Stream xmlData = new MemoryStream();
                await stream.CopyToAsync(xmlData);
                xmlData.Position = 0;
                return XDocument.Load(xmlData);
            }
        }