From dcfd6e014a703c16df515fdc70dcc797c3bfccb2 Mon Sep 17 00:00:00 2001 From: Thomas Trageser Date: Sun, 7 Apr 2013 19:42:30 +0100 Subject: [PATCH] SPRNET-1545 Request or Session scoped objects with IDisposable are failing during the 2nd time of the creation --- .../Factory/Support/AbstractObjectFactory.cs | 2 +- .../Factory/Support/WebObjectFactory.cs | 43 +++++++++++++++---- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/Spring/Spring.Core/Objects/Factory/Support/AbstractObjectFactory.cs b/src/Spring/Spring.Core/Objects/Factory/Support/AbstractObjectFactory.cs index 4cd65361..76b7e9b6 100644 --- a/src/Spring/Spring.Core/Objects/Factory/Support/AbstractObjectFactory.cs +++ b/src/Spring/Spring.Core/Objects/Factory/Support/AbstractObjectFactory.cs @@ -2342,7 +2342,7 @@ namespace Spring.Objects.Factory.Support } } - protected void RegisterDisposableObjectIfNecessary(string name, object instance, RootObjectDefinition od) + protected virtual void RegisterDisposableObjectIfNecessary(string name, object instance, RootObjectDefinition od) { if (od.IsSingleton && RequiresDestruction(instance, od)) { diff --git a/src/Spring/Spring.Web/Objects/Factory/Support/WebObjectFactory.cs b/src/Spring/Spring.Web/Objects/Factory/Support/WebObjectFactory.cs index 3176441a..41a72240 100644 --- a/src/Spring/Spring.Web/Objects/Factory/Support/WebObjectFactory.cs +++ b/src/Spring/Spring.Web/Objects/Factory/Support/WebObjectFactory.cs @@ -28,6 +28,7 @@ using System.Web.Caching; using System.Web.SessionState; using Common.Logging; using Spring.Collections; +using Spring.Context.Attributes; using Spring.Context.Support; using Spring.Objects.Factory.Config; using Spring.Util; @@ -266,10 +267,9 @@ namespace Spring.Objects.Factory.Support protected override object CreateAndCacheSingletonInstance( string objectName, RootObjectDefinition objectDefinition, object[] arguments) { - if (IsWebScopedSingleton(objectDefinition) - ) + if (IsWebScopedSingleton(objectDefinition)) { - ObjectScope scope = ((IWebObjectDefinition)objectDefinition).Scope; + ObjectScope scope = GetObjectScope(objectDefinition); if (scope == ObjectScope.Request) { @@ -349,6 +349,22 @@ namespace Spring.Objects.Factory.Support return instance; } + /// + /// We need this override so that Web Scoped Singletons are not registered in general + /// DisposalObjectRegister + /// + /// + /// + /// + protected override void RegisterDisposableObjectIfNecessary(string name, object instance, + RootObjectDefinition objectDefinition) + { + if (!IsWebScopedSingleton(objectDefinition)) + { + base.RegisterDisposableObjectIfNecessary(name, instance, objectDefinition); + } + } + /// /// Add the created, but yet unpopulated singleton to the singleton cache /// to be able to resolve circular references @@ -363,7 +379,7 @@ namespace Spring.Objects.Factory.Support { if (IsWebScopedSingleton(objectDefinition)) { - ObjectScope scope = ((IWebObjectDefinition) objectDefinition).Scope; + ObjectScope scope = GetObjectScope(objectDefinition); if (scope == ObjectScope.Request) { this.Request[objectName] = rawSingletonInstance; @@ -396,7 +412,7 @@ namespace Spring.Objects.Factory.Support { if (IsWebScopedSingleton(objectDefinition)) { - ObjectScope scope = ((IWebObjectDefinition) objectDefinition).Scope; + ObjectScope scope = GetObjectScope(objectDefinition); if (scope == ObjectScope.Request) { this.Request.Remove(objectName); @@ -418,15 +434,26 @@ namespace Spring.Objects.Factory.Support private bool IsWebScopedSingleton(IObjectDefinition objectDefinition) { - if (objectDefinition.IsSingleton - && objectDefinition is IWebObjectDefinition) + if (objectDefinition.IsSingleton && + (objectDefinition is IWebObjectDefinition || objectDefinition is ScannedGenericObjectDefinition)) { - ObjectScope scope = ((IWebObjectDefinition) objectDefinition).Scope; + ObjectScope scope = GetObjectScope(objectDefinition); return (scope == ObjectScope.Request) || (scope == ObjectScope.Session); } return false; } + private ObjectScope GetObjectScope(IObjectDefinition objectDefinition) + { + if (objectDefinition is IWebObjectDefinition) + return ((IWebObjectDefinition) objectDefinition).Scope; + + ObjectScope scope; + Enum.TryParse(objectDefinition.Scope, true, out scope); + + return scope == null ? ObjectScope.Singleton : scope; + } + /// /// Configures object instance by injecting dependencies, satisfying Spring lifecycle /// interfaces and applying object post-processors.