Пример #1
0
        /// <summary>
        /// Gets the connection string with the given name.
        /// </summary>
        /// <param name="namespaceName">The namespace name</param>
        /// <param name="keyName">The connection string key name</param>
        /// <returns>The connection string value</returns>
        public virtual string GetConnectionString(string namespaceName, string keyName)
        {
            List <ServiceBusConnectionDetail> connectionStrings = GetConnectionString(namespaceName);
            ServiceBusConnectionDetail        connectionString  = connectionStrings.Find(c => c.KeyName.Equals(
                                                                                             keyName,
                                                                                             StringComparison.OrdinalIgnoreCase));

            return(connectionString.ConnectionString);
        }
        /// <summary>
        /// Lists the notification hubs associated with a namespace.
        /// </summary>
        /// <param name='namespaceName'>
        /// Required. The namespace name.
        /// </param>
        /// <param name='notificationHubName'>
        /// Required. The notification hub name.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// The set of connection details for a service bus entity.
        /// </returns>
        public async System.Threading.Tasks.Task <Microsoft.WindowsAzure.Management.ServiceBus.Models.ServiceBusConnectionDetailsResponse> GetConnectionDetailsAsync(string namespaceName, string notificationHubName, CancellationToken cancellationToken)
        {
            // Validate
            if (namespaceName == null)
            {
                throw new ArgumentNullException("namespaceName");
            }
            if (notificationHubName == null)
            {
                throw new ArgumentNullException("notificationHubName");
            }

            // 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("namespaceName", namespaceName);
                tracingParameters.Add("notificationHubName", notificationHubName);
                Tracing.Enter(invocationId, this, "GetConnectionDetailsAsync", tracingParameters);
            }

            // Construct URL
            string url     = "/" + (this.Client.Credentials.SubscriptionId != null ? this.Client.Credentials.SubscriptionId.Trim() : "") + "/services/servicebus/namespaces/" + namespaceName.Trim() + "/NotificationHubs/" + notificationHubName.Trim() + "/ConnectionDetails";
            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", "2013-08-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
                    ServiceBusConnectionDetailsResponse result = null;
                    // Deserialize Response
                    cancellationToken.ThrowIfCancellationRequested();
                    string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

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

                    XElement feedElement = responseDoc.Element(XName.Get("feed", "http://www.w3.org/2005/Atom"));
                    if (feedElement != null)
                    {
                        if (feedElement != null)
                        {
                            foreach (XElement entriesElement in feedElement.Elements(XName.Get("entry", "http://www.w3.org/2005/Atom")))
                            {
                                ServiceBusConnectionDetail entryInstance = new ServiceBusConnectionDetail();
                                result.ConnectionDetails.Add(entryInstance);

                                XElement contentElement = entriesElement.Element(XName.Get("content", "http://www.w3.org/2005/Atom"));
                                if (contentElement != null)
                                {
                                    XElement connectionDetailElement = contentElement.Element(XName.Get("ConnectionDetail", "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect"));
                                    if (connectionDetailElement != null)
                                    {
                                        XElement keyNameElement = connectionDetailElement.Element(XName.Get("KeyName", "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect"));
                                        if (keyNameElement != null)
                                        {
                                            string keyNameInstance = keyNameElement.Value;
                                            entryInstance.KeyName = keyNameInstance;
                                        }

                                        XElement connectionStringElement = connectionDetailElement.Element(XName.Get("ConnectionString", "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect"));
                                        if (connectionStringElement != null)
                                        {
                                            string connectionStringInstance = connectionStringElement.Value;
                                            entryInstance.ConnectionString = connectionStringInstance;
                                        }

                                        XElement authorizationTypeElement = connectionDetailElement.Element(XName.Get("AuthorizationType", "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect"));
                                        if (authorizationTypeElement != null)
                                        {
                                            string authorizationTypeInstance = authorizationTypeElement.Value;
                                            entryInstance.AuthorizationType = authorizationTypeInstance;
                                        }

                                        XElement rightsSequenceElement = connectionDetailElement.Element(XName.Get("Rights", "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect"));
                                        if (rightsSequenceElement != null)
                                        {
                                            foreach (XElement rightsElement in rightsSequenceElement.Elements(XName.Get("AccessRights", "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect")))
                                            {
                                                entryInstance.Rights.Add(((AccessRight)Enum.Parse(typeof(AccessRight), rightsElement.Value, true)));
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }

                    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();
                }
            }
        }