c# - 无法从字符串值加载类型

标签 c# .net spring spring.net

我正在开发一个使用 NHibernate 和 Spring.Net 框架的程序。当我执行应用程序时,抛出以下异常:

Error creating context 'spring.root': Could not load type from string value 'BlackOpsP2.Data.EmployeeDao, BlackOpsP2.Data'.

应用程序配置:

<?xml version="1.0"?>
<configuration> 
    <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821"/>
        <sectionGroup name="spring">
            <section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core" />
            <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
            <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
        </sectionGroup>
    </configSections>

    <spring>
        <context>
            <resource uri="config://spring/objects"/>
        </context>
        <parsers>
            <parser type="Spring.Data.Config.DatabaseNamespaceParser, Spring.Data" />
            <parser type="Spring.Transaction.Config.TxNamespaceParser, Spring.Data"/>
        </parsers>

        <objects xmlns="http://www.springframework.net"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:tx="http://www.springframework.net/tx"
             xmlns:db="http://www.springframework.net/database">

            <db:provider id="DbProvider"
                           provider="SqlServer-2.0"
                           connectionString="Data Source=CHETUIWK123\SQL2008R2;Integrated Security=true;Database=TestNhibernate;"/>
            <object id="MySessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate33">
                <property name="ExposeTransactionAwareSessionFactory" value="true" />
                <property name="DbProvider" ref="DbProvider"/>
                <property name="MappingAssemblies">
                    <list>
                        <value>BlackOpsP2.Core</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="hibernate.connection.driver_class"
                               value="NHibernate.Driver.SqlClientDriver"/>

                    </dictionary>
                </property>
            </object>

            <object id="HibernateTransactionManager"
            type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate33">
                <property name="SessionFactory" ref="MySessionFactory"/>
            </object>

            <object id="EmployeeDao" xsi:schemaLocation="" type="BlackOpsP2.Data.EmployeeDao, BlackOpsP2.Data">
                <property name="SessionFactory" ref="MySessionFactory"/>
            </object>

            <object id="EmployeeService" type="BlackOpsP2.Services.ARModule.EmployeeService, BlackOpsP2.Services">
                <property name="TransactionManager" ref="HibernateTransactionManager"/>
                <property name="Dao" ref="EmployeeDao"/>
            </object>

            <object id="EmployeeController" type="BlackOpsP2.Presentation.Controller.ARModule.EmployeeController, BlackOpsP2.Presentation">
                <property name="Service" ref="EmployeeService"/>
            </object>

            <!--> manually added -->
            <object id="Form" type="BlackOpsP2.UI.Form1, BlackOpsP2.UI">
                <property name="Controller" ref="EmployeeController"/>
            </object>
            <!--> /****/ -->

            <tx:attribute-driven transaction-manager="HibernateTransactionManager"/>
        </objects>
    </spring>

    <log4net>
        <appender name="FileAppender" type="log4net.Appender.FileAppender,log4net">
            <file value="E:\Test\log.txt"/>
            <appendToFile value="true"/>
            <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date [%thread] %level %logger - %message%newline"/>
            </layout>
            <filter type="log4net.Filter.LevelRangeFilter">
                <levelMin value="INFO"/>
                <levelMax value="FATAL"/>
            </filter>
        </appender>

        <root>
            <level value="DEBUG"/>
            <appender-ref ref="FileAppender"/>
        </root>
    </log4net>
</configuration>

我实现了以下目录结构:

  1. ABC.核心
  2. ABC.Data
  3. ABC.Presentation
  4. ABC.Services
  5. ABC.UI

我在 stackoverflow 和 google 上浏览了很多链接,但我无法找到解决我问题的相关信息。

不知道为什么会抛出这个异常,也不知道如何解决。我认为这可能与输出目录有关,或者 Spring Framework 找不到 dll。

详细跟踪异常:

System.Configuration.ConfigurationErrorsException was unhandled  
Message=Error creating context 'spring.root': Could not load type from
string value 'BlackOpsP2.Data.EmployeeDao, BlackOpsP2.Data'.  
Source=System.Configuration   BareMessage=Error creating context
'spring.root': Could not load type from string value
'BlackOpsP2.Data.EmployeeDao, BlackOpsP2.Data'.   Line=0   StackTrace:
       at System.Configuration.BaseConfigurationRecord.EvaluateOne(String[]
keys, SectionInput input, Boolean isTrusted, FactoryRecord
factoryRecord, SectionRecord sectionRecord, Object parentResult)
       at System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord
factoryRecord, SectionRecord sectionRecord, Object parentResult,
Boolean getLkg, Boolean getRuntimeObject, Object& result, Object&
resultRuntimeObject)
       at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String
configKey, Boolean getLkg, Boolean checkPermission, Boolean
getRuntimeObject, Boolean requestIsHere, Object& result, Object&
resultRuntimeObject)
       at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String
configKey, Boolean getLkg, Boolean checkPermission, Boolean
getRuntimeObject, Boolean requestIsHere, Object& result, Object&
resultRuntimeObject)
       at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String
configKey, Boolean getLkg, Boolean checkPermission, Boolean
getRuntimeObject, Boolean requestIsHere, Object& result, Object&
resultRuntimeObject)
       at System.Configuration.BaseConfigurationRecord.GetSection(String
configKey)
       at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String
sectionName)
       at System.Configuration.ConfigurationManager.GetSection(String sectionName)
       at Spring.Util.ConfigurationUtils.GetSection(String sectionName)
       at Spring.Context.Support.ContextRegistry.InitializeContextIfNeeded()
       at Spring.Context.Support.ContextRegistry.GetContext()
       at BlackOpsP2.UI.Program.Main() in E:\Project\BlackOps-P2\BlackOpsP2.UI\Program.cs:line 19
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean
ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()   InnerException: Spring.Core.CannotLoadObjectTypeException
       Message=Cannot resolve type [BlackOpsP2.Data.EmployeeDao, BlackOpsP2.Data] for object with name 'EmployeeDao' defined in config
[E:\Project\Bin\BlackOps-P2\BlackOpsP2.UI\BlackOpsP2.UI.vshost.exe.Config#spring/objects]
line 1
       Source=Spring.Core
       ObjectName=EmployeeDao
       ObjectTypeName=BlackOpsP2.Data.EmployeeDao, BlackOpsP2.Data
       ResourceDescription=config [E:\Project\Bin\BlackOps-P2\BlackOpsP2.UI\BlackOpsP2.UI.vshost.exe.Config#spring/objects]
line 1
       StackTrace:
            at Spring.Objects.Factory.Support.AbstractObjectFactory.ResolveObjectType(RootObjectDefinition
rod, String objectName)
            at Spring.Objects.Factory.Support.DefaultListableObjectFactory.PreInstantiateSingletons()
            at Spring.Context.Support.AbstractApplicationContext.Refresh()
            at Spring.Context.Support.XmlApplicationContext..ctor(XmlApplicationContextArgs
args)
            at Spring.Context.Support.XmlApplicationContext..ctor(String name,
Boolean caseSensitive, String[] configurationLocations)
            at _dynamic_Spring.Context.Support.XmlApplicationContext..ctor(Object[] )
            at Spring.Reflection.Dynamic.SafeConstructor.Invoke(Object[] arguments)
            at Spring.Context.Support.ContextHandler.RootContextInstantiator.InvokeContextConstructor(ConstructorInfo
ctor)
            at Spring.Context.Support.ContextHandler.ContextInstantiator.InstantiateContext()
            at Spring.Context.Support.ContextHandler.InstantiateContext(IApplicationContext
parentContext, Object configContext, String contextName, Type
contextType, Boolean caseSensitive, IList`1 resources)
            at Spring.Context.Support.ContextHandler.Create(Object parent, Object configContext, XmlNode section)
       InnerException: System.TypeLoadException
            Message=Could not load type from string value 'BlackOpsP2.Data.EmployeeDao, BlackOpsP2.Data'.
            Source=Spring.Core
            TypeName=""
            StackTrace:
                 at Spring.Core.TypeResolution.TypeResolver.Resolve(String typeName)
                 at Spring.Core.TypeResolution.GenericTypeResolver.Resolve(String
typeName)
                 at Spring.Core.TypeResolution.CachedTypeResolver.Resolve(String typeName)
                 at Spring.Core.TypeResolution.TypeResolutionUtils.ResolveType(String
typeName)
                 at Spring.Objects.Factory.Support.AbstractObjectDefinition.ResolveObjectType()
                 at Spring.Objects.Factory.Support.AbstractObjectFactory.ResolveObjectType(RootObjectDefinition
rod, String objectName)
            InnerException:

更新

当我在 app.config 中添加以下行时:

<property name="MappingAssemblies">
                <list>
                    <value>BlackOpsP2.Core</value>
                    <value>BlackOpsP2.Data</value>
                </list>
</property>

错误信息变为:

Error creating context 'spring.root': Could not load file or assembly 'BlackOpsP2.Data' or one of its dependencies. The system cannot find the file specified.

有人知道为什么会这样吗?

最佳答案

我已经解决了这个问题。其实有两个小错误。

  1. 合格的实体名称不正确。
  2. 需要在UI dll中添加所有dll的引用。

关于c# - 无法从字符串值加载类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13590042/

相关文章:

C# 应用程序在切换到 TLS 1.2 后无法再访问网站 - GetResponse() 失败

c# - CombGuid 在 C# 和 SQL Server 中的差异

c# - 调用 Dynamic Links Analytics API 时调用者没有权限 [403]

spring - 如何在Spring Webflow中的最终状态externalredirect中设置一些参数?

java - 除了 Spring boot 一次输入的 Pageable 和 Sort 之外,对所有参数使用 @Param

java - Spring mvc 中有哪些 portlet 类?

c# - 无法在没有DataSource的DataGridView中将当前单元格设置为不可见的单元格

c# - 存储的字符串、区域设置资源文件、使用 C# 6 插值?

c# - 通过Docker在IIS上部署经典ASP应用程序

.net - 带有在线文档的 Specflow