簡體   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