Is there a Programmatic/API way to Provision a Service Bus Namespace ?

    General discussion

  • Yes - There is a Rest API way of Creating Service Bus Namespaces - (Resource Provider API Reference - Service Bus)

    Code Snippet to Create Namespace using Rest and return the Http Status code (Success - Ok):

            /// <param name="subscriptionId">Get one from </param>
            /// <param name="managementCertificate">Add a management certificate to the Windows Azure Subscription </param>
            /// <param name="namespaceName">ServiceBus Namespace name to be Created </param>
            /// <param name="region">Region in which the SB Namespace needs to be Created Ex: West US</param>
            public static string CreateServiceBusNamespace(
                string subscriptionId,
                X509Certificate2 managementCertificate,
                string namespaceName,
                string region)
                //build the complete request URI
                string requestURI = string.Format("{0}/services/ServiceBus/Namespaces/{1}", subscriptionId, namespaceName);
                HttpWebRequest request = WebRequest.Create(requestURI) as HttpWebRequest;
                //add Subscription management Certificate to the request
                //create the request headers and specify the method required for this type of operation
                request.Headers.Add("x-ms-version", "2012-03-01");
                request.ContentType = "application/atom+xml";
                request.Method = "PUT";
                request.KeepAlive = true;
                // Serialize NamespaceDescription, if additional properties needs to be specified
                string requestBody = string.Format("<entry xmlns=''>  <content type='application/xml'><NamespaceDescription xmlns:i=\"\" xmlns=\"\"> <Region>{0}</Region></NamespaceDescription>  </content></entry>", region);
                byte[] byteArray = Encoding.UTF8.GetBytes(requestBody);
                request.ContentLength = byteArray.Length;
                //write the data to the stream that holds the request body content
                using (Stream requestStream = request.GetRequestStream())
                    requestStream.Write(byteArray, 0, byteArray.Length);
                // Response contains the Namespace Description if the SB Namespace creation is Successfull
                string responseStatus = HttpStatusCode.Unused.ToString();
                    using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                        responseStatus = response.StatusCode.ToString();
                catch (WebException webException)
                    using (HttpWebResponse exceptionResponse = webException.Response as HttpWebResponse)
                        // Log that the Exception is swallowed and reason why 
                        responseStatus = exceptionResponse.StatusCode.ToString();
                return responseStatus;



    Wednesday, January 16, 2013 8:00 PM

All replies