Take the 2-minute tour ×

I am trying to perform a swap deployment operation, in C#, on a hosted service I have in the azure cloud. My code returns no errors, however, the swap is never performed.

My code is based off sample code from the Microsoft website on how to do a list services operation which uses GET, however swap deployment uses POST.

I'm new to this so it's possible I'm doing this entirely the wrong way. Any help is appreciated.

Here's my code so far:

public void swapDeployment()
    string operationName = "hostedservices";

    Uri swapURI = new Uri("https://management.core.windows.net/"
                      + subscriptionId
                      + "/services/"
                      + operationName+"/"

    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(swapURI);

    request.Headers.Add("x-ms-version", "2009-10-01");
    request.Method = "POST";
    request.ContentType = "application/xml";

    String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?> <Swap xmlns=\"http://schemas.microsoft.com/windowsazure\"><Production>HealthMonitor - 21/10/2011 22:36:08</Production><SourceDeployment>SwapTestProject - 13/12/2011 22:23:20</SourceDeployment></Swap>";
    byte[] bytes = Encoding.UTF8.GetBytes(xml);
    request.ContentLength = bytes.Length;

    X509Store certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);

    catch (Exception e)
        if (e is CryptographicException)
            Console.WriteLine("Error: The store is unreadable.");
        else if (e is SecurityException)
            Console.WriteLine("Error: You don't have the required permission.");
        else if (e is ArgumentException)
            Console.WriteLine("Error: Invalid values in the store.");

    X509Certificate2Collection certCollection = certStore.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, false);

    if (0 == certCollection.Count)
        throw new Exception("Error: No certificate found containing thumbprint " + thumbprint);

    X509Certificate2 certificate = certCollection[0];


    using (Stream requestStream = request.GetRequestStream())
        requestStream.Write(bytes, 0, bytes.Length);

    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        if (response.StatusCode != HttpStatusCode.OK)
            string message = String.Format( "POST failed. Received HTTP {0}",response.StatusCode);
            throw new ApplicationException(message);

            // Error shown at: using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            // Saying: The remote server returned an error: (404) Not Found.

EDIT: I think my main problem is the string xml= line. Its asking for the production name and the deployment name. I thought I only had one! Can someone clarify what I should be putting in here??


share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

The body you're sending looks wrong. (It's missing the <Production> and <SourceDeployment> elements.) Also, you haven't shown the URL you're using. The 404 could be because the URL is wrong. (I would expect something like a 400 for the bad request body.)

If you could share the rest of the code, it may be easier to debug.

share|improve this answer
Thanks for the suggestion. I've added the full code of that class now. The main problem I'm having is I simply don't know how to add the body. I've been guessing based off sample code, but haven't found anything concrete yet. –  eoinzy Dec 12 '11 at 19:40
<SourceDeployment> should contain the name of the deployment you want to put into production (i.e., the deployment that's currently in staging). If there's already something in production, that deployment's name should go in the <Production> element. –  smarx Dec 13 '11 at 3:50
OK so I've updated the code with what I currently have, and I am getting a The remote server returned an error: (400) Bad Request. error. Any ideas? Thanks again for the help. –  eoinzy Dec 13 '11 at 23:02
Actualy nevermind, I managed to fix it. I had left out a slash in the URI at the end! Thanks! –  eoinzy Dec 14 '11 at 0:18
add comment

Based on the documentation for SWAP deployment (http://msdn.microsoft.com/en-us/library/ee460814.aspx), you would need to provide the request body. I did not see that in your code above. Can you try putting the request body and try it again?

share|improve this answer
Hi, I've updated my code now. I am still getting the 404 error after adding the body. –  eoinzy Dec 11 '11 at 19:28
add comment

Your Answer


