From a997f3204400741deb752ada2b7bc6a7fbc06aa4 Mon Sep 17 00:00:00 2001 From: eeichinger Date: Sun, 31 Oct 2010 02:51:35 +0000 Subject: [PATCH] fixed SPRNET-1379, Calculator EnterpriseService example --- .../Spring.Calculator.ClientApp/App.config | 4 +- .../App.config | 10 +- ...g.Calculator.EnterpriseServices.dll.config | 18 +++ ....EnterpriseServices.dll.spring-context.xml | 6 - ...g.Calculator.EnterpriseServices.exe.config | 25 ++++ ...ator.RegisterComponentServices.2008.csproj | 10 +- .../ServicedComponentContextHandler.cs | 20 ++- .../ServicedComponentFactory.cs | 19 ++- .../ServicedComponentHelper.cs | 121 +++++++++++------- 9 files changed, 162 insertions(+), 71 deletions(-) create mode 100644 examples/Spring/Spring.Calculator/src/Spring.Calculator.RegisterComponentServices/Spring.Calculator.EnterpriseServices.dll.config delete mode 100644 examples/Spring/Spring.Calculator/src/Spring.Calculator.RegisterComponentServices/Spring.Calculator.EnterpriseServices.dll.spring-context.xml create mode 100644 examples/Spring/Spring.Calculator/src/Spring.Calculator.RegisterComponentServices/Spring.Calculator.EnterpriseServices.exe.config diff --git a/examples/Spring/Spring.Calculator/src/Spring.Calculator.ClientApp/App.config b/examples/Spring/Spring.Calculator/src/Spring.Calculator.ClientApp/App.config index 0df7c4ba..db44fa9c 100644 --- a/examples/Spring/Spring.Calculator/src/Spring.Calculator.ClientApp/App.config +++ b/examples/Spring/Spring.Calculator/src/Spring.Calculator.ClientApp/App.config @@ -34,7 +34,7 @@ - + @@ -64,7 +64,7 @@ - + diff --git a/examples/Spring/Spring.Calculator/src/Spring.Calculator.RegisterComponentServices/App.config b/examples/Spring/Spring.Calculator/src/Spring.Calculator.RegisterComponentServices/App.config index 74bf8177..47949c8c 100644 --- a/examples/Spring/Spring.Calculator/src/Spring.Calculator.RegisterComponentServices/App.config +++ b/examples/Spring/Spring.Calculator/src/Spring.Calculator.RegisterComponentServices/App.config @@ -56,17 +56,19 @@ - + - + + diff --git a/examples/Spring/Spring.Calculator/src/Spring.Calculator.RegisterComponentServices/Spring.Calculator.EnterpriseServices.dll.config b/examples/Spring/Spring.Calculator/src/Spring.Calculator.RegisterComponentServices/Spring.Calculator.EnterpriseServices.dll.config new file mode 100644 index 00000000..b1adb62c --- /dev/null +++ b/examples/Spring/Spring.Calculator/src/Spring.Calculator.RegisterComponentServices/Spring.Calculator.EnterpriseServices.dll.config @@ -0,0 +1,18 @@ + + + + +
+ + + + + + + + + + diff --git a/examples/Spring/Spring.Calculator/src/Spring.Calculator.RegisterComponentServices/Spring.Calculator.EnterpriseServices.dll.spring-context.xml b/examples/Spring/Spring.Calculator/src/Spring.Calculator.RegisterComponentServices/Spring.Calculator.EnterpriseServices.dll.spring-context.xml deleted file mode 100644 index 0fe6490e..00000000 --- a/examples/Spring/Spring.Calculator/src/Spring.Calculator.RegisterComponentServices/Spring.Calculator.EnterpriseServices.dll.spring-context.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/examples/Spring/Spring.Calculator/src/Spring.Calculator.RegisterComponentServices/Spring.Calculator.EnterpriseServices.exe.config b/examples/Spring/Spring.Calculator/src/Spring.Calculator.RegisterComponentServices/Spring.Calculator.EnterpriseServices.exe.config new file mode 100644 index 00000000..c4aebdcc --- /dev/null +++ b/examples/Spring/Spring.Calculator/src/Spring.Calculator.RegisterComponentServices/Spring.Calculator.EnterpriseServices.exe.config @@ -0,0 +1,25 @@ + + + + +
+ + +
+ + + + + + + + + + + + + + + + + diff --git a/examples/Spring/Spring.Calculator/src/Spring.Calculator.RegisterComponentServices/Spring.Calculator.RegisterComponentServices.2008.csproj b/examples/Spring/Spring.Calculator/src/Spring.Calculator.RegisterComponentServices/Spring.Calculator.RegisterComponentServices.2008.csproj index 4d35c73e..7d49cd3c 100644 --- a/examples/Spring/Spring.Calculator/src/Spring.Calculator.RegisterComponentServices/Spring.Calculator.RegisterComponentServices.2008.csproj +++ b/examples/Spring/Spring.Calculator/src/Spring.Calculator.RegisterComponentServices/Spring.Calculator.RegisterComponentServices.2008.csproj @@ -77,6 +77,13 @@ + + Always + + + Always + + Code @@ -112,9 +119,6 @@ Always - - Always - diff --git a/src/Spring/Spring.Services/EnterpriseServices/ServicedComponentContextHandler.cs b/src/Spring/Spring.Services/EnterpriseServices/ServicedComponentContextHandler.cs index 0f2fa591..89b37fa1 100644 --- a/src/Spring/Spring.Services/EnterpriseServices/ServicedComponentContextHandler.cs +++ b/src/Spring/Spring.Services/EnterpriseServices/ServicedComponentContextHandler.cs @@ -22,6 +22,7 @@ #region Imports +using System; using System.EnterpriseServices; using Spring.Context.Support; @@ -30,12 +31,25 @@ using Spring.Context.Support; namespace Spring.EnterpriseServices { /// - /// Handles loading of <spring/context> configuration sections for s - /// generated by . + /// Handles loading of <spring/context> configuration sections for + /// in-process s generated by + /// . /// /// Erich Eichinger + [Obsolete("not used anymore")] public class ServicedComponentContextHandler : ContextHandler - {} + { + /// + /// Prevent auto-registering the context with the global ContextRegistry + /// + protected override bool AutoRegisterWithContextRegistry + { + get + { + return false; + } + } + } } #endif \ No newline at end of file diff --git a/src/Spring/Spring.Services/EnterpriseServices/ServicedComponentFactory.cs b/src/Spring/Spring.Services/EnterpriseServices/ServicedComponentFactory.cs index 30240b85..3e2f5084 100644 --- a/src/Spring/Spring.Services/EnterpriseServices/ServicedComponentFactory.cs +++ b/src/Spring/Spring.Services/EnterpriseServices/ServicedComponentFactory.cs @@ -22,8 +22,9 @@ #region Imports -using System; - +using System; +using System.Diagnostics; +using System.Reflection; using Spring.Util; using Spring.Objects.Factory; using Spring.Objects.Factory.Support; @@ -151,7 +152,8 @@ namespace Spring.EnterpriseServices public void AfterPropertiesSet() { ValidateConfiguration(); - componentType = Type.GetTypeFromProgID(Name, Server); + componentType = Type.GetTypeFromProgID(Name, Server); + AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve); } #endregion @@ -172,9 +174,14 @@ namespace Spring.EnterpriseServices /// New instance of serviced component. private object CreateInstance() { - return Activator.CreateInstance(componentType); - } - + return Activator.CreateInstance(componentType); + } + + private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) + { + return Assembly.LoadFrom(componentType.Assembly.CodeBase); + } + #endregion } } diff --git a/src/Spring/Spring.Services/EnterpriseServices/ServicedComponentHelper.cs b/src/Spring/Spring.Services/EnterpriseServices/ServicedComponentHelper.cs index 00fb0496..886db46e 100644 --- a/src/Spring/Spring.Services/EnterpriseServices/ServicedComponentHelper.cs +++ b/src/Spring/Spring.Services/EnterpriseServices/ServicedComponentHelper.cs @@ -68,59 +68,80 @@ namespace Spring.EnterpriseServices { if (isInitialized) return; - isInitialized = true; - - // this is to ensure, that assemblies placed next to the component assembly can be loaded - // even when they are not strong named. - AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve); - FileInfo componentAssemblyFile = new FileInfo(componentType.Assembly.Location); - componentDirectory = componentAssemblyFile.Directory.FullName; - // switch to component assembly's directory (affects resolving relative paths during context instantiation!) - Environment.CurrentDirectory = componentDirectory; - // read in config file if any - FileInfo configFile = new FileInfo(componentAssemblyFile.FullName); - if (configFile.Exists) + try { - bool isRunningOutOfProcess = IsRunningOutOfProcess(); + Initialize(componentType); + } + catch (Exception e) + { + Trace.WriteLine("Error configuring application context for COM component of type " + componentType + ": " + e); + throw; + } -#if NET_2_0 - ExeConfigurationSystem comConfig = new ExeConfigurationSystem(configFile.FullName); + isInitialized = true; + } + } - if (isRunningOutOfProcess) + private static void Initialize(Type componentType) + { + // this is to ensure, that assemblies placed next to the component assembly can be loaded + // even when they are not strong named. + AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve); + FileInfo componentAssemblyFile = new FileInfo(componentType.Assembly.Location); + FileInfo assemblyFile = new FileInfo(componentAssemblyFile.FullName); + + bool isRunningOutOfProcess = IsRunningOutOfProcess(); + FileInfo configFile = new FileInfo(componentAssemblyFile.FullName + ".config"); + + // no config file and in-proc -> reuse app's context, error otherwise + if (!configFile.Exists) + { + if (!isRunningOutOfProcess) + { + // check for context with component's name + if (ContextRegistry.IsContextRegistered(componentType.Name)) { - Trace.WriteLine(string.Format("configuring COM OutProc Server '{0}' using '{1}'", componentAssemblyFile.FullName, componentAssemblyFile.FullName + ".config")); - - // make the config "global" - ConfigurationUtils.SetConfigurationSystem(comConfig, true); - _appContext = ContextRegistry.GetContext(); - if (_appContext == null) - { - throw ConfigurationUtils.CreateConfigurationException("Spring-exported COM components require section"); - } - - } + Trace.WriteLine(string.Format("configuring COM InProc Server '{0}' using section from app.config", componentAssemblyFile.FullName, componentType.Name)); + _appContext = ContextRegistry.GetContext(componentType.Name); + } else { - Trace.WriteLine(string.Format("configuring COM InProc Server '{0}' using section from file '{1}'", componentAssemblyFile.FullName, componentAssemblyFile.FullName + ".config")); - _appContext = (IApplicationContext)comConfig.GetSection("spring/context"); - if (_appContext == null) - { - throw ConfigurationUtils.CreateConfigurationException("Spring-exported COM components require section in configuration file"); - } + Trace.WriteLine(string.Format("configuring COM InProc Server '{0}' using section from file '{1}'", componentAssemblyFile.FullName, configFile.FullName)); + _appContext = ContextRegistry.GetContext(); } -#else - _appContext = (IApplicationContext) ConfigurationReader.GetSection(new FileSystemResource(configFile.FullName + ".config"),"spring/context"); - if (_appContext == null) - { - throw ConfigurationUtils.CreateConfigurationException("Spring-exported COM components require section in configuration file"); - } -#endif - } - else - { - Trace.WriteLine(string.Format("No configuration file '{0}' for COM component '{1}' found - bypassing configuration", componentAssemblyFile.FullName + ".config", componentAssemblyFile.FullName)); + return; } + throw ConfigurationUtils.CreateConfigurationException("Spring-exported COM components require section in configuration file '" + configFile.FullName + "'"); } + + // set and switch to component assembly's directory (affects resolving relative paths during context instantiation!) + componentDirectory = componentAssemblyFile.Directory.FullName; + Environment.CurrentDirectory = componentDirectory; + + if (isRunningOutOfProcess) + { +#if NET_2_0 + Trace.WriteLine(string.Format("configuring COM OutProc Server '{0}' using '{1}'", componentAssemblyFile.FullName, configFile.FullName)); + // read in config file + ExeConfigurationSystem comConfig = new ExeConfigurationSystem(assemblyFile.FullName); + // make the config "global" for this process, replacing any + // existing configuration that might already have been loaded + ConfigurationUtils.SetConfigurationSystem(comConfig, true); + _appContext = ContextRegistry.GetContext(); +#else + _appContext = (IApplicationContext) ConfigurationReader.GetSection(new FileSystemResource(configFile.FullName),"spring/context"); +#endif + } + else + { + Trace.WriteLine(string.Format("configuring COM InProc Server '{0}' using section from file '{1}'", componentAssemblyFile.FullName, configFile.FullName)); + _appContext = (IApplicationContext)ConfigurationReader.GetSection(new FileSystemResource(configFile.FullName), "spring/context"); + } + if (_appContext == null) + { + throw ConfigurationUtils.CreateConfigurationException("Spring-exported COM components require section in configuration file"); + } + Trace.WriteLine(string.Format("completed configuring COM Component '{0}' using '{1}'", componentAssemblyFile.FullName, configFile.FullName)); } private static bool IsRunningOutOfProcess() @@ -128,6 +149,7 @@ namespace Spring.EnterpriseServices // TODO: checkout a prob. better way to find out, whether we are executing as a com server or library return AppDomain.CurrentDomain.SetupInformation.ApplicationName == "dllhost.exe"; } + private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) { string name = args.Name.Split(',')[0]; @@ -142,11 +164,16 @@ namespace Spring.EnterpriseServices public static object GetObject(ServicedComponent sender, string targetName) { EnsureComponentContextRegistryInitialized(sender.GetType()); - if (_appContext == null) + try { - throw ConfigurationUtils.CreateConfigurationException("Spring-exported COM components require section in configuration file"); + return _appContext.GetObject(targetName); + + } + catch (Exception e) + { + Trace.WriteLine("Error configuring application context for COM component of type " + sender.GetType() + ": " + e); + throw; } - return _appContext.GetObject(targetName); } } }