예제 #1
0
파일: Factory.cs 프로젝트: nastprol/Piano
        public Constructor GetNew()
        {
            var container = new ChildKernel(parentContainer);

            container.Bind <IModeSettings>().To <ModeSettings>().InSingletonScope();

            container.Bind <Map>().ToSelf().InSingletonScope();

            container.Bind(x =>
                           x.From(Assembly.GetAssembly(typeof(IGameMode))).SelectAllClasses().InheritedFrom <IGameMode>()
                           .BindAllInterfaces());

            container.Bind <ModeSettings>()
            .ToSelf()
            .InSingletonScope()
            .WithConstructorArgument("modes", container.GetAll <IGameMode>().ToArray());
            container.Bind <GameState>().ToSelf().InSingletonScope()
            .WithConstructorArgument("shift", container.Get <KeySettings>().Height);

            container.Bind <GameForm>().ToSelf().InSingletonScope();
            container.Bind <IMouseInput>().ToMethod(c => c.Kernel.Get <GameForm>()).InSingletonScope();
            container.Bind <IKeyInput>().ToMethod(c => c.Kernel.Get <GameForm>()).InSingletonScope();
            container.Bind <VisualizationSettings>().ToSelf().InSingletonScope();
            container.Bind <KeyBoardSettings>().ToSelf().InSingletonScope();
            container.Bind(x =>
                           x.From(Assembly.GetAssembly(typeof(IInputControl))).SelectAllClasses().InheritedFrom <IInputControl>()
                           .BindAllInterfaces());

            container.Bind <IInputControlSettings>().To <InputControlSettings>().InSingletonScope();
            container.Bind <InputControlSettings>()
            .ToSelf()
            .InSingletonScope()
            .WithConstructorArgument("controls", container.GetAll <IInputControl>().ToArray());

            container.Bind <Controller>().ToSelf().InSingletonScope();

            return(container.Get <Constructor>());
        }
예제 #2
0
        public void TestOneChildGetParent()
        {
            StandardKernel kernel = new StandardKernel();
            ChildKernel    child  = new ChildKernel(kernel);

            kernel.Bind <IBaseClass>().To <ClassA>();

            child.Bind <IBaseClass>().To <ClassB>();

            var classesFromParent = kernel.GetAll <IBaseClass>();

            Assert.AreEqual(1, classesFromParent.Count());

            var classesFromChild = child.GetAll <IBaseClass>();

            Assert.AreEqual(1, classesFromChild.Count());
        }
예제 #3
0
        internal async Task <bool> Start(HostControl hostControl)
        {
            _hostControl = hostControl;

            _log.Info("Starting Hosted Microservice...");

            int retries = MaxRetries;

            while (retries > 0)
            {
                var childKernel = new ChildKernel(_kernel);

                var configuration        = childKernel.Get <HostedServiceConfiguration>();
                var startup              = childKernel.Get <Startup>();
                var deferredRegistration = await _discovery.RegisterServiceDeferred();

                if (configuration.RestApiPort > 0)
                {
                    var apiUri = $"http://{configuration.RestApiHostname}:{configuration.RestApiPort}";
                    try
                    {
                        _app = WebApp.Start(apiUri, startup.Configuration);
                        _log.Info($"Started API at {apiUri}");

                        _registration = await _discovery.RegisterService(deferredRegistration);

                        MonitorServiceRegistration(_cancellationTokenSource.Token);

                        _inServiceWorkers = childKernel.GetAll <IInServiceWorker>().ToArray();
                        if (_inServiceWorkers.Any())
                        {
                            _inServiceWorkers.ToList().ForEach(worker => worker.Start(_cancellationTokenSource.Token));
                            _log.Info($"Startd {_inServiceWorkers.Count()} in service workers");
                        }

                        return(true);
                    }
                    catch (Exception ex)
                    {
                        _log.Warn($"Failed to start the API host on {apiUri}: {ex.Message}");
                        while (ex.InnerException != null)
                        {
                            _log.Debug(ex.Message);
                            ex = ex.InnerException;
                        }

                        if (_app != null)
                        {
                            _app.Dispose();
                            _app = null;
                        }

                        _log.Info($"Retrying...({MaxRetries - retries}/{MaxRetries})");
                        configuration.RestApiPort = 0;
                        retries--;
                    }
                }
                else
                {
                    retries = 0;
                }

                await Task.Delay(RetryPeriodInSeconds * 1000, _cancellationTokenSource.Token);
            }

            _discovery.DeregisterService(_registration);
            _log.Fatal($"Could not start API due to one or more fatal errors");

            return(false);
        }