我正在尝试在C#中对我在azure云中的托管服务执行交换部署操作。我的代码没有返回错误,但是,从不执行交换。
我的代码基于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=
行。它要求生产名称和部署名称。我以为我只有一个!有人可以澄清我应该在这里放什么吗?
由于
答案 0 :(得分:2)
你发送的身体看起来不对劲。 (它缺少<Production>
和<SourceDeployment>
元素。)此外,您还没有显示您正在使用的网址。 404可能是因为URL错误。 (对于糟糕的请求主体,我希望会有类似400的东西。)
如果您可以共享其余代码,则可能更容易调试。
答案 1 :(得分:0)
根据SWAP部署的文档(http://msdn.microsoft.com/en-us/library/ee460814.aspx),您需要提供请求正文。我在上面的代码中没有看到。您可以尝试提交请求正文并再试一次吗?