What's the difference between the webrole onStart() event and Application_Start() global.asax event?

半世苍凉 提交于 2019-11-29 04:03:57
sharptooth

When there's no class extending RoleEntryPoint the web role will run just fine, just no extra code is run instead of OnStart(), Run() and OnStop().

Application_Start() is completely unrelated to Azure and is completely ignored by Azure runtime, it's just some piece of ASP.NET wiring. You can easily have Application_Start() unconditionally throwing an exception and that won't prevent your web role from getting started, just all HTTP requests will fail.

Bear in mind that starting with SDK 1.3 the default mode is "IIS mode" where the web role payload containing RoleEntryPoint descendant runs in one process (WaIISHost.exe) and ASP.NET code runs in another process. The process with RoleEntryPoint is started by Azure runtime first, it runs OnStart() and enters the infinite loop in Run(), then the instance is opened for HTTP requests. If you use IIS 7.5 and have "autostart" enabled you may have Application_Start() executed earlier, but otherwise you won't have Application_Start() executed until the first request comes.

So the key is that there're two different processes running your code and each has its own lifetime and that dictates limitations on how you can design your application.

The RoleEntryPoint descendant class can have any name, belong to any namespace and be located in any .cs file within the project which is selected as the payload for the web role - that will likely be your ASP.NET project. Under these conditions the RoleEntryPoint descendant will be located by Azure runtime and its methods will be run as part of role instance lifetime.

I may be misunderstanding the question here, so please let me know if so.

You can certainly start with a regular ASP.NET project and later convert it to run in a Windows Azure Web Role. The WebRole.cs isn't technically required - the role should start without it. But, if you want to inject some logic as part of the role instance's startup process, you can add a WebRole class, inheriting from RoleEntryPoint, and override the OnStart(). There you could do things like configure diagnostics, create Windows Azure storage tables or queues, etc.

Generally, I would tend to put configuration/bootstrap logic in the Web Role's OnStart() - more platform specific config/bootstrap.

You can have single entry point class to your role that is inherited from RoleEntryPoint class. Normally workerrole.cs or webrole.cs are inherited from this.

You can give any class name provided it is inherited from RoleEntryPoint.

OnStart event of this class is getting fired when your role is becoming is ready. You can write your initialization logic here before your application starts. It could be initialization of IoC containers, Windows azure diagnostic configuration or anything.

Application_Start event is fired when you hit your website first time. You role is already in ready state.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!