Azure -不能编程执行VIP交换



我正在尝试在azure云中的托管服务上使用c#执行交换部署操作。我的代码没有返回任何错误,但是,永远不会执行交换。

我的代码是基于微软网站上关于如何使用GET进行列表服务操作的示例代码,但是交换部署使用POST。

我是新手,所以有可能我完全用错了方法。如有任何帮助,不胜感激。

下面是目前为止的代码:
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.
编辑:我认为我的主要问题是string xml=线。它询问生产名称和部署名称。我以为我只有一个!有人能澄清我应该在这里放什么吗??

谢谢

你发送的正文看起来不对。(它缺少<Production><SourceDeployment>元素。)此外,您还没有显示您正在使用的URL。404可能是因为URL错误。(我认为错误的请求正文应该是400)

如果您可以共享其余的代码,可能会更容易调试。

根据SWAP部署的文档(http://msdn.microsoft.com/en-us/library/ee460814.aspx),您需要提供请求体。我没有看到你上面的代码。您可以尝试放置请求正文,然后再试一次吗?

最新更新