简体   繁体   中英

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:

<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():

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!

AlbumLibraryWPF is the namespace and AlbumLibraryWPF.AlbumLibrary is 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.

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.

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

<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

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

You should use tha id attribute instead of name :

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

Also it should be config://spring/objects instead of config://spring/obects .

You need to double check that you have a type called AlbumLibrary in AlbumLibraryWPF namespace defined in AlbumLibraryWPF assembly.

  1. Open VS2012 or VS2010 with Administrator Permissions
  2. Config: 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.

  • "AlbumLibraryWPF.AlbumLibrary" = NameSapce name
  • "AlbumLibraryWPF" = solution name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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