Advertisement
NickJosevski

How to have only some constructor parameters injected with A

May 31st, 2011
454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.97 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using Autofac;
  4. using Daedalus.Channel;
  5. using Daedalus.Common;
  6. using Daedalus.Contracts.Common;
  7. using Daedalus.Contracts.Services;
  8. using Daedalus.Hosting.Interfaces;
  9. using Daedalus.Logic;
  10. using RabbitMQ.Client;
  11. using Topshelf.Configuration.Dsl;
  12. using log4net;
  13. using log4net.Config;
  14.  
  15. namespace Daedalus.Hosting.Hosts
  16. {
  17.     public class ProductServiceHost : IHost
  18.     {
  19.         //This instance of manager is only for debugging
  20.         private IManager<IProductService, ISerializer> _manager;
  21.  
  22.         private readonly ILog _log = LogManager.GetLogger(typeof(ProductServiceHost));
  23.         private IContainer _container;
  24.         private String ExchangeName { get; set; }
  25.  
  26.         public ProductServiceHost()
  27.         {
  28.             _log.InfoFormat("Enter Host Constructor");
  29.             ExchangeName = "test.workqueue.product";
  30.             SetupAutofac();
  31.         }
  32.  
  33.         public void InitializeHostedService(IServiceConfigurator<IManager<IProductService, ISerializer>> cfg)
  34.         {
  35.             cfg.HowToBuildService(n => _container.Resolve<IManager<IProductService, ISerializer>>());
  36.             cfg.WhenStarted(s =>
  37.                                 {
  38.                                     XmlConfigurator.Configure(
  39.                                         new FileInfo(
  40.                                             Path.Combine(
  41.                                                 AppDomain.CurrentDomain.BaseDirectory,
  42.                                                 String.Format("{0}.log4net.config", GetType().Name))));
  43.  
  44.                                     s.Start();
  45.                                 });
  46.             cfg.WhenStopped(s =>
  47.                                 {
  48.                                     s.Stop();
  49.                                     LogManager.Shutdown();
  50.                                 });
  51.         }
  52.  
  53.         public void OverrideStart()
  54.         {
  55.             _manager = _container.Resolve<IManager<IProductService, ISerializer>>();
  56.             _manager.Start();
  57.         }
  58.  
  59.         /// <summary>
  60.         /// Composition Root for Daedalus
  61.         /// </summary>
  62.         public void SetupAutofac()
  63.         {
  64.             _log.InfoFormat("SetupAutofac() on {0} @ {1}", GetType().Name, DateTime.Now);
  65.             var builder = new ContainerBuilder();
  66.  
  67.             SetupRegistrations(builder);
  68.  
  69.             _container = builder.Build();
  70.  
  71.             var e = _container.Resolve<IEndpoint<IProductService, ISerializer>>();
  72.             var b = e.IsRunning;
  73.         }
  74.  
  75.  
  76.         private void SetupRegistrations(ContainerBuilder builder)
  77.         {
  78.             _log.InfoFormat("SetupRegistrations()");
  79.  
  80.             builder.RegisterModule<LogInjectionModule>();
  81.  
  82.             builder.RegisterType<DaedalusRpcClient<IProductService>>().As<IDaedalusRpcClient<IProductService>>();
  83.  
  84.             builder.Register(c => new FakeDal()).As<IDal>();
  85.  
  86.             builder.Register(c => new ProductEngine(c.Resolve<IDal>()))
  87.                 .As<IProductService>();
  88.  
  89.  
  90.             builder.Register(c => new AllSerializeMethods())
  91.                 .As<ISerializer>();
  92.  
  93.             builder.Register(
  94.                 c => new ConnectionFactory {Address = "127.0.0.1"}.CreateConnection()
  95.                 ).As<IConnection>();
  96.  
  97.             builder.Register(
  98.                 c =>
  99.                     {
  100.                         var model = c.Resolve<IConnection>().CreateModel();
  101.                         model.ExchangeDeclare(ExchangeName, ExchangeType.Direct);
  102.                         return model;
  103.                     }
  104.                 ).As<IModel>();
  105.  
  106.             builder.Register(
  107.                 c =>
  108.                     {
  109.                         var ch = c.Resolve<IModel>();
  110.                         var queueName = ch.EnsureQueue(ExchangeName);
  111.                         var sub = new DaedalusSubscription(ch, queueName);
  112.                         return sub;
  113.                     }
  114.                 ).As<ISubscription>();
  115.  
  116.             builder.RegisterType<DaedalusRpcServer<ProductEngine, AllSerializeMethods>>()
  117.                                       /*c.Resolve<ISubscription>(),
  118.                                       c.Resolve<IProductService>() as ProductEngine,
  119.                                       c.Resolve<ISerializer>() as AllSerializeMethods,
  120.                                       c.Resolve<ILog>())*/
  121.                 .As<IDaedalusRpcServer<IProductService, ISerializer>>();
  122.  
  123.             builder.RegisterType<DaedalusServiceEndpoint<IProductService, ISerializer>>(
  124.                     /*c.Resolve<IDaedalusRpcServer<IProductService, ISerializer>>(),
  125.                     c.Resolve<ILog>())*/
  126.                 ).As<IEndpoint<IProductService, ISerializer>>();
  127.  
  128.             builder.Register(
  129.                 c =>
  130.                 new Manager<IProductService, ISerializer>(
  131.                     c.Resolve<IEndpoint<IProductService, ISerializer>>())
  132.                 )
  133.                 .As<IManager<IProductService, ISerializer>>();
  134.         }
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement