From c2b4c2abde3a8d3a96bde1717831f89647564736 Mon Sep 17 00:00:00 2001 From: cneuwirt Date: Wed, 26 Sep 2007 14:49:18 +0000 Subject: [PATCH] Fixed IOC-96 "FactorySupport fails to create components if the factory instance is a proxy". The activator checks if it is a proxy if the factory method cannot be found. git-svn-id: https://svn.castleproject.org/svn/castle/trunk@4351 73e77b4c-caa6-f847-a29a-24ab75ae54b6 --- .../Facilities/FactorySupport/FactoryActivator.cs | 43 ++++++++++++++-------- .../Castle.MicroKernel/Proxy/ProxyUtil.cs | 17 +++++++++ .../Castle.Windsor.Tests-vs2005.csproj | 1 + 3 files changed, 46 insertions(+), 15 deletions(-) diff --git a/InversionOfControl/Castle.MicroKernel/Facilities/FactorySupport/FactoryActivator.cs b/InversionOfControl/Castle.MicroKernel/Facilities/FactorySupport/FactoryActivator.cs index 402ffe5dd..9ed87687c 100644 --- a/InversionOfControl/Castle.MicroKernel/Facilities/FactorySupport/FactoryActivator.cs +++ b/InversionOfControl/Castle.MicroKernel/Facilities/FactorySupport/FactoryActivator.cs @@ -21,6 +21,7 @@ namespace Castle.Facilities.FactorySupport using Castle.MicroKernel; using Castle.MicroKernel.ComponentActivator; using Castle.MicroKernel.Facilities; + using Castle.MicroKernel.Proxy; using Castle.MicroKernel.SubSystems.Conversion; /// @@ -57,28 +58,40 @@ namespace Castle.Facilities.FactorySupport factoryType.GetMethod(factoryCreate, BindingFlags.Public | BindingFlags.Static); - MethodInfo instanceCreateMethod = - factoryType.GetMethod(factoryCreate, - BindingFlags.Public | BindingFlags.Instance); - if (staticCreateMethod != null) { return Create(null, factoryId, staticCreateMethod, factoryCreate, context); } - else if (instanceCreateMethod != null) + else { object factoryInstance = Kernel[factoryId]; - return Create(factoryInstance, factoryId, instanceCreateMethod, factoryCreate, context); - } - else - { - String message = String.Format("You have specified a factory " + - "('{2}' - method to be called: {3}) " + - "for the component '{0}' {1} but we couldn't find the creation method" + - "(neither instance or static method with the name '{3}')", - Model.Name, Model.Implementation.FullName, factoryId, factoryCreate); - throw new FacilityException(message); + MethodInfo instanceCreateMethod = + factoryInstance.GetType().GetMethod(factoryCreate, + BindingFlags.Public | BindingFlags.Instance); + + if (instanceCreateMethod == null) + { + factoryInstance = ProxyUtil.GetUnproxiedInstance(factoryInstance); + + instanceCreateMethod = + factoryInstance.GetType().GetMethod(factoryCreate, + BindingFlags.Public | BindingFlags.Instance); + } + + if (instanceCreateMethod != null) + { + return Create(factoryInstance, factoryId, instanceCreateMethod, factoryCreate, context); + } + else + { + String message = String.Format("You have specified a factory " + + "('{2}' - method to be called: {3}) " + + "for the component '{0}' {1} but we couldn't find the creation method" + + "(neither instance or static method with the name '{3}')", + Model.Name, Model.Implementation.FullName, factoryId, factoryCreate); + throw new FacilityException(message); + } } } diff --git a/InversionOfControl/Castle.MicroKernel/Proxy/ProxyUtil.cs b/InversionOfControl/Castle.MicroKernel/Proxy/ProxyUtil.cs index 8d5a89738..8f56c9ef8 100644 --- a/InversionOfControl/Castle.MicroKernel/Proxy/ProxyUtil.cs +++ b/InversionOfControl/Castle.MicroKernel/Proxy/ProxyUtil.cs @@ -14,7 +14,9 @@ namespace Castle.MicroKernel.Proxy { + using System.Runtime.Remoting; using Castle.Core; + using Castle.Core.Interceptor; /// /// Helper support for proxy configuration. @@ -39,5 +41,20 @@ namespace Castle.MicroKernel.Proxy return options; } + + public static object GetUnproxiedInstance(object instance) + { + if (!RemotingServices.IsTransparentProxy(instance)) + { + IProxyTargetAccessor accessor = instance as IProxyTargetAccessor; + + if (accessor != null) + { + instance = accessor.DynProxyGetTarget(); + } + } + + return instance; + } } } \ No newline at end of file diff --git a/InversionOfControl/Castle.Windsor.Tests/Castle.Windsor.Tests-vs2005.csproj b/InversionOfControl/Castle.Windsor.Tests/Castle.Windsor.Tests-vs2005.csproj index e1316606d..002b61afd 100644 --- a/InversionOfControl/Castle.Windsor.Tests/Castle.Windsor.Tests-vs2005.csproj +++ b/InversionOfControl/Castle.Windsor.Tests/Castle.Windsor.Tests-vs2005.csproj @@ -221,6 +221,7 @@ Code + -- 2.11.4.GIT