代码之家  ›  专栏  ›  技术社区  ›  ChrisF

为什么在调用ContextRegistry.getContext()时从Spring.net引发异常?

  •  4
  • ChrisF  · 技术社区  · 15 年前

    尽管这个解决方案如此明显,我本不应该把它贴出来,但我把它留作提醒和参考他人的一个有用点。

    我的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 是要实例化的类的完全限定名。我猜是我错了,但不知道怎么回事。

    6 回复  |  直到 10 年前
        1
  •  5
  •   ChrisF    15 年前

    我觉得自己是个傻瓜。

    这是因为我未能将albumlibrary.dll复制到正确的输出目录。这意味着Spring找不到它——即使在我修复了Kent突出显示的程序集名称问题之后。

        2
  •  1
  •   Tim Cooper    13 年前

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

        3
  •  1
  •   user2704320    10 年前

    我得到这个错误是因为错误地有一个打字错误。*2]在app.config文件中。一旦我把它拿出来,错误就消失了。像这样的东西

    <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

        4
  •  0
  •   Darin Dimitrov    15 年前

    你应该用tha id 属性而不是 name :

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

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

    您需要再次检查是否有一个名为 AlbumLibrary 在里面 AlbumLibraryWPF 命名空间定义于 蛋白库 装配。

        5
  •  -1
  •   T.Rob    13 年前

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

    • “albumlibrarywpf.albumlibrary”=namesapce名称
    • “albumlibrarywpf”=解决方案名称
        6
  •  -1
  •   Scott Keith    12 年前
    1. 使用管理员权限打开VS2012或VS2010
    2. config:type=“namespace.type,程序集”

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