View Full Version : Problem with DataQuickStart example
ctlqt12
08-15-2007, 06:52 PM
Hi all,
I'm new to Spring.Net, i tried out 2 days for making an example about interacting Database in this example.
I use ( Spring.NET-1.1-RC1.zip, VSS 2005, MSSQL 2000 ), and i tried to change Template/ExampleTests.cs to Console applicaton.
I tried to use CommandCallbackDao to select some data and display them on console, but unsuccessful. The following is what in Template/ExampleTests.cs
private IApplicationContext ctx;
[SetUp]
public void InitContext()
{
// Configure Spring programmatically
NamespaceParserRegistry.RegisterParser(typeof(Data baseNamespaceParser));
ctx = new XmlApplicationContext(
"assembly://Spring.DataQuickStart.Tests/Spring.DataQuickStart.Template/ExampleTests.xml");
}
public int CallbackDaoTest()
{
CommandCallbackDao commandCallbackDao = ctx["commandCallbackDao"] as CommandCallbackDao; //error in this row
int count = commandCallbackDao.FindCountWithPostalCode("1010");
return count;
}
static void Main(string[] args)
{
System.Console.WriteLine("Begin get data ....");
ExampleTests objData = new ExampleTests();
int count = objData.CallbackDaoTest();
System.Console.WriteLine("Total is : " + count);
System.Console.ReadLine();
}
When i run Template/ExampleTests.cs, I get error "Object reference not set to an instance of an object." on bold line. ( I don't change anything in Template/ExampleTests.xml )
How could i do that solve that error?
Mark Pollack
08-16-2007, 08:52 PM
Hi,
The code that initializes the IApplicationContext is not called, it is sitting inside the method InitContext. You can call this in your contructor or explicitly in the main to get rid of the error. Just a note, I'll be converting the examples to not be unit test and instead console or winform applications which I think will make it easier for people to get started using the code and experimenting with it.
Cheers,
Mark
jim_fisk
08-20-2007, 01:56 PM
I'm having the same trouble in getting the data quick start example going as an console app as well. Now I do include a call to the ContextRegistry to return the IApplicationContext, but it throws an exception moaning about (Cannot instantiate Type [Spring.Context.Support.XmlApplicationContext] using ctor [Void .ctor(System.String, Boolean, System.String[])] : 'Exception has been thrown by the target of an invocation.') <- that is the inner exception.
Any help in this would be great. Thank you.
Here is a copy of my app.config
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
</sectionGroup>
<!--<sectionGroup name="common">
<section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
</sectionGroup>-->
<section name="databaseSettings" type="System.Configuration.NameValueSectionHandler"/>
</configSections>
<spring>
<context>
<resource uri="file://Dao.xml"/>
</context>
</spring>
<!--<common>
<logging>
<factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter , Common.Logging.Log4Net129">
<arg key="configType" value="EXTERNAL" />
</factoryAdapter>
</logging>
</common>-->
<databaseSettings>
<add key="db.datasource" value="vbase3.db;version=3"/>
<add key="db.user" value="springqa"/>
<add key="db.password" value="springqa"/>
<add key="db.database" value="Northwind"/>
</databaseSettings>
</configuration>
and my dao.xml file contents
<?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>
The Northwind 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:provider id="DbProvider"
provider="SQLite-1.0.43"
connectionString="Data Source=vbase3.db;Version=3"/>
<!--
<object id="DbProvider" type="Spring.Data.Support.SqlProvider, Spring.Data">
<property name="ConnectionString"
value="Data Source=${db.datasource};Database=${db.database};Us er ID=${db.user};Password=${db.password};Trusted_Conn ection=False"/>
</object>
-->
<object id="SessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate">
<property name="DbProvider" ref="DbProvider"/>
<property name="MappingAssemblies">
<list>
<value>ConsoleApplication1</value>
</list>
</property>
<property name="HibernateProperties">
<dictionary>
<entry key="hibernate.connection.provider"
value="NHibernate.Connection.DriverConnectionProvider"/>
<entry key="hibernate.dialect"
value="NHibernate.Dialect.SQLiteDialect"/>
<entry key="hibernate.connection.driver_class"
value="NHibernate.Driver.SQLite20Driver"/>
</dictionary>
</property>
</object>
<object id="HibernateTransactionManager"
type="Spring.Data.NHibernate.HibernateTransactionManager , Spring.Data.NHibernate">
<property name="DbProvider" ref="DbProvider"/>
<property name="SessionFactory" ref="SessionFactory"/>
</object>
<object id="HibernateTemplate" type="Spring.Data.NHibernate.HibernateTemplate">
<property name="SessionFactory" ref="SessionFactory" />
<property name="TemplateFlushMode" value="Auto" />
<property name="CacheQueries" value="true" />
</object>
<!-- Data Access Objects -->
<object id="GeographicalAreaDao" type="ConsoleApplication1, ConsoleApplication1">
<property name="HibernateTemplate" ref="HibernateTemplate"/>
</object>
</objects>
Mark Pollack
08-20-2007, 10:28 PM
Hi,
I'm pretty sure there should be a more detailed inner exception, can you put a breakpoint in a catch block around where you create the context and take a look. That would help.
Just another FYI, I'll be converting the sample apps to be console/winform based as compared to nunit tests. Longer term I'd like to have project templates to choose from to avoid the common config errors/issues.
Regards,
Mark
Mark Pollack
08-21-2007, 02:29 AM
Hi,
One thing I can notice off the bat is that you should register the parser for the database namespace. (We have a strategy for eliminating this manual registration but for now it is needed).
So the section group name spring should look like
<sectionGroup name='spring'>
<section name='context' type='Spring.Context.Support.ContextHandler, Spring.Core'/>
<section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHand ler, Spring.Core"/>
</sectionGroup>
and the spring section itself
<spring>
<parsers>
<parser type="Spring.Data.Config.DatabaseNamespaceParser, Spring.Data" />
</parsers>
<context>
<resource uri='file://Dao.xml'/>
</context>
</spring>
You should also make sure that the file Dao.xml gets copied to the runtime directory.
Hope this helps,
Mark
jim_fisk
08-21-2007, 10:00 AM
Right I added those entries into the config file as per your instructions and made sure that the Dao.xml was being copied to the bin dir after every successful build. Ran it again and BANG, here is the FULL inner exception :-
A first chance exception of type 'System.Configuration.ConfigurationErrorsException ' occurred in System.Configuration.dll
Spring.Objects.FatalObjectException: Cannot instantiate Type [Spring.Context.Support.XmlApplicationContext] using ctor [Void .ctor(System.String, Boolean, System.String[])] : 'Exception has been thrown by the target of an invocation.' ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> Spring.Objects.Factory.ObjectDefinitionStoreExcept ion: Unexpected exception parsing XML document from file [C:\Documents and Settings\jamesf\My Documents\Visual Studio 2005\Projects\testSprintAndNHbernate\ConsoleApplic ation1\bin\Debug\Dao.xml] ---> System.TypeInitializationException: The type initializer for 'Spring.Objects.Factory.Xml.XmlParserRegistry' threw an exception. ---> System.Configuration.ConfigurationErrorsException: An error occurred creating the configuration section handler for spring/parsers: Could not load type 'Spring.Context.Support.NamespaceParsersSectionHan dler' from assembly 'Spring.Core'. (C:\Documents and Settings\jamesf\My Documents\Visual Studio 2005\Projects\testSprintAndNHbernate\ConsoleApplic ation1\bin\Debug\ConsoleApplication1.vshost.exe.co nfig line 6) ---> System.TypeLoadException: Could not load type 'Spring.Context.Support.NamespaceParsersSectionHan dler' from assembly 'Spring.Core'.
at System.Configuration.TypeUtil.GetTypeWithReflectio nPermission(IInternalConfigHost host, String typeString, Boolean throwOnError)
at System.Configuration.RuntimeConfigurationRecord.Ru ntimeConfigurationFactory.Init(RuntimeConfiguratio nRecord configRecord, FactoryRecord factoryRecord)
at System.Configuration.RuntimeConfigurationRecord.Ru ntimeConfigurationFactory.InitWithRestrictedPermis sions(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord)
at System.Configuration.RuntimeConfigurationRecord.Ru ntimeConfigurationFactory..ctor(RuntimeConfigurati onRecord configRecord, FactoryRecord factoryRecord)
at System.Configuration.RuntimeConfigurationRecord.Cr eateSectionFactory(FactoryRecord factoryRecord)
at System.Configuration.BaseConfigurationRecord.FindA ndEnsureFactoryRecord(String configKey, Boolean& isRootDeclaredHere)
--- End of inner exception stack trace ---
at System.Configuration.BaseConfigurationRecord.FindA ndEnsureFactoryRecord(String configKey, Boolean& isRootDeclaredHere)
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.Objects.Factory.Xml.XmlParserRegistry..ccto r()
--- End of inner exception stack trace ---
at Spring.Objects.Factory.Xml.XmlParserRegistry.GetSc hemas()
at Spring.Objects.Factory.Xml.XmlObjectDefinitionRead er.DoLoadObjectDefinitions(Stream stream, IResource resource)
--- End of inner exception stack trace ---
at Spring.Objects.Factory.Xml.XmlObjectDefinitionRead er.DoLoadObjectDefinitions(Stream stream, IResource resource)
at Spring.Objects.Factory.Xml.XmlObjectDefinitionRead er.LoadObjectDefinitions(IResource resource)
at Spring.Objects.Factory.Support.AbstractObjectDefin itionReader.LoadObjectDefinitions(String location)
at Spring.Objects.Factory.Support.AbstractObjectDefin itionReader.LoadObjectDefinitions(String[] locations)
at Spring.Context.Support.AbstractXmlApplicationConte xt.LoadObjectDefinitions(XmlObjectDefinitionReader objectDefinitionReader)
at Spring.Context.Support.AbstractXmlApplicationConte xt.LoadObjectDefinitions(DefaultListableObjectFact ory objectFactory)
at Spring.Context.Support.AbstractXmlApplicationConte xt.RefreshObjectFactory()
at Spring.Context.Support.AbstractApplicationContext. Refresh()
at Spring.Context.Support.XmlApplicationContext..ctor (Boolean refresh, String name, Boolean caseSensitive, IApplicationContext parentContext, String[] configurationLocations)
at Spring.Context.Support.XmlApplicationContext..ctor (String name, Boolean caseSensitive, String[] configurationLocations)
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle._InvokeConstructor(Obje ct[] args, SignatureStruct& signature, IntPtr declaringType)
at System.RuntimeMethodHandle.InvokeConstructor(Objec t[] args, SignatureStruct signature, RuntimeTypeHandle declaringType)
at System.Reflection.RuntimeConstructorInfo.Invoke(Bi ndingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.ConstructorInfo.Invoke(Object[] parameters)
at Spring.Objects.ObjectUtils.InstantiateType(Constru ctorInfo constructor, Object[] arguments)
--- End of inner exception stack trace ---
at Spring.Objects.ObjectUtils.InstantiateType(Constru ctorInfo constructor, Object[] arguments)
at Spring.Context.Support.ContextHandler.RootContextI nstantiator.InvokeContextConstructor(ConstructorIn fo ctor)
at Spring.Context.Support.ContextHandler.ContextInsta ntiator.InstantiateContext()
Bruno Baia
08-21-2007, 10:20 AM
Hi,
You are using an old Spring.NET version (I presume 1.1 M2).
Mark's post is based on the latest version : 1.1 RC1
Bruno
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.