编程语言
首页 > 编程语言> > c# – Spring.NET – 未将对象引用设置为对象的实例

c# – Spring.NET – 未将对象引用设置为对象的实例

作者:互联网

我是.NET的新手.我之前在Java中使用过Spring Framework和Hibernate,但这是我在.NET中的第一次.为了开发我的应用程序,我使用Spring.Northwind解决方案作为示例.

hibernate映射是:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
    <class name="EMSApplication.Domain.Employee, EMSApplication.Domain" table="ems_Employees" proxy="EMSApplication.Domain.IEmployee, EMSApplication.Domain" lazy="true">
        <id name="Id" type="System.Int32">
        <column name="Id" not-null="true"/>
        <generator class="native"/>
        </id>
        <property name="Username" type="System.String">
        <column name="Username" not-null="true"/>
        </property>
        <property name="Firstname" type="System.String">
        <column name="Firstname" not-null="true"/>
        </property>     
    </class>    
</hibernate-mapping>

正如您所看到的,Employee类只有三个字段.它实现了IEmployee,它包含了这些字段的get和set方法的定义(如果你想看到我能提供的这些类).

现在Web.config我已经将spring设置为sectionGroup并将其定义为:

<configSections>
    <sectionGroup name="spring">
    <section name="context" type="Spring.Context.Support.WebContextHandler, Spring.Web"/>
    <section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core"/>
    </sectionGroup>
    <section name="databaseSettings" type="System.Configuration.NameValueSectionHandler"/>
</configSections>
<spring>
    <parsers/>
    <context>
    <resource uri="~/Web.xml"/>
    </context>
</spring>
<databaseSettings>
    <add key="db.datasource" value="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\JYM.mdf;Integrated Security=True;User Instance=True"/>
</databaseSettings>

另外我在appsettings中添加了Spring.Data.NHibernate.Support.OpenSessionInViewModule.SessionFactoryObjectName键:

<appSettings>
    <add key="Spring.Data.NHibernate.Support.OpenSessionInViewModule.SessionFactoryObjectName" value="NHibernateSessionFactory"/>
</appSettings>

system.web的httpModules包含以下条目:

<httpModules>
    <add name="Spring" type="Spring.Context.Support.WebSupportModule, Spring.Web"/>
    <add name="OpenSessionInView" type="Spring.Data.NHibernate.Support.OpenSessionInViewModule, Spring.Data.NHibernate32"/>
</httpModules>

最后,Spring资源Web.xml具有以下对象:

<object type="Spring.Objects.Factory.Config.PropertyPlaceholderConfigurer, Spring.Core">
    <property name="ConfigSections" value="databaseSettings"/>
</object>

<db:provider id="DbProvider" provider="System.Data.SqlClient" connectionString="Data Source=|DataDirectory|\JYM.mdf"/>

<object id="NHibernateSessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate32">
    <property name="DbProvider" ref="DbProvider"/>
    <property name="MappingResources">
    <list>
        <value>assembly://EMSApplication/Domain/EMSApplication.hbm.xml</value>
    </list>
    </property>
    <property name="HibernateProperties">
    <dictionary>
        <entry key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
        <entry key="dialect" value="NHibernate.Dialect.MsSql2008Dialect"/>
        <entry key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>
        <entry key="proxyfactory.factory_class" value="NHibernate.Bytecode.DefaultProxyFactoryFactory, NHibernate"/>
        <entry key="show_sql" value="true"/>
        <entry key="hbm2ddl.auto" value="update"/>
        <entry key="cache.use_query_cache" value="true"/>
    </dictionary>
    </property>

    <property name="ExposeTransactionAwareSessionFactory" value="true" />
</object>

<object id="transactionManager" type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate32">
    <property name="DbProvider" ref="DbProvider"/>
    <property name="SessionFactory" ref="NHibernateSessionFactory"/>
</object>

<object type="Spring.Dao.Attributes.PersistenceExceptionTranslationPostProcessor, Spring.Data"/>

<object id="EmployeeDao" type="EMSApplication.NHibernate.HibernateEmployeeDao">
    <property name="SessionFactory" ref="NHibernateSessionFactory"/>
</object>

<tx:attribute-driven transaction-manager="transactionManager"/>

现在,当我从Visual Studio 2010开始调试时,我得到了

System.NullReferenceException: Object reference not set to an instance
of an object.

完整的堆栈跟踪是:

    [NullReferenceException: Object reference not set to an instance of an object.]
   System.Xml.XmlReader.CalcBufferSize(Stream input) +14
   System.Xml.XmlTextReaderImpl.InitStreamInput(Uri baseUri, String baseUriStr, Stream stream, Byte[] bytes, Int32 byteCount, Encoding encoding) +66
   System.Xml.XmlTextReaderImpl..ctor(String url, Stream input, XmlNameTable nt) +93
   System.Xml.XmlTextReader..ctor(Stream input) +74
   NHibernate.Cfg.Configuration.AddInputStream(Stream xmlInputStream, String name) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs:631

[MappingException: Could not configure datastore from input stream ]
   NHibernate.Cfg.Configuration.LogAndThrow(Exception exception) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs:342
   NHibernate.Cfg.Configuration.AddInputStream(Stream xmlInputStream, String name) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs:641
   NHibernate.Cfg.Configuration.AddInputStream(Stream xmlInputStream) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs:611
   Spring.Data.NHibernate.LocalSessionFactoryObject.AfterPropertiesSet() in c:\_prj\spring-net\trunk\src\Spring\Spring.Data.NHibernate20\Data\NHibernate\LocalSessionFactoryObject.cs:599
   Spring.Objects.Factory.Support.AbstractAutowireCapableObjectFactory.InvokeInitMethods(Object target, String name, IConfigurableObjectDefinition definition) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Objects\Factory\Support\AbstractAutowireCapableObjectFactory.cs:1294
   Spring.Objects.Factory.Support.AbstractAutowireCapableObjectFactory.ConfigureObject(String name, RootObjectDefinition definition, IObjectWrapper wrapper) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Objects\Factory\Support\AbstractAutowireCapableObjectFactory.cs:1890
   Spring.Objects.Factory.Support.WebObjectFactory.ConfigureObject(String name, RootObjectDefinition definition, IObjectWrapper wrapper) in c:\_prj\spring-net\trunk\src\Spring\Spring.Web\Objects\Factory\Support\WebObjectFactory.cs:450
   Spring.Objects.Factory.Support.AbstractAutowireCapableObjectFactory.InstantiateObject(String name, RootObjectDefinition definition, Object[] arguments, Boolean allowEagerCaching, Boolean suppressConfigure) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Objects\Factory\Support\AbstractAutowireCapableObjectFactory.cs:921

[ObjectCreationException: Error creating object with name 'NHibernateSessionFactory' defined in 'file [D:\Code\EMSApplication\EMSApplication\Web.xml] line 14' : Initialization of object failed : Could not configure datastore from input stream ]
   Spring.Objects.Factory.Support.AbstractAutowireCapableObjectFactory.InstantiateObject(String name, RootObjectDefinition definition, Object[] arguments, Boolean allowEagerCaching, Boolean suppressConfigure) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Objects\Factory\Support\AbstractAutowireCapableObjectFactory.cs:938
   Spring.Objects.Factory.Support.AbstractObjectFactory.CreateAndCacheSingletonInstance(String objectName, RootObjectDefinition objectDefinition, Object[] arguments) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Objects\Factory\Support\AbstractObjectFactory.cs:2144
   Spring.Objects.Factory.Support.WebObjectFactory.CreateAndCacheSingletonInstance(String objectName, RootObjectDefinition objectDefinition, Object[] arguments) in c:\_prj\spring-net\trunk\src\Spring\Spring.Web\Objects\Factory\Support\WebObjectFactory.cs:299
   Spring.Objects.Factory.Support.AbstractObjectFactory.GetObjectInternal(String name, Type requiredType, Object[] arguments, Boolean suppressConfigure) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Objects\Factory\Support\AbstractObjectFactory.cs:2065
   Spring.Objects.Factory.Support.AbstractObjectFactory.GetObject(String name) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Objects\Factory\Support\AbstractObjectFactory.cs:1826
   Spring.Objects.Factory.Support.DefaultListableObjectFactory.GetObjectsOfType(Type type, Boolean includePrototypes, Boolean includeFactoryObjects) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Objects\Factory\Support\DefaultListableObjectFactory.cs:770
   Spring.Objects.Factory.ObjectFactoryUtils.ObjectsOfTypeIncludingAncestors(IListableObjectFactory factory, Type type, Boolean includePrototypes, Boolean includeFactoryObjects) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Objects\Factory\ObjectFactoryUtils.cs:268
   Spring.Dao.Support.PersistenceExceptionTranslationInterceptor.DetectPersistenceExceptionTranslators(IListableObjectFactory objectFactory) in c:\_prj\spring-net\trunk\src\Spring\Spring.Data\Dao\Support\PersistenceExceptionTranslationInterceptor.cs:155
   Spring.Dao.Support.PersistenceExceptionTranslationInterceptor..ctor(IListableObjectFactory objectFactory) in c:\_prj\spring-net\trunk\src\Spring\Spring.Data\Dao\Support\PersistenceExceptionTranslationInterceptor.cs:73
   Spring.Dao.Attributes.PersistenceExceptionTranslationAdvisor..ctor(IListableObjectFactory objectFactory, Type repositoryAttributeType) in c:\_prj\spring-net\trunk\src\Spring\Spring.Data\Dao\Attributes\PersistenceExceptionTranslationAdvisor.cs:63
   Spring.Dao.Attributes.PersistenceExceptionTranslationPostProcessor.set_ObjectFactory(IObjectFactory value) in c:\_prj\spring-net\trunk\src\Spring\Spring.Data\Dao\Attributes\PersistenceExceptionTranslationPostProcessor.cs:95
   Spring.Objects.Factory.Support.AbstractAutowireCapableObjectFactory.ConfigureObject(String name, RootObjectDefinition definition, IObjectWrapper wrapper) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Objects\Factory\Support\AbstractAutowireCapableObjectFactory.cs:1886
   Spring.Objects.Factory.Support.WebObjectFactory.ConfigureObject(String name, RootObjectDefinition definition, IObjectWrapper wrapper) in c:\_prj\spring-net\trunk\src\Spring\Spring.Web\Objects\Factory\Support\WebObjectFactory.cs:450
   Spring.Objects.Factory.Support.AbstractAutowireCapableObjectFactory.InstantiateObject(String name, RootObjectDefinition definition, Object[] arguments, Boolean allowEagerCaching, Boolean suppressConfigure) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Objects\Factory\Support\AbstractAutowireCapableObjectFactory.cs:930
   Spring.Objects.Factory.Support.AbstractObjectFactory.CreateAndCacheSingletonInstance(String objectName, RootObjectDefinition objectDefinition, Object[] arguments) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Objects\Factory\Support\AbstractObjectFactory.cs:2144
   Spring.Objects.Factory.Support.WebObjectFactory.CreateAndCacheSingletonInstance(String objectName, RootObjectDefinition objectDefinition, Object[] arguments) in c:\_prj\spring-net\trunk\src\Spring\Spring.Web\Objects\Factory\Support\WebObjectFactory.cs:299
   Spring.Objects.Factory.Support.AbstractObjectFactory.GetObjectInternal(String name, Type requiredType, Object[] arguments, Boolean suppressConfigure) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Objects\Factory\Support\AbstractObjectFactory.cs:2065
   Spring.Objects.Factory.Support.AbstractObjectFactory.GetObject(String name) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Objects\Factory\Support\AbstractObjectFactory.cs:1826
   Spring.Objects.Factory.Support.DefaultListableObjectFactory.GetObjectsOfType(Type type, Boolean includePrototypes, Boolean includeFactoryObjects) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Objects\Factory\Support\DefaultListableObjectFactory.cs:770
   Spring.Context.Support.AbstractApplicationContext.GetObjectsOfType(Type type, Boolean includePrototypes, Boolean includeFactoryObjects) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Context\Support\AbstractApplicationContext.cs:1383
   Spring.Context.Support.AbstractApplicationContext.RegisterObjectPostProcessors(IConfigurableListableObjectFactory objectFactory) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Context\Support\AbstractApplicationContext.cs:652
   Spring.Context.Support.AbstractApplicationContext.Refresh() in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Context\Support\AbstractApplicationContext.cs:1000
   Spring.Context.Support.WebApplicationContext..ctor(WebApplicationContextArgs args) in c:\_prj\spring-net\trunk\src\Spring\Spring.Web\Context\Support\WebApplicationContext.cs:125
   Spring.Context.Support.WebApplicationContext..ctor(String name, Boolean caseSensitive, String[] configurationLocations) in c:\_prj\spring-net\trunk\src\Spring\Spring.Web\Context\Support\WebApplicationContext.cs:82
   _dynamic_Spring.Context.Support.WebApplicationContext..ctor(Object[] ) in c:\_prj\spring-net\trunk\src\Spring\Spring.Web\Caching\AspNetCache.cs:126
   Spring.Reflection.Dynamic.SafeConstructor.Invoke(Object[] arguments) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Reflection\Dynamic\DynamicConstructor.cs:116
   Spring.Context.Support.RootContextInstantiator.InvokeContextConstructor(ConstructorInfo ctor) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Context\Support\ContextHandler.cs:563
   Spring.Context.Support.ContextInstantiator.InstantiateContext() in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Context\Support\ContextHandler.cs:508
   Spring.Context.Support.ContextHandler.InstantiateContext(IApplicationContext parentContext, Object configContext, String contextName, Type contextType, Boolean caseSensitive, String[] resources) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Context\Support\ContextHandler.cs:351
   Spring.Context.Support.WebContextHandler.InstantiateContext(IApplicationContext parent, Object configContext, String contextName, Type contextType, Boolean caseSensitive, String[] resources) in c:\_prj\spring-net\trunk\src\Spring\Spring.Web\Context\Support\WebContextHandler.cs:127
   Spring.Context.Support.ContextHandler.Create(Object parent, Object configContext, XmlNode section) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Context\Support\ContextHandler.cs:289

[ConfigurationErrorsException: Error creating context 'spring.root': Could not configure datastore from input stream ]
   System.Configuration.BaseConfigurationRecord.EvaluateOne(String[] keys, SectionInput input, Boolean isTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult) +199
   System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult, Boolean getLkg, Boolean getRuntimeObject, Object& result, Object& resultRuntimeObject) +1153
   System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject) +1468
   System.Configuration.BaseConfigurationRecord.GetSection(String configKey) +41
   System.Web.HttpContext.GetSection(String sectionName) +52
   System.Web.Configuration.HttpConfigurationSystem.GetSection(String sectionName) +57
   System.Web.Configuration.HttpConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String configKey) +6
   System.Configuration.ConfigurationManager.GetSection(String sectionName) +78
   Spring.Util.ConfigurationUtils.GetSection(String sectionName) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Util\ConfigurationUtils.cs:71
   Spring.Context.Support.WebApplicationContext.GetContextInternal(String virtualPath) in c:\_prj\spring-net\trunk\src\Spring\Spring.Web\Context\Support\WebApplicationContext.cs:335
   Spring.Context.Support.WebApplicationContext.GetRootContext() in c:\_prj\spring-net\trunk\src\Spring\Spring.Web\Context\Support\WebApplicationContext.cs:223
   Spring.Context.Support.WebSupportModule.Init(HttpApplication app) in c:\_prj\spring-net\trunk\src\Spring\Spring.Web\Context\Support\WebSupportModule.cs:175
   System.Web.HttpApplication.InitModulesCommon() +172
   System.Web.HttpApplication.InitModules() +43
   System.Web.HttpApplication.InitInternal(HttpContext context, HttpApplicationState state, MethodInfo[] handlers) +828
   System.Web.HttpApplicationFactory.GetNormalApplicationInstance(HttpContext context) +304
   System.Web.HttpApplicationFactory.GetApplicationInstance(HttpContext context) +107
   System.Web.HttpRuntime.ProcessRequestInternal(HttpWorkerRequest wr) +327

在这里你可以看到:

[ObjectCreationException: Error creating object with name
‘NHibernateSessionFactory’ defined in ‘file
[D:\Code\EMSApplication\EMSApplication\Web.xml] line 14’ :
Initialization of object failed : Could not configure datastore from
input stream ]

所以我无法弄清楚如何解决这个问题.任何帮助或建议我真的很感激.

谢谢.

N.B.:我没有更改IIS 7中的任何设置.

编辑:

这些是使用的参考:

解决方法:

汇编://EMSApplication/Domain/EMSApplication.hbm.xml是嵌入式资源(VS – >属性 – >构建操作 – >嵌入式资源)?

但是,我不建议指向嵌入式资源,而是指向@Marijn在前一篇文章中已经指出的程序集:

<propertyname="MappingAssemblies">
  <list>
    <value>EMSApplication.Domain</value> <-- Assembly name
  </list>
</property

标签:c,asp-net,nhibernate,spring-net,mapping-resources
来源: https://codeday.me/bug/20190621/1250603.html