【问题标题】:Azure - Cannot programmatically perform VIP SwapAzure - 无法以编程方式执行 VIP 交换
【发布时间】:2012-12-12 05:38:31
【问题描述】:

我正在尝试使用 C# 对我在天蓝色云中的托管服务执行交换部署操作。我的代码没有返回错误,但是从未执行过交换。

我的代码基于 Microsoft 网站上关于如何使用 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= 行。它要求生产名称和部署名称。我以为我只有一个!有人能澄清一下我应该在这里放什么吗??

谢谢

【问题讨论】:

    标签: c# azure cloud


    【解决方案1】:

    您发送的正文看起来不对。 (它缺少&lt;Production&gt;&lt;SourceDeployment&gt; 元素。)此外,您还没有显示您正在使用的URL。 404 可能是因为 URL 错误。 (对于错误的请求正文,我预计会有 400 之类的结果。)

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

    【讨论】:

    • 感谢您的建议。我现在已经添加了该类的完整代码。我遇到的主要问题是我根本不知道如何添加正文。我一直在根据示例代码猜测,但还没有找到任何具体的东西。
    • 应包含您要投入生产的部署的名称(即当前处于暂存阶段的部署)。如果生产中已经有一些东西,那么该部署的名称应该放在 元素中。
    • 好的,所以我已经用我目前拥有的代码更新了代码,我收到了 The remote server returned an error: (400) Bad Request. 错误。有任何想法吗?再次感谢您的帮助。
    • 其实没关系,我设法修复它。最后我在 URI 中遗漏了一个斜杠!谢谢!
    【解决方案2】:

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

    【讨论】:

    • 嗨,我现在更新了我的代码。添加正文后,我仍然收到404 error
    猜你喜欢
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多