Azure - Cannot programmatically perform VIP Swap

2019-07-15 12:40发布

问题:

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+"/"
                      +serviceName);

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

    try
    {
        certStore.Open(OpenFlags.ReadOnly);
    }
    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.");
        }
        else
        {
            throw;
        }
    }

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

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

    X509Certificate2 certificate = certCollection[0];

    request.ClientCertificates.Add(certificate);

    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??

Thanks

回答1:

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.



回答2:

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?



标签: c# azure cloud