如何注入字典?

时间:2018-07-25 05:43:18

标签: c# windows-services

如何为每个任务注入新的处理器字典? 我使用Autofac容器。我有Windows服务,我想将Run方法称为新任务。也许,我应该将容器替换为ServiceManager类吗? 每个任务必须具有带有处理器的单独词典。

public class Sevice : ServiceBase
    {
        private IContainer container;

        protected override void OnStart(string[] args)
        {
            var builder = new ContainerBuilder();
            builder.RegisterType(Logger).As<ILogger>();
            builder.RegisterType<Configuration>();

            container = builder.Build();

            var serviceRunner = container.Resolve<ServiceManagaer>();

            serviceManagarer.Start();
        }
    }

    public class ServiceManager
    {
        private readonly Configuration configuration;
        private readonly Logger logger;

        public ServiceManager(Configuration configuration, Logger logger)
        {
            this.configuration = configuration;
            this.logger = logger;
        }

        public void Start()
        {
            for (var i = 0; i < 5; i++)
            {
                var task = Task.Factory.StartNew(Run);
            }
        }

        public void Run()
        {
            var processors = new Dictionary<string, IProcessor>
            {
                {"A", new TestA()},
                {"B", new TestB()},
                {"C", new TestC()},
            };

            while (true)
            {
                Thread.Sleep(1000);

                processors[A].Process;
            }

        }
    }

0 个答案:

没有答案