none
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 - http://msdn.microsoft.com/en-us/library/windowsazure/jj856303.aspx (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 https://Windows.Azure.com </param>
            /// <param name="managementCertificate">Add a management certificate to the Windows Azure Subscription http://msdn.microsoft.com/en-us/library/gg551726.aspx </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("https://management.core.windows.net/{0}/services/ServiceBus/Namespaces/{1}", subscriptionId, namespaceName);
                HttpWebRequest request = WebRequest.Create(requestURI) as HttpWebRequest;
    
                //add Subscription management Certificate to the request
                request.ClientCertificates.Add(managementCertificate);
    
                //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 http://msdn.microsoft.com/en-us/library/jj873988.aspx
                string requestBody = string.Format("<entry xmlns='http://www.w3.org/2005/Atom'>  <content type='application/xml'><NamespaceDescription xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.microsoft.com/netservices/2010/10/servicebus/connect\"> <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();
                try
                {
                    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;
            }

    HTH!

    SreeramG





    Wednesday, January 16, 2013 8:00 PM

All replies