Start run twice when implementing Autofac.IStartable

112 views
Skip to first unread message

Jason Hickey

unread,
May 7, 2014, 7:58:30 PM5/7/14
to topshelf...@googlegroups.com
Hi everyone, and thanks in advance for any assistance.

In following with practice seen around quite a few examples that use Autofac injection, I've noticed the above mentioned behaviour. Implementing IStartable means Start() will be fired upon the container being built. Cool.
Then when I create the host as follows

var host = HostFactory.New(x =>
                {
                    x.Service<IStartable>(s =>
                    {
                        s.ConstructUsing(name => container.Resolve<IStartable>());
                        s.WhenStarted(tc => tc.Start());
                        s.WhenStopped(_ =>
                        {
                            Console.WriteLine("Service is stopping");
                            container.Dispose();
                        });
                    });
                    x.RunAsLocalSystem();
                });

Start() gets hit again - no problem, I understand why here too.

Having said that, is it just numerous examples out in the wild that are ignoring this fact, and let it run twice, when in fact they should either

a) not be calling Start() in the WhenStarted callback when implementing Autofac.IStartable; or
b) not be implementing Autofac.IStartable, and instead create their own interface for resolution in ConstructUsing, and hit anything you want at startup via the Topshelf WhenStartedCallback

Again, thanks for any push in a preferable direction.

Chris Patterson

unread,
May 8, 2014, 4:21:19 PM5/8/14
to topshelf...@googlegroups.com
I wouldn't be using IStartable with Autofac/Topshelf. Let Topshelf manage the service lifecycle.



--
You received this message because you are subscribed to the Google Groups "topshelf-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to topshelf-discu...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages