繁体   English   中英

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

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

即使解决方案如此明显,我也不应该发布这个,我将其作为提醒和其他人的有用参考点。

我的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突出显示的程序集名称问题之后。

我收到此错误是因为错误地在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

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

您应该使用tha id属性而不是name

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

它也应该是config://spring/objects而不是config://spring/obects

您需要仔细检查AlbumLibraryWPF程序AlbumLibraryWPF定义的AlbumLibraryWPF命名空间中是否存在名为AlbumLibrary的类型。

  1. 使用管理员权限打开VS2012或VS2010
  2. 配置:type =“namespace.type,assembly”

然后再次尝试运行解决方案。

您可以尝试更改类型。 type =“AlbumLibraryWPF.AlbumLibrary,AlbumLibraryWPF”,第一个参数表示NameSpace,第二个参数(点后面)表示解决方案名称。

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

暂无
暂无

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

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