2012-11-22 29 views
0

我下面給出的例子(攻擊舊模式):城堡溫莎StartableFacility不啓動

http://docs.castleproject.org/Default.aspx?Page=Startable-Facility&NS=Windsor&AspxAutoDetectCookieSupport=1

這裏是我的全部源代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using NUnit.Framework; 
using Castle.Facilities.Startable; 
using Castle.MicroKernel; 
using Castle.MicroKernel.Registration; 

namespace Test 
{ 
    public interface IStartable 
    { 
     void Start(); 
     void Stop(); 
    } 

    public class Startable : IStartable 
    { 
     public Startable() 
     { 
      Console.WriteLine("Created!"); 
     } 

     public void Start() 
     { 
      Console.WriteLine("Started!"); 
     } 

     public void Stop() 
     { 
      Console.WriteLine("Stopped!"); 
     } 
    } 

    [TestFixture] 
    public class StartableFacilityContainerTest 
    { 

     [Test] 
     public void TestOperation() 
     { 
      IKernel container = new DefaultKernel(); 

      container.AddFacility<StartableFacility>(); 

      container.Register(Component.For<Startable>()); 
      Console.WriteLine("Registered!"); 

      container.Dispose(); 
      Console.WriteLine("Released!"); 
     } 
    } 
} 

然而,當我運行,我得到:

Registered! 
Released! 

當我期望得到(如在例子中給出的):

Created! 
Started! 
Registered! 
Stopped! 
Released! 

基本上我的Startable沒有開始。

這在.NET 4.0和溫莎城堡3.0

測試

我做了什麼錯?

回答

0

嘗試

container.Register(Component.For<Startable>() 
    .StartUsingMethod(s => s.Start) 
    .StopUsingMethod(s => s.Stop); 
+0

乾杯。似乎舊模式不再受支持。 –

3

我使用的是安裝程序。這幫助了我:

container.AddFacility<StartableFacility>(f => f.DeferredTryStart()); 
+0

也適合我。 thnx兄弟分享 –

0

問題是你已經創建並實施自己的IStartable接口,而不是僅僅執行Castle.Core.IStartable