Question

I have created an instance of TCP listener (network socket) as a TopShelf Windows service. Since this project is a service with Main() and not a class library, I'm not certain how to create a constructor that will permit me to inject a callback.

Specifically, the idea is that when the TCP listener receives a message, it should invoke the injected callback method. Normally with a class library I would create a constructor taking as argument ICallback and instantiate accordingly.

Can I perform constructor injection with my TopShelf service? If so, how?

Was it helpful?

Solution

With something like this:

    HostFactory.New(x =>
                {
                    x.SetDisplayName("Your service");
                    x.SetServiceName("yourservice");
                    x.Service<MyService>(c =>
                    {
                        c.SetServiceName("My service");
                        c.ConstructUsing(name => container.Resolve<MyService>());
                        c.WhenStarted(s => s.Start());
                        c.WhenStopped(s => s.Stop());
                    });

                })
                    .Run();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top