Partilhar via


RoleEntryPoint.OnStart Method ()

 

Runs code that initializes a role instance.

Namespace:   Microsoft.WindowsAzure.ServiceRuntime
Assembly:  Microsoft.WindowsAzure.ServiceRuntime (in Microsoft.WindowsAzure.ServiceRuntime.dll)

Syntax

public virtual bool OnStart()
public:
virtual bool OnStart()
abstract OnStart : unit -> bool
override OnStart : unit -> bool
Public Overridable Function OnStart As Boolean

Return Value

Type: System.Boolean

true if initialization succeeds; otherwise, false. The default return value is true.

Remarks

Override the OnStart method to run initialization code for your role. The following code example shows how to override the OnStart method:

public class WorkerRole : RoleEntryPoint
{
   public override bool OnStart()
   { 
      try
      {
         // Add initialization code here
      } 
      catch (Exception e)
      {
         Trace.WriteLine("Exception during OnStart: " + e.ToString());
         // Take other action as needed.
      }

      return base.OnStart();
   }
}

Before the OnStart method returns, the status of the role instance is set to Busy and the instance is not available through the load balancer.

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.

A web role can include initialization code in the ASP.NET Application_Start method instead of the OnStart method. The Application_Start method is called after the OnStart method. For more information about the Application_Start method, see ASP.NET Application Life Cycle Overview for IIS 5.0 and 6.0 or ASP.NET Application Life Cycle Overview for IIS 7.0.

See Also

RoleEntryPoint Class
Microsoft.WindowsAzure.ServiceRuntime Namespace

Return to top