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();
}
}
}
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.Service1constructor, 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:
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:
says ‘I want a concrete instance of
AuctionControl, but I dont’ want to determine exactly what type that is at compile-time andResolvewill figure it out at run-time’. However, in order forUnityto determine what to give you when you ask for anAuctionControl, you have to tell it what thatResolvecall should return. To do that, you need to setup a call toRegisterTypebefore you do anyResolve-ing, something like:which is, in this case, pointless as
Services.Engine.AuctionControlalways resolves to itself. (RegisterType<WhenAskedForThisType, GiveMeThisType>();).