1

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

私はstackoverflowとgoogleで多くのリンクをたどりましたが、問題を解決する関連情報を見つけることができませんでした。

この例外がスローされる理由がわかりません。また、それを解決する方法もわかりません。出力ディレクトリに関連するものか、SpringFrameworkが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つを読み込めませんでした。システムは、指定されたファイルを見つけることができません。

なぜそれが起こっているのか誰かが知っていますか?

4

1 に答える 1

6

私は問題を解決しました。実は、小さな間違いが 2 つあります。

  1. 修飾エンティティ名が正しくありません。
  2. UI dll にすべての dll の参照を追加する必要があります。
于 2012-11-28T12:29:24.673 に答える