code to power on and power off an instance in windows azure

513 views Asked by At

Hi I am a beginner in windows azure. I want to create an instance in windows azure and later power it on and power it off programatically with C# code. I have written code to perform these operations in hyper V server using WMI but I do not understand how to do it using the windows azure API. Please help me to do this. If possible please give me the code so that I can follow it easily.

1

There are 1 answers

0
David Makogon On BEST ANSWER

The answer is: You can sort-of do what you're asking. Each role in your deployment defines a particular VM (Windows 2008 Server with whatever code you write that gets deployed to it), and each role must have one or more instances. So: As long as you're ok scaling from 1 to n instances (but not 0), you can sort-of make this work.

Why sort-of? When you power down instances (e.g. go from 4 instances to 3), you cannot selectively choose which instance gets powered down. Therefore, a good pattern is to work with stateless operations. Let's say you're reading from a queue and doing some processing. In the event one of your role instances gets shut down, then you can simply stop processing queue messages, finish whatever message(s) are being processed (within a few minutes' time), then the instance shuts down and other instances continue processing.

Ok, now for the last part: To change instance count (assuming you're ok with what's described so far), you'd need to change the deployment configuration, which holds settings for number of instances for each role. To do this with C#, you'd need to call the Service Management API, which is REST-based and fully documented here. Having said that: There are Windows Azure PowerShell cmdlets that greatly simplify this, and can be called from any of your roles (or from an on-premises machine). If you really want to make these calls with C#, check out the csmanage example, written in C#.