如何在windows serivce中解析UnityContainer以及何时解析
本文关键字:UnityContainer 何时解 windows serivce | 更新日期: 2023-09-27 18:25:17
我正在创建一个Windows服务,它使用现有的应用程序和域模型类库。
应用层已经定义了ContainerConfig
,它注册了像这样的所有接口
public class ConfigContainer
{
public UnityContainer ContainerConfig()
{
UnityContainer container = new UnityContainer();
container.RegisterType<IAttachmentService, AttachmentService>(new ContainerControlledLifetimeManager());
container.RegisterType<IBrxxgeService, BrxxgeService>(new ContainerControlledLifetimeManager());
container.RegisterType<ICaxxxxociationService, CaxxxxociationService>(new ContainerControlledLifetimeManager());
container.RegisterType<ITraxxxacityService, TraxxxcityService>(new ContainerControlledLifetimeManager());
return container;
}
}
像这样注册的服务接口有30多个。窗口内服务Program.cs
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new DClearanceService()
};
ServiceBase.Run(ServicesToRun);
}
然后在DClearanceService.cs
中
public partial class DClearanceService : ServiceBase
{
private ConfigContainer _containerConfig = new ConfigContainer();
private UnityContainer _container = new UnityContainer();
public DimensionalClearanceService()
{
InitializeComponent();
ExceptionHandlingManager.InitializeExceptionManager();
_container = _containerConfig.ContainerConfig();
}
我的方法是解析容器:
public class EMTrocessor
{
_clRequestService = Container.Resolve<IClRequestService>();
public bool ProcessMessage(string message)
{
List<ClOutput> clOutputs = _clRequestService.GetClOutputs();
}
}
如果我在调试器模式下使用visualstudio运行此代码,它会正常工作,但当我运行此windows服务时,我会得到Microsoft.Practices.Unity.ResolutionFailedException
<Description>An exception of type 'Microsoft.Practices.Unity.ResolutionFailedException' occurred and was caught.</Description>
<DateTime>2015-11-30 16:53:55Z</DateTime>
<ExceptionType>Microsoft.Practices.Unity.ResolutionFailedException, Microsoft.Practices.Unity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=6d32ff45e0ccc69f</ExceptionType>
<Message>Resolution of the dependency failed, type = "CPR.Apps.Application.Interfaces.IClEventService", name = "(none)".
Exception occurred while: Calling constructor CPR.Apps.Application.Services.CleranceEventService().
Exception is: ResolutionFailedException - Resolution of the dependency failed, type = "CPR.Apps.Domain.Interfaces.IClEventManager", name = "(none)".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The current type, CPR.Apps.Domain.Interfaces.IClEventManager, is an interface and cannot be constructed. Are you missing a type mapping?
-----------------------------------------------
At the time of the exception, the container was:
Resolving CPR.Apps.Domain.Interfaces.IClEventManager,(none)
当我调用服务时,它已经解析了相关的服务,这就是visualstudio与db交互并返回结果的方式。为什么当我运行Windows服务时它不能做同样的事情?
我的问题是:
如何解决此问题?我需要添加吗
_attachmentService = Container.Resolve<IAttachmentService>(); _brxxgeService = Container.Resolve<IBrxxgeService>(); _clRequestService = Container.Resolve<IClRequestService>();
这里的所有接口服务?
- 如果是,我应该在哪里添加它们
请帮忙。
使用continer扩展的更好方法:
public class ModelContainerExtension : UnityContainerExtension
{
protected override void Initialize()
{
Container.RegisterType<IAttachmentService, AttachmentService>(new ContainerControlledLifetimeManager());
Container.RegisterType<IBrxxgeService, BrxxgeService>(new ContainerControlledLifetimeManager());
Container.RegisterType<ICaxxxxociationService, CaxxxxociationService>(new ContainerControlledLifetimeManager());
Container.RegisterType<ITraxxxacityService, TraxxxcityService>(new ContainerControlledLifetimeManager());
}
}
public partial class DClearanceService : ServiceBase
{
private UnityContainer _container = new UnityContainer();
public DimensionalClearanceService()
{
InitializeComponent();
ExceptionHandlingManager.InitializeExceptionManager();
_container.AddExtension(new ModelContainerExtension());
}
为什么更好?在您的案例中,您替换了整个容器变量,如果您之前进行了一些注册,您将丢失它。使用AddExtension
,您将在现有注册的基础上添加新注册。