Exemplo n.º 1
0
        public static async Task <MopHost> BuildHost(string[] args, CancellationToken token)
        {
            var props = await HostPropertiesFactory.BuildPropertiesAsync(args);

            var injector   = new InjectorService();
            var mainSystem = ActorSystemFactory.BuildFrom(props);

            injector.RegisterService(() => props, LifeCycle.Singleton);
            injector.RegisterService(() => new MopLifeService(token), LifeCycle.Singleton);
            injector.RegisterService(() => mainSystem, LifeCycle.Singleton);
            injector.RegisterService <IInjectorService>(() => injector, LifeCycle.Singleton);
            injector.RegisterService <IRIPService, RIPService>(LifeCycle.Singleton);
            injector.RegisterService <IHost, MopHost>(LifeCycle.Singleton);
            injector.RegisterService <ILogService, LogService>(LifeCycle.Singleton);
            injector.RegisterService <IConfigService, ConfigService>(LifeCycle.Singleton);
            injector.RegisterService <IEventService, EventService>(LifeCycle.Singleton);
            injector.RegisterService <PluginLoader>(LifeCycle.Transient);
            injector.RegisterService <IPluginService, PluginService>(LifeCycle.Singleton);
            injector.RegisterService <AssemblyResolver>(LifeCycle.Transient);

            if (injector.GetService <AssemblyResolver>() is AssemblyResolver r)
            {
                r.RegisterAppDomain(AppDomain.CurrentDomain);
            }

            if (injector.GetService <IHost>() is MopHost host)
            {
                return(host);
            }

            throw new Exception("Failed to instantiate host");
        }
Exemplo n.º 2
0
        public void TestInjector_InitConstrait()
        {
            InjectorService.ResetInitStatus();
            var injector = new InjectorService();

            Assert.NotNull(injector);
            Assert.Throws <AccessViolationException>(() => new InjectorService());
        }
Exemplo n.º 3
0
 public static void RegisterServices(IServiceCollection services)
 {
     InjectorService.InjectServices(services);
     InjectorRepository.InjectRepositories(services);
     InjectorDatabase.InjectDatabase(services);
     InjectorSecurity.InjectSecutiry(services);
     InjectorDomain.InjectDomain(services);
     InjectorEngine.InjectEngine(services);
 }
Exemplo n.º 4
0
        public void TestInjector_RegisterGenericType_TypeInstance()
        {
            InjectorService.ResetInitStatus();
            var injector = new InjectorService();

            injector.RegisterService <ITestInterface, TestImplConst>();
            var service = injector.GetService <ITestInterface>();

            Assert.NotNull(service);
            Assert.Equal(new TestImplConst().Name, service.Name);
        }
Exemplo n.º 5
0
        public void TestInjector_RegisterType_Instance()
        {
            InjectorService.ResetInitStatus();
            var injector = new InjectorService();

            injector.RegisterService(typeof(TestImplConst));
            var service = injector.GetService <TestImplConst>();

            Assert.NotNull(service);
            Assert.Equal(new TestImplConst().Name, service.Name);
        }
Exemplo n.º 6
0
        public void TestInjector_RegisterGenericType_InstanceCreator()
        {
            InjectorService.ResetInitStatus();
            var injector = new InjectorService();
            var target   = "PIKA";

            injector.RegisterService <ITestInterface>(() => new TestImpl(target));
            var service = injector.GetService <ITestInterface>();

            Assert.NotNull(service);
            Assert.Equal(target, service.Name);
        }
Exemplo n.º 7
0
        public void Stop()
        {
            foreach (var c in ConfigurationClasses)
            {
                c.Stop(this);
            }

            var serviceManager = InjectorService.Resolve <IServiceManager>();

            serviceManager.StopServices();

            var persistanceService = InjectorService.Resolve <IPersistanceService>();

            persistanceService.Save();
        }
Exemplo n.º 8
0
        public void Start()
        {
            foreach (var c in ConfigurationClasses)
            {
                c.Start(this);
            }

            var serviceManager = InjectorService.Resolve <IServiceManager>();

            foreach (var s in InjectorService.ResolveAll <IService>())
            {
                serviceManager.AddService(s);
            }

            serviceManager.StartServices();
        }
Exemplo n.º 9
0
        public void TestInjector_transient_default()
        {
            InjectorService.ResetInitStatus();
            var injector = new InjectorService();
            var target   = "PIKA";
            var target2  = "EKANS";

            injector.RegisterService <ITestInterface>(() => new TestImpl(target));
            var service = injector.GetService <ITestInterface>();

            Assert.NotNull(service);
            Assert.Equal(target, service.Name);

            service.Name = target2;
            var newService = injector.GetService <ITestInterface>();

            Assert.Equal(target, newService.Name);
        }
Exemplo n.º 10
0
        public void TestInjector_singleton()
        {
            InjectorService.ResetInitStatus();
            var injector = new InjectorService();
            var target   = "PIKA";
            var target2  = "EKANS";

            injector.RegisterService <ITestInterface>(() => new TestImpl(target), LifeCycle.Singleton);
            var service = injector.GetService <ITestInterface>();

            Assert.NotNull(service);
            Assert.Equal(target, service.Name);

            service.Name = target2;
            var newService = injector.GetService <ITestInterface>();

            Assert.Equal(target2, newService.Name);
        }
Exemplo n.º 11
0
        /// <summary>
        /// 初始化依赖注入实例和注入配置。
        /// </summary>
        private static bool InitService()
        {
            bool success = true;

            try
            {
                var iocBuilder = CreateIocBuilder();
                var container  = iocBuilder.Build();
                var locator    = new AutofacServiceLocator(container);
                ServiceLocator.SetLocatorProvider(() => locator);
                InjectorService.InitInjector(ServiceLocator.Current);

                InitServiceData();
                success = true;
            }
            catch (Exception err)
            {
                success = false;
                LoggingService.Error("Program.InitService(),初始化依赖注入服务失败。", err);
                System.Diagnostics.Trace.WriteLine("Program InitService Error:" + err.ToString());
                MessageBox.Show("初始化依赖注入服务失败:" + err.ToString());
            }
            return(success);
        }
Exemplo n.º 12
0
        public void Configure(string BasePath = null)
        {
            Console.WriteLine("#################################################################################################");
            var lines = File.ReadAllLines("Title.txt");

            foreach (var l in lines)
            {
                Console.WriteLine(l);
            }
            Console.WriteLine("#################################################################################################");
            Console.WriteLine("Device Name: " + Caspara.DeviceName);
            Console.WriteLine("Platform Type: " + Environment.OSVersion.Platform.ToString());
            Console.WriteLine("OS Version: " + Environment.OSVersion.VersionString);
            Console.WriteLine("64-Bit: " + Environment.Is64BitProcess);
            Console.WriteLine("#################################################################################################");

            foreach (var c in ConfigurationClasses)
            {
                c.Configure(this);
            }

            if (!InjectorService.IsRegistered <IConsoleLogger>())
            {
                InjectorService.Register <ConsoleTextLogger>().As <IConsoleLogger>().AsSingleton();
            }

            if (!InjectorService.IsRegistered <IExtensionManager>())
            {
                InjectorService.Register <ExtensionManager>().As <IExtensionManager>().AsSingleton();
            }

            if (!InjectorService.IsRegistered <IServiceManager>())
            {
                InjectorService.Register <ServiceManager>().As <IServiceManager>().AsSingleton();
            }

            if (!InjectorService.IsRegistered <ISerializer>())
            {
                InjectorService.Register <Serializer>().As <ISerializer>();
            }

            if (!InjectorService.IsRegistered <ISerializerService>())
            {
                InjectorService.Register <LocalSerializerService>().As <ISerializerService>().AsSingleton();
            }

            if (!InjectorService.IsRegistered <IPersistanceService>())
            {
                InjectorService.Register <PersistanceService>().As <IPersistanceService>().AsSingleton();
            }

            var extensionManager = InjectorService.Resolve <IExtensionManager>();

            extensionManager.LocateExtensions();
            if (BasePath == null)
            {
                BasePath = extensionManager.GetApplicationPath();
            }

            ApplicationPath = extensionManager.GetApplicationPath();

            var persistanceService = InjectorService.Resolve <IPersistanceService>();

            persistanceService.Load <PersistanceModel>(Path.Combine(BasePath, "PersistanceModel.xml"));

            extensionManager.LoadExtensions(this);
        }