无法从字符串值加载类型

时间:2012-11-27 17:45:31

标签: c# .net spring spring.net

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

  

创建上下文'spring.root'时出错:无法从字符串值'BlackOpsP2.Data.EmployeeDao,BlackOpsP2.Data'加载类型。

的app.config:

<?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.Core
  2. ABC.Data
  3. ABC.Presentation
  4. ABC.Services
  5. ABC.UI
  6. 我在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>
    

    错误消息更改为:

      

    创建上下文'spring.root'时出错:无法加载文件或程序集“BlackOpsP2.Data”或其依赖项之一。系统找不到指定的文件。

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

1 个答案:

答案 0 :(得分:6)

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

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