0

我正在遵循以下示例(激进的旧模式):

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 和 Castle Windsor 3.0 中测试的

我做错什么了?

4

3 回答 3

4

我正在使用安装程序。这帮助了我:

container.AddFacility<StartableFacility>(f => f.DeferredTryStart());
于 2014-04-29T14:35:11.217 回答
0

尝试

container.Register(Component.For<Startable>()
     .StartUsingMethod(s => s.Start)
     .StopUsingMethod(s => s.Stop);
于 2012-11-22T16:02:32.107 回答
0

问题是您已经创建并实现了自己的IStartable界面,而不仅仅是实现Castle.Core.IStartable

于 2017-02-08T14:16:40.200 回答