Search code examples
azure-web-rolesazure-cloud-services

Can I run long running task(synchronous) in OnStart() method in azure cloud service web role?


There is some initialization work (It takes around <= 10 min) which needs to be done before web role starts accepting the requests. SO my question is "Are there any constraints in OnStart() method"? Will the web role restart in between?


Solution

  • Are there any constraints in OnStart() method?

    It seems there is not any constraint in OnStart() method. If the OnStart method returns false, the role instance is immediately stopped. If the method returns true, Windows Azure starts the role by calling the Run method. In general, you should avoid returning false from the OnStart method.

    Will the web role restart in between?

    If an exception occurs within one of the lifecycle methods, Azure will raise the UnhandledException event, and then the process is terminated. After your role has been taken offline, it will be restarted by Azure. So you should avoid UnhandledException in the method.

    For more details, you could refer to the articles.

    1. Customize the Lifecycle of a Web or Worker role in .NET
    2. Role throws unhandled exceptions while initializing or stopping