2009-02-10 58 views
0

我跟着Windsor Inversion of Control (IoC) Getting Started example,這是在C#中,但我在VB.Net中實現它,我遇到了一個小問題。Windsor IoC示例幫助 - 「HttpServiceWatcher&未註冊。」

這是我得到的全部例外:

無法創建組件'form.component',因爲它具有滿足的依賴關係。 form.component正在等待以下依賴性:

服務: - InversionOfControl.HttpServiceWatcher &這是未註冊。

但我想我正在註冊它 - 這是第一個註冊!

我使用VB 8(Visual Studio 2005/.Net 2.0)和Windsor 1.0 RC3


這裏是我的App.vb

 
Imports Castle.Windsor 

Public Class App 

    Public Shared Sub Main() 

     Dim container As New WindsorContainer 

     'register the components 
     container.AddComponent("httpservicewatcher", _ 
      GetType(HttpServiceWatcher)) 
     container.AddComponent("email.notifier", GetType(IFailureNotifier), _ 
      GetType(EmailFailureNotifier)) 
     container.AddComponent("alarm.notifier", GetType(IFailureNotifier), _ 
      GetType(AlarmFailureNotifier)) 
     container.AddComponent("form.component", GetType(Form1)) 

     'request the component from the container 
     Dim aForm As Form = container(GetType(Form1)) 

     'use it! 
     Application.Run(aForm) 

     'release it 
     container.Release(aForm) 

    End Sub 

End Class 

Form1中

 
Public Class Form1 

    Private oServiceWatcher As HttpServiceWatcher 

    Sub New(ByRef ServiceWatcher As HttpServiceWatcher) 

     ' This call is required by the Windows Form Designer. 
     InitializeComponent() 

     ' Add any initialization after the InitializeComponent() call. 
     Me.oServiceWatcher = ServiceWatcher 
    End Sub 
End Class 

HttpServiceWatcher

 
Public Class HttpServiceWatcher 

    Private oNotifier As IFailureNotifier 

    Sub New(ByRef Notifier As IFailureNotifier) 
     oNotifier = Notifier 
    End Sub 

    Sub StartWatching() 

     'should start a thread to ping the service 
     'if (pingresult = Failed) 
     oNotifier.Notify() 
     'end if 

    End Sub 

    Sub StopWatching() 

     'stop thread 

    End Sub 

End Class 

IFailureNotifier

 
Public Interface IFailureNotifier 

    Sub Notify() 

End Interface 

AlarmFailureNotifierEmailFailureNotifier都實現IFailureNotifier但


我已經嘗試通過將IFailureNotifier第一,HttpServiceWatcher改變順序Notify()方法爲空第三和形式最後但我得到同樣的錯誤。

我已經做了一個清潔和重建,但我得到了同樣的錯誤。

我明顯是新手(正如我正在經歷'入門'),你能指出我錯過了什麼嗎?

感謝您:O)

回答

1

我不是一個VB的行家,但我懷疑的問題是,在新的子ByRef關鍵字。嘗試將其更改爲:

Public Class Form1 

    Private oServiceWatcher As HttpServiceWatcher 

    Sub New(ServiceWatcher As HttpServiceWatcher) 

     ' This call is required by the Windows Form Designer. 
     InitializeComponent() 

     ' Add any initialization after the InitializeComponent() call. 
     Me.oServiceWatcher = ServiceWatcher 
    End Sub 
End Class 
+0

謝謝你的提示 - 當我刪除了爲「ByRef」它得到了與「BYVAL」替換(我認爲這必須是一個或另一個,但我給它什麼也沒有一試無論如何!),我改變了項目中的兩個構造函數:Form1和HttpServiceWatcher,並對其進行排序。再次感謝:o) – Andrew 2009-02-10 21:17:04