How does this one look

On 7/15/06, Ayende Rahien <[EMAIL PROTECTED]> wrote:

I am not sure what the implications of multiply threads attaching/detaching to the event at the same time. I would guess that it would cause locks, but I am not sure.

It is certain that the event add/remove methods will become hotspots in the application.

 

One thing to note is that this:

if (EndRequest != null) EndRequest(sender, e);

 

Is not MT safe. If I unsubscribed from the event between the if and the call, you will get null reference exception.

 

From: [EMAIL PROTECTED] [mailto: [EMAIL PROTECTED]] On Behalf Of Craig Neuwirt
Sent: Saturday, July 15, 2006 3:47 PM


To: [email protected]
Subject: Re: [Castle-users] LifeStyle for an HttpRequest

 

 

On 7/15/06, Ayende Rahien <[EMAIL PROTECTED]> wrote:

That approach is a memory leak, mind you.

The static EndRequest event will hold references to all the life style managers for all the requests.

 

But I am unregistering from the event too.  How will it still hold one to the managers.

 

 

It is preferable to just register it in the HttpContext, something like this:

 

Public void RegisterForDisposale(IDisposable dis)

{

                IList<IDisposable> disposables = HttpCotext.Current.Items["Disposables.List.Items"];

                disposables.Add(dis);

}

 

Public void OnEndRequest(object sender, EventArgs e)

{

                Foreach(IDisposable dis in HttpContext.Current.Items["Disposables.List.Items"])

                {

                                Dis.Dispose();

                }

}

 

 

I'll try out this approach.  It requires fewer registrations.

 

thanks

 

From: [EMAIL PROTECTED] [mailto: [EMAIL PROTECTED]] On Behalf Of Craig Neuwirt
Sent: Saturday, July 15, 2006 3:21 PM


To: [email protected]
Subject: Re: [Castle-users] LifeStyle for an HttpRequest

 

Yup, that is what I ended up doing.  I just created a Module that register for EndRequest events and exposes a

static EndRequest event that the lifestyle manager can register interest in.   It would have been nice if you could

have been able to do

 

HttpContext.ApplicationInstance.EndRequest += Application_EndRequest

 

at any time.

 

thanks,

  craig

 

On 7/15/06, Ayende Rahien <[EMAIL PROTECTED] > wrote:

Create a singleton object that will dispose of objects in the HttpContext.

On the first Resolve(), register the object in the singleton, and have it register for EndReqeust, and dipose all the registered objects in that requrest.

 

From: [EMAIL PROTECTED] [mailto: [EMAIL PROTECTED]] On Behalf Of Craig Neuwirt
Sent: Saturday, July 15, 2006 2:32 PM


To: [email protected]
Subject: Re: [Castle-users] LifeStyle for an HttpRequest

 

Unfortunately, my exact idea does not work.  I actually wanted to attach to the EndRequest event within the PerWebReqyestLifeStyleManager on the SPECIFIC instance related to the current Http context.  This would

have avoided modules or Global.asax needs.  However, the pipeline takes a snapshot of the event handlers

before processing begins to any new registrations are ignored.

 

On 7/15/06, Ayende Rahien < [EMAIL PROTECTED] > wrote:

That would work too.

I have a bias toward HttpModules because they are reusables, that is all.

If you want to go this route, just use Global.asax for this. The Application_EndRequest will be called per request.

 

From: [EMAIL PROTECTED] [mailto: [EMAIL PROTECTED]] On Behalf Of Craig Neuwirt
Sent: Saturday, July 15, 2006 1:50 AM


To: [email protected]
Subject: Re: [Castle-users] LifeStyle for an HttpRequest

 

What if I register for the EndRequest on the HttpContext.Current.ApplicationInstance and then Release the component there.  Wouldn't that event apply only to that specific ApplicationInstance.  I could unregister the event too.

On 7/14/06, Ayende Rahien < [EMAIL PROTECTED] > wrote:

I see, that being the case, I would suggest using the PerRequest lifestyle, and then handling the RequestEnd in a HttpModule.

As well as adding a finalizer, just in case.

 

From: [EMAIL PROTECTED] [mailto: [EMAIL PROTECTED]] On Behalf Of Craig Neuwirt
Sent: Friday, July 14, 2006 11:19 PM


To: [email protected]
Subject: Re: [Castle-users] LifeStyle for an HttpRequest

 

It is a legacy component that holds connections to one or more databases as well as resources that must be released.

On 7/14/06, Ayende Rahien < [EMAIL PROTECTED] > wrote:

Why do you need to release the component?

 

From: [EMAIL PROTECTED] [mailto: [EMAIL PROTECTED]] On Behalf Of Craig Neuwirt
Sent: Friday, July 14, 2006 10:56 PM


To: [email protected]
Subject: Re: [Castle-users] LifeStyle for an HttpRequest

 

Thats kinda what I want to do.  My only concern is how to determine when the logical thread is over so the LifeStyleManager can properly release the component.

On 7/14/06, Ayende Rahien < [EMAIL PROTECTED] > wrote:

Create a hybrid then, which would delegate to the PerRequest or PerThread if the HttpContext.Current exists or not.

 

From: [EMAIL PROTECTED] [mailto: [EMAIL PROTECTED]] On Behalf Of Craig Neuwirt
Sent: Friday, July 14, 2006 6:16 PM

Subject: Re: [Castle-users] LifeStyle for an HttpRequest

 

I agree.  I would like to create a LifestyleManager that functions as a PerWebRequest

in a Web environment and a PerLogicalRequest in a non-web environment.  In affect,

a LogicalCallRequestLifeStyleManager.  I wonder if it is correct to use HttpContext if

available, otherwise use the CallContext.  The true details of the latter still eludes me.

 

On 7/14/06, hammett < [EMAIL PROTECTED] > wrote:

You may add, but I'd say that you do not create a new enum option for
it, as it works only on web environments and we have to protect the
users from themselves :-D

On 7/14/06, Craig Neuwirt < [EMAIL PROTECTED]> wrote:
> Perfect.   Any reason it is not in the Castle core?

--
Cheers,
hammett
http://hammett.castleproject.org/


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
CastleProject-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/castleproject-users

 



-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642


_______________________________________________
CastleProject-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/castleproject-users

 



-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642


_______________________________________________
CastleProject-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/castleproject-users

 



-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642


_______________________________________________
CastleProject-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/castleproject-users

 



-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642


_______________________________________________
CastleProject-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/castleproject-users

 



-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642


_______________________________________________
CastleProject-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/castleproject-users

 



-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642


_______________________________________________
CastleProject-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/castleproject-users

 



-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642


_______________________________________________
CastleProject-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/castleproject-users



using System;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using Castle.MicroKernel;
using Castle.MicroKernel.Lifestyle;

namespace BWater.RBT.Components.IoC
{
        [Serializable]
        public class PerWebRequestLifestyleManager : AbstractLifestyleManager
        {
                private string PerRequestObjectID = 
"PerRequestLifestyleManager_" + Guid.NewGuid().ToString();

                #region ILifestyleManager Members

                public override object Resolve(CreationContext context)
                {
                        HttpContext current = HttpContext.Current;
                        
                        if (current.Items[PerRequestObjectID] == null)
                        {
                                if (!PerWebRequestLifestyleModule.Initialized)
                                {
                                        throw new 
ConfigurationErrorsException("Looks like you forgot to register the http module 
" +
                                                
typeof(PerWebRequestLifestyleModule).FullName +
                                                "\r\nAdd '<add 
name=\"PerRequestLifestyle\" 
type=\"BWater.RBT.Components.IoC.PerWebRequestLifestyleModule, 
BWater.RBT.Components\" />' " +
                        "to the <httpModules> section on your web.config");
                                }

                                object instance = base.Resolve(context);
                                current.Items[PerRequestObjectID] = instance;
                                
PerWebRequestLifestyleModule.RegisterForEviction(this, instance);
                        }

                        return current.Items[PerRequestObjectID];
                }

                public override void Release(object instance)
                {
                        // Do nothing.
                }

                internal void Evict(object instance)
                {
                        base.Release(instance);
                }
                
                public override void Dispose()
                {
                }
                
                #endregion
        }

        #region PerWebRequestLifestyleModule

        /// <summary>
        /// Since registering for <see cref="HttpApplication.EndRequest"/> after
        /// ASP.NET Pipeline has initialized has no affect, this module will let
        /// the EndRequest events propogate to the <see 
cref="PerWebRequestLifestyleManager"/>.
        /// </summary>
        public class PerWebRequestLifestyleModule : IHttpModule
        {
                private static bool initialized;

                private const string PerRequestEvict = 
"PerRequestLifestyleManager_Evict"; 

                public void Init(HttpApplication context)
                {
                        initialized = true;
                        context.EndRequest += new 
EventHandler(Application_EndRequest);
                }

                public void Dispose()
                {

                }

                internal static void 
RegisterForEviction(PerWebRequestLifestyleManager manager, object instance)
                {
                        HttpContext context = HttpContext.Current;

                        IDictionary<PerWebRequestLifestyleManager, object> 
candidates =
                                (IDictionary<PerWebRequestLifestyleManager, 
object>)context.Items[PerRequestEvict];

                        if (candidates == null)
                        {
                                candidates = new 
Dictionary<PerWebRequestLifestyleManager, object>();
                                context.Items[PerRequestEvict] = candidates;
                        }

                        candidates.Add(manager, instance);
                }
                
                protected void Application_EndRequest(Object sender, EventArgs 
e)
                {
                        HttpApplication application = (HttpApplication) sender;
                        IDictionary<PerWebRequestLifestyleManager, object> 
candidates = 
                                (IDictionary<PerWebRequestLifestyleManager, 
object>)
                                application.Context.Items[PerRequestEvict];
                        
                        if (candidates != null)
                        {
                                foreach 
(KeyValuePair<PerWebRequestLifestyleManager, object> candidate in candidates)
                                {
                                        candidate.Key.Evict(candidate.Value);
                                }

                                
application.Context.Items.Remove(PerRequestEvict);
                        }
                }
                
                internal static bool Initialized
                {
                        get { return initialized; }
                }
        }
        
        #endregion
}
-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
CastleProject-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/castleproject-users

Reply via email to