运行代码
string name = txtname.Text.ToString().Trim();
string pwd = txtpwd.Text.ToString().Trim();
IApplicationContext cxt = ContextRegistry.GetContext();
UserManager userManager = (UserManager)cxt.GetObject("UserManager");
Model.User user = new User();
user.Name = name;
user.Pwd = pwd;
userManager.SaveUser(user);
配置文件:dao.xml
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net"
xmlns:db="http://www.springframework.net/database">
<description>
Definitions for the NHibernate Objects.
</description>
<!-- Database and NHibernate Configuration这下面是配置使用Nhibernate -->
<db:provider id="DbProvider"
provider="SqlServer-2.0"
connectionString="database=Test;uid=sa;pwd=szs;server=.;"/>
<object id="SessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate32">
<property name="DbProvider" ref="DbProvider" />
<property name="MappingAssemblies">
<list>
<!--这是配置嵌入资源的xx类对应的xx.hbm.xml文件所在的项目名称-->
<value>Model</value>
</list>
</property>
<property name="HibernateProperties">
<dictionary>
<entry key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
<entry key="hibernate.dialect" value="NHibernate.Dialect.MsSql2005Dialect" />
<entry key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
<entry key="show_sql" value="true" />
</dictionary>
</property>
</object>
<object id="HibernateTransactionManager" type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate32">
<property name="DbProvider" ref="DbProvider" />
<property name="sessionFactory" ref="SessionFactory" />
</object>
<object id="TransactionInterceptor" type="Spring.Transaction.Interceptor.TransactionInterceptor, Spring.Data">
<property name="TransactionManager" ref="HibernateTransactionManager" />
<property name="TransactionAttributeSource">
<object type="Spring.Transaction.Interceptor.AttributesTransactionAttributeSource, Spring.Data" />
</property>
</object>
</objects>
Service.xml
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
<object id="DepManager" >
<property name="Target">
<object type="Manager.DepManager,Manager">
<property name="DaoDep" ref="Dao.Dep"/>
</object>
</property>
</object>
<object id="UserManager" >
<property name="Target">
<object type="Manager.UserManager,Manager">
<property name="DaoUser" ref="Dao.User"/>
</object>
</property>
</object>
</objects>
堆栈信息:
源错误:
行 55: string pwd = txtpwd.Text.ToString().Trim();
行 56:
行 57: IApplicationContext cxt = ContextRegistry.GetContext();
行 58: UserManager userManager = (UserManager)cxt.GetObject("UserManager");
行 59:
源文件: d:\MyProgram\Asp.net\Nhibernate\UI\Default.aspx.cs 行: 57
堆栈跟踪:
[ArgumentException: 已添加了具有相同键的项。]
System.ThrowHelper.ThrowArgumentException(ExceptionResource resource) +51
System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) +7464876
System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value) +11
NHibernate.Cfg.Configuration.AddProperties(IDictionary`2 additionalProperties) +86
Spring.Data.NHibernate.LocalSessionFactoryObject.AfterPropertiesSet() +1606
Spring.Objects.Factory.Support.AbstractAutowireCapableObjectFactory.InvokeInitMethods(Object target, String name, IConfigurableObjectDefinition definition) +204
Spring.Objects.Factory.Support.AbstractAutowireCapableObjectFactory.ConfigureObject(String name, RootObjectDefinition definition, IObjectWrapper wrapper) +516
Spring.Objects.Factory.Support.WebObjectFactory.ConfigureObject(String name, RootObjectDefinition definition, IObjectWrapper wrapper) +89
Spring.Objects.Factory.Support.AbstractAutowireCapableObjectFactory.InstantiateObject(String name, RootObjectDefinition definition, Object[] arguments, Boolean allowEagerCaching, Boolean suppressConfigure) +601
[ObjectCreationException: Error creating object with name 'NHibernateSessionFactory' defined in 'URL [http://localhost:11845/UI/Configs/Dao.xml] line 11' : Initialization of object failed : 已添加了具有相同键的项。]
Spring.Objects.Factory.Support.AbstractAutowireCapableObjectFactory.InstantiateObject(String name, RootObjectDefinition definition, Object[] arguments, Boolean allowEagerCaching, Boolean suppressConfigure) +744
Spring.Objects.Factory.Support.AbstractObjectFactory.CreateAndCacheSingletonInstance(String objectName, RootObjectDefinition objectDefinition, Object[] arguments) +180
Spring.Objects.Factory.Support.WebObjectFactory.CreateAndCacheSingletonInstance(String objectName, RootObjectDefinition objectDefinition, Object[] arguments) +150
Spring.Objects.Factory.Support.AbstractObjectFactory.GetObjectInternal(String name, Type requiredType, Object[] arguments, Boolean suppressConfigure) +1065
Spring.Objects.Factory.Support.DefaultListableObjectFactory.PreInstantiateSingletons() +478
Spring.Context.Support.AbstractApplicationContext.Refresh() +933
Spring.Context.Support.WebApplicationContext..ctor(WebApplicationContextArgs args) +99
_dynamic_Spring.Context.Support.WebApplicationContext..ctor(Object[] ) +207
Spring.Reflection.Dynamic.SafeConstructor.Invoke(Object[] arguments) +12
Spring.Context.Support.RootContextInstantiator.InvokeContextConstructor(ConstructorInfo ctor) +136
Spring.Context.Support.ContextInstantiator.InstantiateContext() +26
Spring.Context.Support.ContextHandler.InstantiateContext(IApplicationContext parentContext, Object configContext, String contextName, Type contextType, Boolean caseSensitive, String[] resources) +130
Spring.Context.Support.WebContextHandler.InstantiateContext(IApplicationContext parent, Object configContext, String contextName, Type contextType, Boolean caseSensitive, String[] resources) +266
Spring.Context.Support.ContextHandler.Create(Object parent, Object configContext, XmlNode section) +275
[ConfigurationErrorsException: Error creating context '/ui': 已添加了具有相同键的项。]
System.Configuration.BaseConfigurationRecord.EvaluateOne(String[] keys, SectionInput input, Boolean isTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult) +202
System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult, Boolean getLkg, Boolean getRuntimeObject, Object& result, Object& resultRuntimeObject) +1061
System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject) +1431
System.Configuration.BaseConfigurationRecord.GetSection(String configKey, Boolean getLkg, Boolean checkPermission) +56
System.Configuration.BaseConfigurationRecord.GetSection(String configKey) +8
System.Web.HttpContext.GetSection(String sectionName) +47
System.Web.Configuration.HttpConfigurationSystem.GetSection(String sectionName) +39
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) +88
Spring.Context.Support.ContextRegistry.InitializeContextIfNeeded() +50
Spring.Context.Support.ContextRegistry.GetContext() +46
_Default.btnSubmit_Click(Object sender, EventArgs e) in d:\MyProgram\Asp.net\Nhibernate\UI\Default.aspx.cs:57
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +6741
System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +242
System.Web.UI.Page.ProcessRequest() +80
System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) +21
System.Web.UI.Page.ProcessRequest(HttpContext context) +49
ASP.default_aspx.ProcessRequest(HttpContext context) in c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\ui\acf12ce8\6701c690\App_Web_ra9ri1wx.0.cs:0
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75