如何在Windows服务器中解析UnityContainer

时间:2015-12-01 03:28:55

标签: c# dependency-injection windows-services unity-container

我正在创建一个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();
    }
 }

如果我在调试器模式下使用visual studio运行此代码,它运行正常,但是当我运行此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)

当我调用服务时,它已经解析了相关服务,这就是visual studio与db交互并返回结果的方式。为什么在运行Windows服务时它不能这样做?

我的问题是:

  • 如何解决此问题?我需要添加

    吗?
    _attachmentService = Container.Resolve<IAttachmentService>();
    
    _brxxgeService = Container.Resolve<IBrxxgeService>();
    
    _clRequestService = Container.Resolve<IClRequestService>();
    

这里的所有接口服务?

  • 如果是,我应该在哪里添加它们?

请帮忙。

1 个答案:

答案 0 :(得分:1)

使用continer extensions的更好方法:

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,您可以向现有注册添加新注册。