PDA

View Full Version : Error instantiating context 'spring.root'


myenterprize
06-11-2007, 03:22 AM
I am using Spring.Net and nHibernate for the very first time...I must admit I dont know much about .NET applications. I have copied the example App.config from Northwind example shipped with NHibernet12 module of Spring and tried changing it as I need. But the thing would always fail with an exception. It seems like really basic thing but I have spent quite sometime on it: The exception trace is at the bottom:

Here is my configuration file: (Advance apologies for all the Cut & Paste)

<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="spring">
<section name="context"type="Spring.Context.Support.ContextHandler, Spring.Core" />
<section name="parsers" type="Spring.Context.Support.ConfigParsersSectionHandler , Spring.Core" />
</sectionGroup>
<section name="databaseSettings" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<spring>
<parsers>
<parser type="Spring.Data.DatabaseConfigParser, Spring.Data" />
</parsers>
<context>
<resource uri="Dao.xml" />
</context>
</spring>
<!-- These properties are referenced in Dao.xml -->
<databaseSettings>
<add key="db.datasource" value="localhost" />
<add key="db.user" value="rat" />
<add key="db.password" value="password" />
<add key="db.database" value="userandgroups" />
</databaseSettings>
<appSettings/>
<connectionStrings/>
</configuration>

DAO.xml

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net"
xmlns:db="http://www.springframework.net/database">

<!-- Referenced by main application context configuration file -->
<description>
My Object definitions for the Data Access Objects.
</description>

<!-- Property placeholder configurer for database settings -->
<object type="Spring.Objects.Factory.Config.PropertyPlaceholderC onfigurer, Spring.Core">
<property name="ConfigSections" value="databaseSettings"/>
</object>

<!-- Database and NHibernate Configuration -->
<db:dbProvider id="DbProvider"
provider="MySql"
connectionString="Server=${db.datasource};Database=${db.database};Ui d=${db.user};Pwd=${db.password};Trusted_Connection =False"/>
<object id="SessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate12">
<property name="DbProvider" ref="DbProvider"/>
<property name="MappingAssemblies">
<list>
<value>My.Project.Domain</value>
</list>
</property>
<property name="HibernateProperties">
<dictionary>
<entry key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
<entry key="hibernate.dialect" value="NHibernate.Dialect.MySQLDialect" />
<entry key="hibernate.connection.driver_class" value="NHibernate.Driver.MySqlDataDriver" />
</dictionary>
</property>

</object>


<object id="HibernateTransactionManager"
type="Spring.Data.NHibernate.HibernateTransactionManager , Spring.Data.NHibernate12">

<property name="DbProvider" ref="DbProvider"/>
<property name="SessionFactory" ref="SessionFactory"/>

</object>

<!-- Data Access Objects -->
<object id="UserDao" type="My.Project.Dao.BaseDao&lt; My.Project.Domain.Security.User,UInt32>, My.Project.Dao" >
<property name="SessionFactory" ref="SessionFactory"/>
</object>
<object id="GroupDao" type My.Project.Dao.BaseDao&lt; My.Project.Domain.Security.Group,UInt32>, My.Project.Dao">
<property name="SessionFactory" ref="SessionFactory"/>
</object>
</objects>

The code:

private IApplicationContext ctx;
private void Form1_Load(object sender, EventArgs e)
{
ctx = ContextRegistry.GetContext(AbstractApplicationCont ext.ContextSectionName);
IDao<User, Int32> userDao =IDao<User,Int32>)ctx.GetObject("UserDao");
if (userDao != null) {
MessageBox.Show("Looks like working. Try Using it positively");
}
}

The Exception:
System.Configuration.ConfigurationErrorsException was unhandled
Message="Error instantiating context 'spring.root'."
Source="System.Configuration"
BareMessage="Error instantiating context 'spring.root'."
Line=0
StackTrace:
at System.Configuration.BaseConfigurationRecord.Evalu ateOne(String[] keys, SectionInput input, Boolean isTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult)
at System.Configuration.BaseConfigurationRecord.Evalu ate(FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult, Boolean getLkg, Boolean getRuntimeObject, Object& result, Object& resultRuntimeObject)
at System.Configuration.BaseConfigurationRecord.GetSe ctionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
at System.Configuration.BaseConfigurationRecord.GetSe ctionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
at System.Configuration.BaseConfigurationRecord.GetSe ctionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
at System.Configuration.BaseConfigurationRecord.GetSe ction(String configKey, Boolean getLkg, Boolean checkPermission)
at System.Configuration.BaseConfigurationRecord.GetSe ction(String configKey)
at System.Configuration.ClientConfigurationSystem.Sys tem.Configuration.Internal.IInternalConfigSystem.G etSection(String sectionName)
at System.Configuration.ConfigurationManager.GetSecti on(String sectionName)
at Spring.Util.ConfigurationUtils.GetSection(String sectionName)
at Spring.Context.Support.ContextRegistry.InitializeC ontextIfNeeded()
at Spring.Context.Support.ContextRegistry.GetContext( String name)
at SinusNode.CommunityBuilder.Test.Form1.Form1_Load(O bject sender, EventArgs e) in C:\Documents and Settings\Ahmad\My Documents\Visual Studio 2005\Projects\Zone\SinusNode.CommunityBuilder.Test \Form1.cs:line 35
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Mes sage& m)
at System.Windows.Forms.ContainerControl.WndProc(Mess age& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.O nMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.W ndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.SafeNativeMethods.ShowWindow( HandleRef hWnd, Int32 nCmdShow)
at System.Windows.Forms.Control.SetVisibleCore(Boolea n value)
at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
at System.Windows.Forms.Control.set_Visible(Boolean value)
at System.Windows.Forms.Application.ThreadContext.Run MessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.Run MessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at SinusNode.CommunityBuilder.Test.Program.Main() in C:\Documents and Settings\Ahmad\My Documents\Visual Studio 2005\Projects\Zone\SinusNode.CommunityBuilder.Test \Program.cs:line 17
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.Run UsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context( Object state)
at System.Threading.ExecutionContext.Run(ExecutionCon text executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

Bruno Baia
06-11-2007, 11:49 AM
Hi,


ctx = ContextRegistry.GetContext(AbstractApplicationCont ext.ContextSectionName);

Try this instead :

ctx = ContextRegistry.GetContext();


Another potential error is the location of the DAO.xml file. With your configuration it should be in the bin directory.


Check this thread, there is a Windows Form project for the Northwind sample :
Spring.Northwind.Windows - Need a WinForms nHibernate SQL Quickstart (http://forum.springframework.net/showthread.php?t=2905)


HTH,
Bruno

myenterprize
06-12-2007, 02:07 PM
Thank you for the reply.

I have already tried
<code>
ctx = ContextRegistry.GetContext();
</code>

I thought Visual Studio 2005 will copy the file to bin directory automatically as I have specified that in my Dao.xml's properties.

My other guess was that there is an error in my Dao.xml, but I have tried it with a spring file which works fine through manual programming, I mean I can create context using a new XMLApplicationContext(...).

Could you please have a look at App.config file and see if everything is OK. The sections, handlers etc.
Regards

Bruno Baia
06-12-2007, 03:40 PM
Try to get the base exception checking the inner exception until it's null.
You'll get an explicit error.

HTH,
Bruno

myenterprize
06-13-2007, 11:36 AM
I was able to resolve that after digging the Exception trace. I put a try{}catch(Exception ex){} block and did a ex.GetBaseException() and found that some of the references were messed up. I was referring to an older version of Spring dll.
The exception thrown should be more obvious from the spring...instead of just "cant create context spring.root", the stack trace should tell that references are screwed.
Thanks for the help.

Bruno Baia
06-13-2007, 10:16 PM
The exception thrown should be more obvious from the spring...instead of just "cant create context spring.root", the stack trace should tell that references are screwed.
Thanks for the help.
Yep, you are not the only one in this case.
We are working on this.

Bruno