locked
variable scope - best practice RRS feed

  • Question

  • Hi,

    Warning: Novice questions!

    In my WorkerRole, I get the instance id. Trouble is - I do it every time I run through my code in the Run() method. I'd like to fill a variable in during the OnStart() method and have that visible to the code in my Run() method.

    I'm assuming I do this by declaring the variable at the class level, outside of the method, which makes the variable scoped to both methods then? Is that correct? I've tried it and it works, but I'm wondering if that's best practice?

    Thanks

    Curiouser and curiouser

    Sunday, March 27, 2011 11:00 AM

Answers

  • Hello, it does not matter whether you get this variable in the Run or in the OnStart method. Both methods are begin executed exactly once for each cyle of the Life time of the Azure role. As long as you do not assign your variable in the initine loop (while (true) ) of the Run method, your assignemnt will be done only once. And remember, if your Run() method exits or breaks by some reason, your role will recycle!
    Sunday, March 27, 2011 3:11 PM
  • An immediate difference is that the role state is Busy until OnStart() returns. The idea is that by the time Run() is invoked the instance is willing to accept requests. Obviously, for a trivial initialization it doesn't really matter where it goes. However, it is a good practice to separate the initialization out into OnStart() and Run().

    • Marked as answer by Mog Liang Friday, April 1, 2011 9:30 AM
    Monday, March 28, 2011 4:22 PM
    Answerer

All replies

  • Hello, it does not matter whether you get this variable in the Run or in the OnStart method. Both methods are begin executed exactly once for each cyle of the Life time of the Azure role. As long as you do not assign your variable in the initine loop (while (true) ) of the Run method, your assignemnt will be done only once. And remember, if your Run() method exits or breaks by some reason, your role will recycle!
    Sunday, March 27, 2011 3:11 PM
  • Hi Anton,

    Thanks for pointing that out. I see exactly what you are saying.

    If they both run exactly once - why are there separate methods for OnStart() and Run(). I see a lot of examples with configuration change code in the OnStart() method. If it only runs once when the instance starts, why don't developers put the config change code in to the Run() method? I'm assuming there is some more subtle diffeence between them?

    Thank you Anton

    Curiouser and curiouser

     

    Monday, March 28, 2011 8:26 AM
  • An immediate difference is that the role state is Busy until OnStart() returns. The idea is that by the time Run() is invoked the instance is willing to accept requests. Obviously, for a trivial initialization it doesn't really matter where it goes. However, it is a good practice to separate the initialization out into OnStart() and Run().

    • Marked as answer by Mog Liang Friday, April 1, 2011 9:30 AM
    Monday, March 28, 2011 4:22 PM
    Answerer
  • Aha! That explains it. Makes perfect sense.

    Thanks Neil.

    Best,

    curiouser

    Tuesday, March 29, 2011 7:12 AM