简体   繁体   English

为什么我在调用ContextRegistry.GetContext()时从Spring.NET中获得异常?

[英]Why am I getting an exception raised from Spring.NET on the call to ContextRegistry.GetContext()?

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文档并且无法看到我做错了什么 - 但我显然有错误,否则它不会引发异常!

AlbumLibraryWPF is the namespace and AlbumLibraryWPF.AlbumLibrary is the fully qualified name of the class I want to instantiate. AlbumLibraryWPF是名称空间, AlbumLibraryWPF.AlbumLibrary是我想要实例化的类的完全限定名称。 I'm guessing that it's this I've got wrong, but can't see how. 我猜这就是我错了,但看不出怎么样。

I feel such a fool. 我觉得这么傻。

It was because I'd failed to copy the AlbumLibrary.dll to the correct output directory. 这是因为我无法将AlbumLibrary.dll复制到正确的输出目录。 That meant that Spring couldn't find it - even after I'd fixed the assembly name problem Kent highlighted. 这意味着Spring无法找到它 - 即使在我修复了Kent突出显示的程序集名称问题之后。

I was getting this error because by mistake there was a typo [!*2] in app.config file. 我收到此错误是因为错误地在app.config文件中有拼写错误[!* 2]。 Once I took that out , error went away. 一旦我把它拿出来,错误就消失了。 some thing like this 这样的事情

<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

逗号后面的名称应该是程序集名称,它不一定与名称空间名称相同。

You should use tha id attribute instead of name : 您应该使用tha id属性而不是name

<object id="mediaLibrary" type="AlbumLibraryWPF.AlbumLibrary, AlbumLibraryWPF"/>

Also it should be config://spring/objects instead of config://spring/obects . 它也应该是config://spring/objects而不是config://spring/obects

You need to double check that you have a type called AlbumLibrary in AlbumLibraryWPF namespace defined in AlbumLibraryWPF assembly. 您需要仔细检查AlbumLibraryWPF程序AlbumLibraryWPF定义的AlbumLibraryWPF命名空间中是否存在名为AlbumLibrary的类型。

  1. Open VS2012 or VS2010 with Administrator Permissions 使用管理员权限打开VS2012或VS2010
  2. Config: type="namespace.type, assembly" 配置:type =“namespace.type,assembly”

Then try running your solution again. 然后再次尝试运行解决方案。

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”,第一个参数表示NameSpace,第二个参数(点后面)表示解决方案名称。

  • "AlbumLibraryWPF.AlbumLibrary" = NameSapce name “AlbumLibraryWPF.AlbumLibrary”= NameSapce名称
  • "AlbumLibraryWPF" = solution name “AlbumLibraryWPF”=解决方案名称

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM