Exemplo n.º 1
0
        /// <summary> 开启服务 </summary>
        /// <param name="args"></param>
        protected static void Start(string[] args)
        {
            Bootstrap = new SpearBootstrap();
            var services = new ServiceCollection();

            services.AddSpearConfig(ConfigBuild);

            services.AddLogging(builder =>
            {
                builder.AddConfiguration(ConfigHelper.Instance.Config.GetSection("Logging"));
                builder.AddConsole();
            });

            MapServiceCollection?.Invoke(services);
            Bootstrap.BuilderHandler += b =>
            {
                b.Populate(services);
                MapServices?.Invoke(b);
            };
            Bootstrap.Initialize();
            var container = Bootstrap.CreateContainer();

            if (UseServiceProvider != null)
            {
                var provider = new AutofacServiceProvider(container);
                UseServiceProvider.Invoke(provider);
            }

            UseServices?.Invoke(container);
            AppDomain.CurrentDomain.ProcessExit += (sender, e) =>
            {
                Shutdown();
            };
            Console.CancelKeyPress += (sender, e) =>
            {
                Shutdown();
            };
            while (true)
            {
                var cmd = Console.ReadLine();
                if (cmd == "exit")
                {
                    break;
                }
                Command?.Invoke(cmd, container);
            }
        }
        public void ShouldAddServiceUsingCallback()
        {
            //Example that would be added using callback in a
            // [assembly: PreApplicationStartMethod(typeof(InitClass), nameof(StaticVoidInitFunction))]
            MapServiceCollection.ConfigureServices += (serviceCollection) =>
            {
                serviceCollection.AddTransient <IFoo, Foo>();
            };

            var provider = new MockServicProviderCollection();
            var context  = new ServiceConfigurationContext(HostType.TestFramework, provider);
            IConfigurableModule module = new MapServiceCollection();

            module.ConfigureContainer(context);

            Assert.IsTrue(provider.GetService <IFoo>().Enabled);
        }