C# 为什么我会在调用 ContextRegistry.GetContext() 时从 Spring.NET 收到异常?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/765616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why am I getting an exception raised from Spring.NET on the call to ContextRegistry.GetContext()?
提问by ChrisF
Even though the solution is so obvious I should have never have posted this, I'm leaving it up as a reminder and a useful point of reference to others.
尽管解决方案是如此明显,我不应该发布此内容,但我还是将其留作提醒和对其他人有用的参考点。
I've got the following in my app.config file:
我的 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>
Followed by:
其次是:
<spring>
<context>
<resource uri="config://spring/objects"/>
</context>
<objects xmlns="http://www.springframework.net">
<object name="mediaLibrary" type="AlbumLibraryWPF.AlbumLibrary, AlbumLibraryWPF"/>
</objects>
</spring>
Then in my app I've got:
然后在我的应用程序中,我有:
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
}
It all compiles quite nicely, however, I'm getting an exception raised on the call to GetContext():
这一切都编译得很好,但是,我在调用 GetContext() 时遇到了异常:
Error creating context 'spring.root': Could not load type from string value
'AlbumLibraryWPF.AlbumLibrary, AlbumLibraryWPF'.
I've checked the Spring.NET documentation and can't see what I'm doing wrong - but I clearly have got something wrong, otherwise it wouldn't raise the exception!
我已经检查了 Spring.NET 文档并且看不出我做错了什么 - 但我显然有问题,否则它不会引发异常!
AlbumLibraryWPFis the namespace and AlbumLibraryWPF.AlbumLibraryis the fully qualified name of the class I want to instantiate. I'm guessing that it's this I've got wrong, but can't see how.
AlbumLibraryWPF是命名空间,AlbumLibraryWPF.AlbumLibrary是我要实例化的类的完全限定名称。我猜这是我错了,但不知道怎么做。
采纳答案by ChrisF
I feel such a fool.
我觉得自己是个傻瓜。
It was because I'd failed to copy the AlbumLibrary.dll to the correct output directory. That meant that Spring couldn't find it - even after I'd fixed the assembly name problem Kent highlighted.
这是因为我未能将 AlbumLibrary.dll 复制到正确的输出目录。这意味着 Spring 找不到它 - 即使在我修复了 Kent 突出显示的程序集名称问题之后。
回答by Darin Dimitrov
You should use tha idattribute instead of name:
您应该使用 thaid属性而不是name:
<object id="mediaLibrary" type="AlbumLibraryWPF.AlbumLibrary, AlbumLibraryWPF"/>
Also it should be config://spring/objectsinstead of config://spring/obects.
它也应该config://spring/objects代替config://spring/obects.
You need to double check that you have a type called AlbumLibraryin AlbumLibraryWPFnamespace defined in AlbumLibraryWPFassembly.
您需要仔细检查您是否在程序AlbumLibrary集中AlbumLibraryWPF定义的命名空间中调用了一个类型AlbumLibraryWPF。
回答by Kent Boogaart
The name after the comma should be the assembly name, which is not necessarily the same as the namespace name.
逗号后面的名称应该是程序集名称,它不一定与命名空间名称相同。
回答by jason
You can try change the type. The type="AlbumLibraryWPF.AlbumLibrary, AlbumLibraryWPF", first parameter means NameSpace and the second parameter (behind the dot) means Solution Name.
您可以尝试更改类型。type="AlbumLibraryWPF.AlbumLibrary, AlbumLibraryWPF",第一个参数表示名称空间,第二个参数(点后面)表示解决方案名称。
- "AlbumLibraryWPF.AlbumLibrary" = NameSapce name
- "AlbumLibraryWPF" = solution name
- "AlbumLibraryWPF.AlbumLibrary" = NameSapce 名称
- "AlbumLibraryWPF" = 解决方案名称
回答by leo
- Open VS2012 or VS2010 with Administrator Permissions
- Config: type="namespace.type, assembly"
- 以管理员权限打开 VS2012 或 VS2010
- 配置:type="namespace.type, assembly"
Then try running your solution again.
然后再次尝试运行您的解决方案。
回答by user2704320
I was getting this error because by mistake there was a typo [!*2] in app.config file. Once I took that out , error went away. some thing like this
我收到这个错误是因为 app.config 文件中错误地有一个错字 [!*2]。一旦我把它拿出来,错误就消失了。像这样的事情
<context>
<!--<resource uri="~//Aspects.xml"/>-->
<!--<resource uri="~//Dao.xml"/>-->
<!--<resource uri="~//Spring.xml"/>-->
<resource uri="file://Spring.xml"/>
<resource uri="file://Dao.xml"/>
</context>
!*2
!*2

