Application_start does not work

I wrote the code in the application_start() method in my global.asax file. It does not get called when I deploy my application to the IIS server. The code is available when I run it in the .NET framework.

I tried to restart the application many times, but it still does not work.

I also tried the suggestion at the following link.

Application_Start not starting?

+4
source share
4 answers

There are a few things you need to know before trying to debug Appplication_Start . There is -

One: when the code is executed and why it cannot be debugged by attaching to it.

The application launch method starts when you start the application pool and launch your website for the first time. If you deploy new results to IIS, IIS can restart it yourself, but there is no guarantee that it will. Thus, the deployment of new codes does not guarantee a restart of the pool and the launch of the application. You must restart the application pool to ensure that the application starts.

When debugging applications, IIS Visual Studio joins a process with the name w3wp.exe or similart (I forgot the actual name of the executable file), which is a workflow and is available only after, remember that your application pool is up and running. Thus, in other words, if you see this in the list of services, then the application has already been launched and joining it will not allow you to debug it. This is a kind of tug of war over time.

Thus, in other words, you can delay the launch of the application if you are not very fast.

Two, solution 1 - with Dev Server

Run the application in the visual studio with the Asp.net or IIS express development server, after which you can debug. But if you really want to debug IIS, check the next section

Two, solution 2 - with IIS

There is a library called System.Diagnostics , Debugger , and it has a good way to call a debugger in code. You can read it here - http://msdn.microsoft.com/en-us/library/system.diagnostics.debugger.break(v=vs.110).aspx

Change the application to start with this -

 public void Application_Start(){ ....... //other codes Debugger.Break() or Debugger.Launch() } 

When this line is executed, IIS will stop execution and show you the debugger selector window (similar to attached) enter image description here , keep your solution open in vs and select it from the list, you can debug as usual ... :)

If you are using Windows 8 and the debugger does not start, read this article to enable it -

http://blogs.msdn.com/b/mapo/archive/2013/11/07/debugger-launch-not-displaying-jit-debugger-selection-popup-on-windows-8-8-1.aspx

Three: a very important thing

I noticed that you said that you are adding db entries to Application_Start. You should keep in mind that Application_Start does not have an HttpContext , ViewContext , so your Db access code can be unsuccessful for many other reasons.

+1
source

This worked for me:

  • Menu β†’ Assembly β†’ Clean Solution
  • Menu β†’ Assembly β†’ Reconstruction

Then Application_Start () was launched only for the first time.

0
source

Make sure that the Global.asax file is actually deployed to the destination folder in the root. If the file is missing, then the code you wrote for Application_Start will never be called.

Also make sure the signature is correct.

 public class Global : System.Web.HttpApplication { protected void Application_Start(object sender, EventArgs e) {/*do something here like logging so you know it was called*/} } 
0
source

If you are using Server 2008R2 (or earlier) and / or IIS 7.5, you may need to study the application initialization module. It can be downloaded here:

www.iis.net/downloads/microsoft/application-initialization

With versions of IIS prior to 8.0, application launch is not called until the first web request arrives. I am reading your question, how do you want your application to quit before the first web request, yes?

Here is a fantastic guide to setting up this module (if applicable to you): http://blogs.msdn.com/b/benjaminperkins/archive/2014/01/07/configure-the-iis-application-initialization-module.aspx

The main outputs are that you need to configure the application pool to AlwaysRunning instead of OnDemand. You also need to set the preloadEnabled flag for your site. After all this is done, start iisreset, and you will see the results of starting your application (see the database, as it says there).

Other answers are also relevant, as it is difficult to debug, and you are missing all the subtleties you are used to, such as httpcontext at the beginning of the application.

If you are using IIS 8.0, you should read the link above to configure preload.

0
source

Source: https://habr.com/ru/post/1208322/


All Articles