Search code examples
c#.net-4.0castle-windsor

Castle Windsor StartableFacility does not start


I was following the example (Aggressive old mode) given in:

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

Here is my full source code:

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!");
        }
    }
}

However when I run it, I get:

Registered!
Released!

when I expect to get (as given in the example):

Created!
Started!
Registered!
Stopped!
Released!

Basically my Startable did not start.

This is tested in .Net 4.0 and Castle Windsor 3.0

What did I do wrong?


Solution

  • try

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