Unity - running Windows Service(s)

Jul 7, 2008 at 10:13 PM
 The following template is provided when creating a new Windows Service

static void Main()
{
    ServiceBase
[] ServicesToRun;
    ServicesToRun =
new ServiceBase[]
    {
        new DataGatewayService(new GatewayService())
    };
    ServiceBase.Run(ServicesToRun);
}

For now I'm going with the following code to launch my Windows Service (second service won't be required for a wee bit) but being new to Unity I'm not sure what the best way is to get an Array of ServiceBase[] to resolve....   In my ignorance I would opt to manually populate a Servicebase[] by iterating through the results of a ResolveAll(<ServiceBase>()) and passing it on to ServiceBase.Run().  

static
void Main()
{
    IUnityContainer container = new UnityContainer()
        .RegisterType<
ServiceBase, Service1>()
        .RegisterType<
ILogger, TraceLogger>()
        .RegisterType<
IImageProcessor, ImageProcessorService>();

    ServiceBase.Run(container.Resolve<ServiceBase>());
}

Is there a better way?

 

 

Jul 11, 2008 at 8:37 PM

Why do you need to register your Service classes? Do they have dependencies on each other?

Why can't you just do:

ServiceBase.Run( new ServiceBase[] {
   container.Resolve<DataGatewayService>(),
   container.Resolve<MyOtherService>(),
   container.Resolve<TheThirdService>() });

You are writing the Service Main, so you know the names of the concrete classes. If you feel better, you can always register them against an interface Type and resolve them with the interface name.

Ray
initializecomponent.blogspot.com

Jul 15, 2008 at 2:13 PM


RayHenry wrote:

Why do you need to register your Service classes? Do they have dependencies on each other?

Why can't you just do:

ServiceBase.Run( new ServiceBase[] {
   container.Resolve<DataGatewayService>(),
   container.Resolve<MyOtherService>(),
   container.Resolve<TheThirdService>() });

You are writing the Service Main, so you know the names of the concrete classes. If you feel better, you can always register them against an interface Type and resolve them with the interface name.

Ray
initializecomponent.blogspot.com




[RayHenry] Why do you need to register your Service classes? Do they have dependencies on each other?

I'm creating an infrastructure for the team that will simplify Window Service(s) development and debugging (they simply have to work with the factory concrete classes for each process type); the following is the extent of the Windows Service code.   I have an MVP WinForm app that implements the same interface and provides a Start, Stop, Pause and Continue with logging interface.   Once I figure out how, I'll move the configuration into an App.Config and share it between the Win Service and WinForm applications (via VSS). 

namespace ImageProcessor
{
    public partial class Service1 : ServiceBase
    {
        private IImageProcessor _imageProcessorService;
        /// <summary>
        /// Service that will handle the image processing
        /// </summary>
        [Dependency]
        public IImageProcessor ImageProcessorService
        {
            get { return _imageProcessorService; }
            set { _imageProcessorService = value; }
        }

        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            _imageProcessorService.Start(args);
        }

        protected override void OnStop()
        {
            _imageProcessorService.Stop();
        }

        protected override void OnPause()
        {
            _imageProcessorService.Pause();
        }

        protected override void OnContinue()
        {
            _imageProcessorService.Continue();
        }
    }
}

----------------------------------------------

[RayHenry] Why can't you just do:

ServiceBase.Run( new ServiceBase[] {
   container.Resolve<DataGatewayService>(),
   container.Resolve<MyOtherService>(),
   container.Resolve<TheThirdService>() });

Forest through the trees...   Thank you very much - sure is a lot cleaner than what I had in mind ;)