Tuesday, November 8, 2011

Windows Azure Service Management API to delete deployment from Azure Hosted Service using c#

Continuing with same let’s have a look at deleting deployment from windows azure using service management API via c#.
The configuration and other related settings will be same as stated in previous post. Code for deleting deployment from windows azure hosted service is as follows –
X509Certificate2 cert = new X509Certificate2(Server.MapPath(CertificatePath), CertificatePassword);
var serviceManagment = ServiceManagementHelper.CreateServiceManagementChannel(EndpointConfigurationName, cert);

HostedService hostedService = serviceManagment.GetHostedServiceWithDetails(SubscriptionId, lblHostedServiceName.Text, true);

foreach (Deployment deployment in hostedService.Deployments)
{
UpdateDeploymentStatusInput u = new UpdateDeploymentStatusInput();
u.Status = "Suspended";
serviceManagment.UpdateDeploymentStatus(SubscriptionId, hostedService.ServiceName, deployment.Name, u);
while (deploymentStatus != "Suspended")
{
Thread.Sleep(20000);
HostedService tempHostedService = serviceManagment.GetHostedServiceWithDetails(SubscriptionId, lblHostedServiceName.Text, true);
//retrieve current deployment to know its latest status
Deployment tempDeployment = tempHostedService.Deployments.Find(delegate(Deployment d) { return d.Name == deployment.Name; });
deploymentStatus = tempDeployment.Status;
}
                        serviceManagment.DeleteDeployment(SubscriptionId, hostedService.ServiceName, deployment.Name);
}

Hope this helps.
Cheers…
Happy Coding!!!

No comments:

Post a Comment