Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Worker Role - Using OnStart() vs. Constructor for Once Only Initialization

Reading through various documentation and blogs, it sounds like OnStart is the place to initialize your objects and things you need before your role starts receiving traffic. It's not clear to me however if there is a case where OnStart can be called more than once using the same instance of the worker role class?

For instance I have an injection container and a database connection that should be created only once. I need to ensure that the role can't be stopped and started again leaving all the current objects in memory. In that case, it would seem better to use the worker role constructor to initialize the objects.

like image 393
KingOfHypocrites Avatar asked Jun 27 '15 23:06

KingOfHypocrites


1 Answers

OnStart() (a member of the RoleEntryPoint class, and a method you need to override) is only called once in the role instance lifecycle. Return true and then Run() is called. Return false and the role instance is recycled (restarted).

Remember that OnStart() is called before the role instance is added to the load balancer. This gives you the opportunity to initialize things before traffic starts going to it.

FYI Here's more info about role lifecycle.

like image 128
David Makogon Avatar answered Sep 28 '22 14:09

David Makogon