1

I have a windows service that I have successfully installed using installutil but when I run it I get an error saying that the service failed to start because it failed to respond in a timely fashion. In the Event Viewer, I can see this error.

Application: AuctionControl.Service.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: Microsoft.Practices.Unity.ResolutionFailedException
Stack:
   at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(System.Type, System.Object, System.String, System.Collections.Generic.IEnumerable`1<Microsoft.Practices.Unity.ResolverOverride>)
   at Microsoft.Practices.Unity.UnityContainer.Resolve(System.Type, System.String, Microsoft.Practices.Unity.ResolverOverride[])
   at Microsoft.Practices.Unity.UnityContainerExtensions.Resolve[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]](Microsoft.Practices.Unity.IUnityContainer, Microsoft.Practices.Unity.ResolverOverride[])
   at AuctionControl.Service.Service1..ctor()
   at AuctionControl.Service.Program.Main()

My code is below

using System.ServiceProcess;
using Microsoft.Practices.Unity;

namespace AuctionControl.Service
{
    public partial class Service1 : ServiceBase
    {
        #region Constructor(s)

        public Service1()
        {
            InitializeComponent();


            _container = new UnityContainer();

            _auctionControl = _container.Resolve<Services.Engine.AuctionControl>();
        }

        #endregion

        #region Fields

        private readonly Services.Engine.AuctionControl _auctionControl;
        private readonly UnityContainer _container;

        #endregion

        protected override void OnStart(string[] args)
        {
            _auctionControl.StartAuctionControl();
        }

        protected override void OnStop()
        {
            _auctionControl.StopAuctionControl();
        }
    }
}
Sachin Kainth
  • 45,256
  • 81
  • 201
  • 304

1 Answers1

0

This isn't to do with being a Windows Service explicitly, it's because you haven't set up your IoC so that Unity knows what to inject when it's asked for an instance of something in your constructor.

Presumably you have an interface in your AuctionControl.Service.Service1 constructor, but you haven't told your Unity container what concrete class to bind/resolve that interface to.

EDIT:

Do you actually need Unity? It doesn't seem to be doing anything useful.

Try:

public Service1() 
{ 
    InitializeComponent(); 

   _auctionControl = new Services.Engine.AuctionControl();
} 

Does this work?

Unity should allow you to bind (generally) interfaces to concrete types at runtime to give you flexibility in testing and reduce coupling of components. Do you know why there is a Unity container in the code here?

This line:

_auctionControl = _container.Resolve<Services.Engine.AuctionControl>();

says 'I want a concrete instance of AuctionControl, but I dont' want to determine exactly what type that is at compile-time and Resolve will figure it out at run-time'. However, in order for Unity to determine what to give you when you ask for an AuctionControl, you have to tell it what that Resolve call should return. To do that, you need to setup a call to RegisterType before you do any Resolve-ing, something like:

_container.RegisterType<Services.Engine.AuctionControl, Services.Engine.AuctionControl>();

which is, in this case, pointless as Services.Engine.AuctionControl always resolves to itself. (RegisterType<WhenAskedForThisType, GiveMeThisType>();).

nicodemus13
  • 2,258
  • 2
  • 19
  • 31