Global asax and application_endrequest asp.net - asp.net

When I run my method in global.asax it doesn't run and when I use IHttp module it is working. Please give any advice.
Maybe it is caused of :
context.PostRequestHandlerExecute += new EventHandler(Application_EndRequest);
Is it possible to call it without Module?
Code Example:
Method that I run:
public static void EndSession()
{
HttpContext context = HttpContext.Current;
if (context.Session != null)
{
ISession session = context.Session["Session"] as ISession;
if (context.Session["Session"] != null)
{
if (!session.Transaction.IsActive)
OpenTransaction(session);
session.Flush();
CommitTransaction(session);
session.Close();
context.Session["Session"] = null;
}
}
}
Global:
private void Application_EndRequest(object sender, EventArgs e)
{
NhSessionHelper.EndSession();
}
IHTTPMODULE:
namespace MME.DAL.SesionManager
{
internal class SessionRequest : IHttpModule
{
#region Public Methods
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.PostRequestHandlerExecute += new EventHandler(Application_EndRequest);
}
#endregion
#region Private Methods
private void Application_EndRequest(object sender, EventArgs e)
{
NhSessionHelper.EndSession();
}
#endregion
}
}

Ok I understand now PostRequestHandlerExecute fires page finishes execution so the name of
private void Application_EndRequest(object sender, EventArgs e)
was little confusing and that is why there was a problem.

Related

Xamarin Forms MapRenderer OnMapReady event equivalent for iOS?

I need an equivalent event for an iOS renderer:
public class MyMapRenderer : Xamarin.Forms.Maps.Android.MapRenderer
{
...
protected override void OnMapReady(GoogleMap map)
{
base.OnMapReady(map);
// need to do things here
}
...
}
Thanks
You can use MKMapViewDelegate_MapLoaded to handle your actions in iOS renderer:
public class CustomMapRenderer : MapRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<View> e)
{
base.OnElementChanged(e);
var nativeMap = Control as MKMapView;
nativeMap.MapLoaded += NativeMap_MapLoaded;
}
private void NativeMap_MapLoaded(object sender, EventArgs e)
{
Console.WriteLine("NativeMap_MapLoaded");
}
}

Exception logging HttpModule doesn't catch errors from ASP .NET Page Method

We have an HttpModule that is designed to catch exceptions and log them to the db. It looks something like this:
public class ExceptionLoggingModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.Error += OnError;
}
private static void OnError(object sender, EventArgs e)
{
try
{
var context = (HttpApplication) sender;
var exception = context.Server.GetLastError();
if (exception != null)
{
// Log exception
}
}
catch(Exception)
{
}
}
}
This works in general, but I've just noticed that the OnError method never fires when an error occurs within Page Methods (i.e. methods in a code behind file marked with the WebMethod attribute).
How come?
Is there something I can do about this, other than reimplementing the exception logging inside the Page Method itself?
I found a solution that worked for me here:
http://blogs.microsoft.co.il/blogs/oshvartz/archive/2008/05/17/asp-net-error-handling-using-httpmodule-full-and-partial-post-back-ajax-updatepanel.aspx
Here's the key points of the handler I've written:
public class PathfinderModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.PostMapRequestHandler += this.OnPostMapRequestHandler;
context.Error += OnError;
}
private void OnPostMapRequestHandler(object sender, EventArgs e)
{
Page aux = HttpContext.Current.Handler as Page;
if (aux != null)
{
aux.Error += this.OnPageError;
}
}
private static void OnError(object sender, EventArgs e)
{
// Blah..
}
private void OnPageError(object sender, EventArgs e)
{
// Blah...
}
}

How can I set a value to a label through a Thread?

This code is giving an error 'Object reference not set to an instance of an object'.
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
_Default obj = new _Default();
Thread thread = new Thread(new ThreadStart(obj.ThreadStart));
thread.Start();
}
public void ThreadStart()
{
show();
}
private void show()
{
lblget.Text = "hi";
}
No need to create an instance of _Default.
protected void Page_Load(object sender, EventArgs e)
{
Thread thread = new Thread(show);
thread.Start();
}
private void show()
{
lblget.Text = "hi";
}
Read these article - Use Threads and Build Asynchronous Handlers in Your Server-Side Web Code and PageAsyncTask.

How to programmatically remove an HttpModule from the ASP.NET pipeline?

We can programatically add HttpModules using DynamicModuleUtility.RegisterModule(typeof (SomeHttpModule)) - is there a way to remove them?
Instantiate the HTTP Module in Init method in Global.asax
Call Module.Init() as described here
In Init method of your module, hook required eventhandlers.
Override the Dispose method in the handler and unhook the
eventhandlers.
Expose the instance as public property on global.asax so that you
can call Dispose when you want to unregister the module
// Global.asax
public IHttpModule MyModuleInstance { get; private set; }
public override void Init()
{
base.Init();
MyModuleInstance = new MyModule();
MyModuleInstance.Init(this);
}
// MyModule.cs
public void Dispose()
{
_context.BeginRequest -= context_BeginRequest;
}
public void Init(HttpApplication context)
{
_context = context;
context.BeginRequest += context_BeginRequest;
}
private void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = sender as HttpApplication;
app.Context.Response.Write("Hello from OnBeginRequest in custom module.<br>");
}
// TO unregister
protected void Button1_Click(object sender, EventArgs e)
{
this.ApplicationInstance.MyModuleInstance.Dispose();
}

When Is HttpContext.User initialized?

When is the earliest point in which I can access HttpContext.User?
You could use the AuthenticateRequest event of the HttpApplication. Here is some sample code:
public class MyHttpModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.AuthenticateRequest += context_AuthenticateRequest;
}
void context_AuthenticateRequest(object sender, EventArgs e)
{
var application = (HttpApplication) sender;
var name = application.Context.User.Identity.Name;
}
public void Dispose()
{
}
}

Resources