Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I unsubscribe to this .NET event?

I wish to programatically unsubscribe to an event, which as been wired up.

I wish to know how I can unsubscribe to the EndRequest event.

I'm not to sure how to do this, considering i'm using inline code. (is that the correct technical term?)

I know i can use the some.Event -= MethodName to unsubscribe .. but I don't have a method name, here.

The reason I'm using the inline code is because I wish to reference a variable defined outside of the event (which I required .. but feels smelly... i feel like I need to pass it in).

Any suggestions?

Code time..

public void Init(HttpApplication httpApplication)
{
    httpApplication.EndRequest += (sender, e) =>
    {
        if (some logic) 
            HandleCustomErrors(httpApplication, sender, e,
                              (HttpStatusCode)httpApplication.Response.StatusCode);
    };

    httpApplication.Error += (sender, e) => 
            HandleCustomErrors(httpApplication, sender, e);
}

private static void HandleCustomErrors(HttpApplication httpApplication, 
                                       object sender, EventArgs e, 
                                       HttpStatusCode httpStatusCode =
                                           HttpStatusCode.InternalServerError)
{ ... }

This is just some sample code I have, for me to handle errors in a ASP.NET application.

NOTE: Please don't turn this into a discussion about ASP.NET error handling. I'm just playing around with events and using these events for some sample R&D / learning.

like image 789
Pure.Krome Avatar asked Jun 19 '12 01:06

Pure.Krome


People also ask

How to unsubscribe from event in c#?

You cannot easily unsubscribe from an event if you used an anonymous function to subscribe to it. To unsubscribe in this scenario, go back to the code where you subscribe to the event, store the anonymous function in a delegate variable, and then add the delegate to the event.

Do I need to unsubscribe event c#?

To prevent your event handler from being invoked when the event is raised, simply unsubscribe from the event. In order to prevent resource leaks, it is important to unsubscribe from events before you dispose of a subscriber object.

How do I unsubscribe from an event listener?

Event listeners can also be removed by passing an AbortSignal to an addEventListener() and then later calling abort() on the controller owning the signal.

What are events in C# with example?

Events are user actions such as key press, clicks, mouse movements, etc., or some occurrence such as system generated notifications. Applications need to respond to events when they occur. For example, interrupts.


1 Answers

It's not possible to unsubscribe that anonymous delegate. You would need to store it in a variable and unsubscribe it later:

EndRequestEventHandler handler = (sender, e) =>
{
    if (some logic) 
        HandleCustomErrors(httpApplication, sender, e,
                          (HttpStatusCode)httpApplication.Response.StatusCode);
};

httpApplication.EndRequest += handler;
// do stuff
httpApplication.EndRequest -= handler;
like image 114
Marlon Avatar answered Sep 29 '22 23:09

Marlon