.net 启动Windows服务时出错
我有一个使用installutil成功安装的windows服务,但是当我运行它时,我收到一个错误,说该服务无法启动,因为它无法及时响应。在事件查看器中,我可以看到此错误.net 启动Windows服务时出错,.net,c#-4.0,windows-services,unity-container,.net,C# 4.0,Windows Services,Unity Container,我有一个使用installutil成功安装的windows服务,但是当我运行它时,我收到一个错误,说该服务无法启动,因为它无法及时响应。在事件查看器中,我可以看到此错误 Application: AuctionControl.Service.exe Framework Version: v4.0.30319 Description: The process was terminated due to an unhandled exception. Exception Info: Microsof
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()
应用程序:AuctionControl.Service.exe
框架版本:v4.0.30319
描述:由于未处理的异常,进程已终止。
异常信息:Microsoft.Practices.Unity.ResolutionFailedException
堆栈:
在Microsoft.Practices.Unity.UnityContainer.DoBuildUp(System.Type、System.Object、System.String、System.Collections.Generic.IEnumerable`1)
在Microsoft.Practices.Unity.UnityContainer.Resolve(System.Type,System.String,Microsoft.Practices.Unity.ResolverRide[])中
在Microsoft.Practices.Unity.UnityContainerExtensions.Resolve[[System.\uu Canon,mscorlib,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089]](Microsoft.Practices.Unity.IUnityContainer,Microsoft.Practicess.Unity.Resolverride[])
在AuctionControl.Service.Service1..ctor()处
在AuctionControl.Service.Program.Main()处
我的代码在下面
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();
}
}
}
使用System.ServiceProcess;
使用Microsoft.Practices.Unity;
命名空间AuctionControl.Service
{
公共部分类Service1:ServiceBase
{
#区域构造函数
公共服务1()
{
初始化组件();
_容器=新的UnityContainer();
_auctionControl=_container.Resolve();
}
#端区
#区域字段
私有只读服务.Engine.AuctionControl\u AuctionControl;
私有只读UnityContainer\u容器;
#端区
启动时受保护的覆盖无效(字符串[]args)
{
_auctionControl.startActionControl();
}
受保护的覆盖void OnStop()
{
_StopAuctionControl();
}
}
}
这与明确地成为Windows服务无关,因为您还没有设置IoC,以便Unity在请求构造函数中某个实例时知道注入什么
大概您在AuctionControl.Service.Service1
构造函数中有一个接口,但您还没有告诉Unity容器要将该接口绑定/解析到哪个具体类
编辑:
你真的需要统一吗?它似乎没有做任何有用的事情
尝试:
这行吗
Unity应该允许您(通常)在运行时将接口绑定到具体类型,以便在测试中提供灵活性并减少组件的耦合。你知道为什么这里的代码中有一个Unity容器吗
这一行:
_auctionControl = _container.Resolve<Services.Engine.AuctionControl>();
在本例中,这是毫无意义的,因为
Services.Engine.AuctionControl
总是自行解析。(RegisterType();
)。您在服务constructor
和StartService
中做什么?可能有很多原因导致该错误。你试过调试吗?这不是我的代码,所以我还没有看过。我将尝试并调试它。添加一个全局异常处理程序,以便您可以看到错误。
_auctionControl = _container.Resolve<Services.Engine.AuctionControl>();
_container.RegisterType<Services.Engine.AuctionControl, Services.Engine.AuctionControl>();