尽管解决方案非常明显,但我本不应该发布它,但我将其保留下来作为提醒和对其他人的有用引用点。
我的 app.config 文件中有以下内容:
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
其次是:
<spring>
<context>
<resource uri="config://spring/objects"/>
</context>
<objects xmlns="http://www.springframework.net">
<object name="mediaLibrary" type="AlbumLibraryWPF.AlbumLibrary, AlbumLibraryWPF"/>
</objects>
</spring>
然后在我的应用程序中我有:
using Spring.Context;
using Spring.Context.Support;
public partial class AlbumChecker : Window
{
private DataTable dataTable;
private Library library;
private Thread libraryThread;
public AlbumChecker()
{
InitializeComponent();
CreateToolTips();
IApplicationContext ctx = ContextRegistry.GetContext();
library = (Library)ctx.GetObject("mediaLibrary");
// Other initialisation
}
// Other code
}
一切都编译得很好,但是,我在调用 GetContext() 时遇到异常:
Error creating context 'spring.root': Could not load type from string value
'AlbumLibraryWPF.AlbumLibrary, AlbumLibraryWPF'.
我已经检查了 Spring.NET 文档,看不出我做错了什么 - 但我显然做错了什么,否则它不会引发异常!
AlbumLibraryWPF
是命名空间,AlbumLibraryWPF.AlbumLibrary
是我要实例化的类的完全限定名称。我猜是我弄错了,但看不出是怎么回事。
最佳答案
我觉得自己很傻。
这是因为我未能将 AlbumLibrary.dll 复制到正确的输出目录。这意味着 Spring 无法找到它 - 即使在我修复了 Kent 强调的程序集名称问题之后也是如此。
关于c# - 为什么我在调用 ContextRegistry.GetContext() 时收到 Spring.NET 引发的异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/765616/