View Full Version : hierarchical contexts
I've got master pages working fine when the pages are all in the same directory and referenced in the same config file. When I try to move the 'Content.aspx' page into a dir off the root: 'public', I can't get it to work properly. I get an error indicating that:
No object named 'basePage' is defined: Spring.Objects.Factory.Support.WebObjectFactory
That objec tis defined in the root tho'? I thought if the object was not found in the current config, it would automatically search in the higher levels.. I'm sure I'm missing something obvious.
I've got the following in my root config:
<configuration>
<configSections>
<!--
These handlers are standard Spring.NET configuration sections; the meat of the configuration
is done below in the actual sections themselves.
-->
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" />
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
</configSections>
<spring>
<!--
This is the meat; in answer to your post, this is where Spring.NET transparently instantiates
and configures an application context for you 'behind the scenes' as it were...
-->
<context type="Spring.Context.Support.WebApplicationContext, Spring.Web">
<resource uri="config://spring/objects" />
</context>
<objects xmlns="http://www.springframework.net">
<object id="masterPage" type="~/Layout/Master.aspx" />
<object id="masterPageBlank" type="~/Layout/MasterBlank.aspx" />
<object id="basePage" abstract="true">
<property name="Master">
<ref object="masterPage"/>
</property>
</object>
<object type="nTierWeb.mainContent, nTierWeb" singleton="false">
<property name="ContentHandler">
<ref object="contentHandler" />
</property>
</object>
<object id="contentHandler" type="nTierDAL.contentHandler" />
</objects>
</spring>
And then this in the 'public' dir:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<!--
These handlers are standard Spring.NET configuration sections; the meat of the configuration
is done below in the actual sections themselves.
-->
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" />
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
</configSections>
<spring>
<!--
This is the meat; in answer to your post, this is where Spring.NET transparently instantiates
and configures an application context for you 'behind the scenes' as it were...
-->
<context type="Spring.Context.Support.WebApplicationContext, Spring.Web">
<resource uri="config://spring/objects" />
</context>
<objects xmlns="http://www.springframework.net">
<object type="Content.aspx" parent="basePage">
<property name="ContentHandler">
<ref object="contentHandler" />
</property>
</object>
</objects>
</spring>
</configuration>
Thanks in advance!
Rick Evans
07-14-2005, 09:36 AM
Hiya
Indeed... the trials and tribulations of being an early adopter :?
When replicating your issue, I too encountered the same error. I have managed to work through to a solution, and although it is far from satisfactory, it does work. More about that later, onto the solution.
Change your Web.config file in the 'public' dir to look like so...
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="spring">
<section name="childObjects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
</configSections>
<spring>
<context type="Spring.Context.Support.WebApplicationContext, Spring.Web">
<resource uri="config://spring/childObjects" />
</context>
<childObjects xmlns="http://www.springframework.net">
<object type="Content.aspx" parent="basePage">
<property name="ContentHandler">
<ref object="contentHandler" />
</property>
</object>
</childObjects>
</spring>
</configuration>
As you can see, the pertinent change is to use a distinct name (childObjects) for the section that contains your child object definitions.
Yes, this is at variance with the material (http://opensource.atlassian.com/confluence/spring/display/NET/Automatic+context+loading+and+hierarchical+context s) on the wiki.
Thanks for spotting this. I am pinging the other Spring.NET developers concerning this issue... please do keep watching this forum post, 'cos as soon as I have concrete feedback from said other developers, I will post again.
Ciao
Rick
Aleks Seovic
08-07-2005, 06:41 PM
Actually, this should not be necessary. You should completely remove <configSections> element from all Web.config files except the root one (if you define Spring section handlers in machine.config, you don't need them in the root one either).
All you need in your child context definition is <context> element and <objects> element (if your context element references loads object definitions from the same config file, which I believe is best practice for child contexts), and everything should work fine, including object resolution by walking the context hierarchy.
- Aleks
I have a same kind of confuration with root level and child level. I have singleton service objects defined at root level and I reference them at child level pages. I noticed that when I'm debuggin the project in visual studio, if I first access a page at child level I get "No object named 'rootLevelServiceObject' is defined" exception, but if I first access a page at root level everything works fine. So is there a way to ensure that root level objects are instantiated when accessing a child level page?
- Otso
Aleks Seovic
09-02-2005, 12:46 AM
Hm, I cannot reproduce this behavior -- regardless of which page I access first all root definitions get properly initialized.
Can you sand me a sample app that demonstrates the bug. Maybe I have something configured that I'm not aware of. Also, please send me detailed information about your .NET version (mine is 1.1 SP1).
Regards,
Aleks
My .NET Framework version is v1.1.4322. I have sp1 installed. I made this new test project to demonstrate the issue:
in project main folder:
-subfolder
-MainContextForm.aspx
-Service.cs
-Web.config
in sub folder:
-SubContextForm.apsx
-Web.Config
Web.config in main folder:
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
<sectionGroup name="spring">
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/>
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
</sectionGroup>
</configSections>
<spring>
<context type="Spring.Context.Support.WebApplicationContext, Spring.Web">
<resource uri="config://spring/objects"/>
</context>
<objects>
<object name="service1" type="SpringTest.Service, SpringTest"/>
<object type="MainContextForm.aspx">
<property name="Service">
<ref object="service1"/>
</property>
</object>
</objects>
</spring>
<system.web>
<httpHandlers>
<add verb="*" path="*.aspx" type="Spring.Web.Support.PageHandlerFactory, Spring.Web"/>
</httpHandlers>
</system.web>
Web.config in subfolder:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<spring>
<context type="Spring.Context.Support.WebApplicationContext, Spring.Web">
<resource uri="config://spring/objects"/>
</context>
<objects>
<object type="SubContextForm.aspx">
<property name="Service">
<ref object="service1"/>
</property>
</object>
</objects>
</spring>
</configuration>
Both forms have a public property Service of type SpringTest.Service. So when I set startup page in vs.net to be SubContextForm.aspx i get this exception:
Stack Trace:
[NoSuchObjectDefinitionException: No object named 'service1' is defined : Spring.Objects.Factory.Support.WebObjectFactory]
Spring.Objects.Factory.Support.DefaultListableObje ctFactory.GetObjectDefinition(String name) in c:\projects\Spring.Net\src\Spring\Spring.Core\Obje cts\Factory\Support\DefaultListableObjectFactory.c s:442
Spring.Objects.Factory.Support.AbstractObjectFacto ry.GetMergedObjectDefinition(String name, Boolean includingAncestors) in c:\projects\Spring.Net\src\Spring\Spring.Core\Obje cts\Factory\Support\AbstractObjectFactory.cs:419
Spring.Objects.Factory.Support.AbstractObjectFacto ry.GetObject(String name, Type requiredType, Object[] arguments) in c:\projects\Spring.Net\src\Spring\Spring.Core\Obje cts\Factory\Support\AbstractObjectFactory.cs:204
Spring.Objects.Factory.Support.AbstractObjectFacto ry.GetObject(String name, Object[] arguments) in c:\projects\Spring.Net\src\Spring\Spring.Core\Obje cts\Factory\Support\AbstractObjectFactory.cs:1195
Spring.Objects.Factory.Support.AbstractObjectFacto ry.GetObject(String name) in c:\projects\Spring.Net\src\Spring\Spring.Core\Obje cts\Factory\Support\AbstractObjectFactory.cs:1156
Spring.Objects.Factory.Support.AbstractObjectFacto ry.GetObject(String name, Type requiredType, Object[] arguments) in c:\projects\Spring.Net\src\Spring\Spring.Core\Obje cts\Factory\Support\AbstractObjectFactory.cs:202
Spring.Objects.Factory.Support.AbstractObjectFacto ry.GetObject(String name, Object[] arguments) in c:\projects\Spring.Net\src\Spring\Spring.Core\Obje cts\Factory\Support\AbstractObjectFactory.cs:1195
Spring.Objects.Factory.Support.AbstractObjectFacto ry.GetObject(String name) in c:\projects\Spring.Net\src\Spring\Spring.Core\Obje cts\Factory\Support\AbstractObjectFactory.cs:1156
Spring.Objects.Factory.Support.AbstractAutowireCap ableObjectFactory.ResolveReference(IConfigurableOb jectDefinition definition, String name, String argumentName, RuntimeObjectReference reference) in c:\projects\Spring.Net\src\Spring\Spring.Core\Obje cts\Factory\Support\AbstractAutowireCapableObjectF actory.cs:1868
[ObjectCreationException: Error creating object with name 'SubContextForm' defined in 'config [objects] line 8' : Can't resolve reference to object 'service1' while setting 'Service'.]
Spring.Objects.Factory.Support.AbstractAutowireCap ableObjectFactory.ResolveReference(IConfigurableOb jectDefinition definition, String name, String argumentName, RuntimeObjectReference reference) in c:\projects\Spring.Net\src\Spring\Spring.Core\Obje cts\Factory\Support\AbstractAutowireCapableObjectF actory.cs:1872
Spring.Objects.Factory.Support.AbstractAutowireCap ableObjectFactory.ResolveValueIfNecessary(String name, RootObjectDefinition definition, String argumentName, Object argumentValue) in c:\projects\Spring.Net\src\Spring\Spring.Core\Obje cts\Factory\Support\AbstractAutowireCapableObjectF actory.cs:1743
Spring.Objects.Factory.Support.AbstractAutowireCap ableObjectFactory.ApplyPropertyValues(String name, RootObjectDefinition definition, IObjectWrapper wrapper, IPropertyValues properties) in c:\projects\Spring.Net\src\Spring\Spring.Core\Obje cts\Factory\Support\AbstractAutowireCapableObjectF actory.cs:247
Spring.Objects.Factory.Support.AbstractAutowireCap ableObjectFactory.PopulateObject(String name, RootObjectDefinition definition, IObjectWrapper wrapper) in c:\projects\Spring.Net\src\Spring\Spring.Core\Obje cts\Factory\Support\AbstractAutowireCapableObjectF actory.cs:377
Spring.Objects.Factory.Support.AbstractAutowireCap ableObjectFactory.CreateObject(String name, RootObjectDefinition definition, Object[] arguments, Boolean allowEagerCaching) in c:\projects\Spring.Net\src\Spring\Spring.Core\Obje cts\Factory\Support\AbstractAutowireCapableObjectF actory.cs:781
Spring.Objects.Factory.Support.AbstractAutowireCap ableObjectFactory.CreateObject(String name, RootObjectDefinition definition, Object[] arguments) in c:\projects\Spring.Net\src\Spring\Spring.Core\Obje cts\Factory\Support\AbstractAutowireCapableObjectF actory.cs:634
Spring.Objects.Factory.Support.AbstractObjectFacto ry.GetObject(String name, Type requiredType, Object[] arguments) in c:\projects\Spring.Net\src\Spring\Spring.Core\Obje cts\Factory\Support\AbstractObjectFactory.cs:215
Spring.Objects.Factory.Support.AbstractObjectFacto ry.GetObject(String name, Object[] arguments) in c:\projects\Spring.Net\src\Spring\Spring.Core\Obje cts\Factory\Support\AbstractObjectFactory.cs:1195
Spring.Objects.Factory.Support.AbstractObjectFacto ry.GetObject(String name) in c:\projects\Spring.Net\src\Spring\Spring.Core\Obje cts\Factory\Support\AbstractObjectFactory.cs:1156
Spring.Context.Support.AbstractApplicationContext. GetObject(String name) in c:\projects\Spring.Net\src\Spring\Spring.Core\Cont ext\Support\AbstractApplicationContext.cs:987
Spring.Web.Support.PageHandler.System.Web.IHttpHan dler.ProcessRequest(HttpContext context) in c:\projects\Spring.Net\src\Spring\Spring.Web\Web\S upport\PageHandlerFactory.cs:152
System.Web.CallHandlerExecutionStep.System.Web.Htt pApplication+IExecutionStep.Execute()
System.Web.HttpApplication.ExecuteStep(IExecutionS tep step, Boolean& completedSynchronously) +87
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.2032; ASP.NET Version:1.1.4322.2032
I can also send you whole project If you give me your email address.
- Otso
Aleks Seovic
09-02-2005, 03:05 PM
Thanks, whole project would be appreciated.
You can email it to me at aleks at seovic dot com.
Thanks! The suggested work around was fine, but now I have a new problem.
If I globally inject a reference to UserControl in parent context, the injection only happens when i'm using the UserControl in pages defined also in parent context. I'm now using an external Objects.xml for parent context as you suggested.
Otso
vBulletin® v3.7.3, Copyright ©2000-2009, Jelsoft Enterprises Ltd.