global asax application_start application begin_request methods? - iis-7

I have a problem. While migrating from classic pipeline mode to integrated pipeline mode at IIS 7.0 we encounter the problem :
Server Error in '/' Application.
Request is not available in this context...
We found solution for this problem at
mvolo.com/blogs/serverside/archive/2007/11/10/Integrated-mode-Request-is-not-available-in-this-context-in-Application_5F00_Start.aspx
As solution shortly ,in global.asax I must forward the application_start event to Application_BeginRequest event.
void Application_Start(object sender, EventArgs e) { // sender has type 'System.Web.HttpApplicationFactory' }
Application_BeginRequest(Object source, EventArgs e) | {
// sender has type 'System.Web.HttpApplication' }
Or another solution is, Application_Start event can start later then Application_BeginRequest .
any suggestions ?
I have no option like choosing "classic mode "

Move the code to Application_BeginRequest or Session_Start. You shouldn't use the Request object in Application_Start anyway.
The Request object contains information that is specific for one page request. It doesn't really make any sense to do anything with this information in the Application_Start event.

So, change you app pool mode to classic.

Related

ASP.NET Web Application Application_Start Not Being Called in IIS

I have a ASP.NET Visual Basics Web Application that runs on IIS 8 that needs to call Application_Start in Global.asax in order to initialize several variables. However, as far as I can tell, the method never gets called, through remote debugging or logging. The Application is run on Classic Mode.
If I run it locally on Visual Studio, I see it work
Application pool with Classic mode doesn’t have an impact on this feature. On my side, the application_Start event works properly.
Please ensure that App_global.asax.dll located in the BIN folder. We could clean, rebuild the project, and deploy it again. Moreover, during the Application_start event, we had better not write the IIS log or write/modify a database, this commonly leads to a permission issue, which seems not working.
Besides, you could verify the result by using the IIS log.
public class Global : System.Web.HttpApplication
{
public static int a = 34;
protected void Application_Start(object sender, EventArgs e)
{
a = a + 45;
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
Response.AppendToLog("Application BeginRequest...."+a.ToString());
}
}
This will generate a line of record in the IIS log.
2020-08-26 05:49:10 ::1 POST /WebForm1.aspx
Application+BeginRequest....79 8001 - vabqia969VM
Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/84.0.4147.135+Safari/537.36
http://localhost:8001/WebForm1.aspx 200 0 0 3
Please refer to the below link for troubleshooting.
Application_start not working

Application_LogRequest event not fired

I have a global.asax. and I have added a subscription to the LogRequest and BeginRequest
void Application_LogRequest(object sender, EventArgs e)
...
void Application_BeginRequest(object sender, EventArgs e)
....
First, I am not certain what is the real purpose of the LogRequest and when it should fire.
I have also done a test application and I have breakpoints in the LogRequest and BeginRequest above. Somehow the LogRequest is never being fired - it never enters in the breakpoint. And the BeinRequest is firing correctly with each request.
I have done some research online but there is very little about the LogRequest event apart from the fact that it should fire with each request ... but in my case it isn't.
I am running from VS but My website is deployed to IIS.
Many Thanks
According to MSDN Library:
The MapRequestHandler, LogRequest, and PostLogRequest events are supported only if the application is running in Integrated mode in IIS 7.0 and with the .NET Framework 3.0 or later.
So perhaps you are running IIS 6.0, or IIS 7.0 (or later) in Classic mode.

Session value set in Session_Start not available in Page_PreInit

I am having an intermittent problem with an ASP.NET 4.0 Web Forms application.
In Session_Start, I store the master page file path in the session:
protected void Session_Start(Object sender, EventArgs e)
{
// Not shown: get master page path from database
Session["MasterPagePath"] = PathIGotFromTheDatabase;
}
Then in my pages' Page_PreInit, I use that session value to set the Page.MasterPageFile
protected void Page_PreInit(object sender, EventArgs e)
{
Page.MasterPageFile = Session["MasterPagePath"] + #"/MyMasterPage.Master";
}
This works 99% of the time, but occasionally something breaks, and Session["MasterPagePath"] is null. The users report that they have to close all of their active browser sessions in order to use the site again.
My understanding is that since I populate the Session["MasterPagePath"] in Session_Start, it should always be available in my pages' PreInit method. If my session had expired, it would always be repopulated by Session_Start before Page_PreInit is called.
Am I missing something here? Under what conditions could what I describe happen? I am using InProc session state for what it's worth.
I don't think Session objects added on Application_Start are available at control level. Application["myObj"] would be available but for all users.
More information about the life cycle here.

Is there any way to get acces and change to Page before leave the server?

I want to change page content while it is going from the server because i need to add some advertisements inside the html elements that are advertisement holder.
protected void Application_PreSendRequestContent(object sender, EventArgs e) this is good but i couldn't get access to HttpContext . Should i, i don't know :)
But in this method:
protected void Application_EndRequest(object sender, EventArgs e) i could get the HttpContext but i couldn't find the server response in it.
How can i do this?
You might want to implement a HttpModule instead of global.asax. You can find an example of a module that manipulates the response in MSDN: Walkthrough: Creating and Registering a Custom HTTP Module
See also this page for some additional information (e.g. why a HttpModule instead of global.asax): HTTP Handlers and HTTP Modules Overview
To answer your comment: here are some reasons why to use a module instead of global.asax (have a look at the document linked above for more information):
You can implement much of the functionality of a module in the application's Global.asax file [...] however, modules have an advantage over the Global.asax file because they are encapsulated and can be created one time and used in many different applications.
In IIS 7.0, the integrated pipeline enables managed modules to subscribe to pipeline notifications for all requests, not just requests for ASP.NET resources.
You can enable/disable a module via web.config (without touching any code)
You should use a module whenever you must create code that depends on application events, and when the following conditions are true:
You want to re-use the module in other applications.
You want to avoid putting complex code in the Global.asax file.
The module applies to all requests in the pipeline (IIS 7.0 Integrated mode only).
protected void
Application_PreSendRequestContent(object
sender, EventArgs e) this is good but
i couldn't get access to HttpContext .
Should i, i don't know :)
You can always get access to the HttpContext for the current request by using HttpContext.Current

Simulating RemoteOnly custom errors using Application_Error ASP.NET

I handle my errors using Application_Error in global.asax.
How to check if the request is from the local client to display more information about the error or just show the yellow error page. Something like "remoteOnly" that ASP does when handling errors using web.config.
After I posted the question, I went back to my application and played around a little bit then I found this. Now I'm using it.
void Application_Error(object sender, EventArgs e)
{
if(!HttpContext.Current.Request.IsLocal)
HttpContext.Current.Server.ClearError();
// process error handling
}

Resources