Beispiel #1
0
        ServiceProvider DoCreateAutomaticServices(SimpleServiceContainer?startupServices, IStObjMap current)
        {
            var services = new ServiceCollection();
            var reg      = new StObjContextRoot.ServiceRegister(TestHelper.Monitor, services, startupServices);

            var configureArgs = new AutomaticServicesConfigurationEventArgs(current, reg);
            var hIng          = _automaticServicesConfiguring;

            if (hIng != null)
            {
                using (_monitor.Monitor.OpenInfo("Raising Automatic services configuring event."))
                {
                    hIng(this, configureArgs);
                }
            }
            if (!reg.AddStObjMap(current))
            {
                Throw.Exception("AddStObjMap failed. The logs contains detailed information.");
            }
            var hEd = _automaticServicesConfigured;

            if (hEd != null)
            {
                using (_monitor.Monitor.OpenInfo("Raising Automatic services configured event."))
                {
                    hEd(this, configureArgs);
                }
            }
            return(services.BuildServiceProvider());
        }
        public void Our_ServiceRegister_maps_all_to_the_same_instance(bool scoped)
        {
            IServiceCollection services = new ServiceCollection();
            var reg = new StObjContextRoot.ServiceRegister(TestHelper.Monitor, services);

            reg.Register(typeof(IA), typeof(A), scoped, allowMultipleRegistration: false);
            reg.Register(typeof(IB), typeof(A), scoped, allowMultipleRegistration: false);

            var sp = services.BuildServiceProvider();

            var a = sp.GetRequiredService <IA>();
            var b = sp.GetRequiredService <IB>();
            var c = sp.GetRequiredService <A>();

            a.Should().BeSameAs(b).And.BeSameAs(c);

            using (var scope = sp.CreateScope())
            {
                var aS = scope.ServiceProvider.GetRequiredService <IA>();
                var bS = scope.ServiceProvider.GetRequiredService <IB>();
                var cS = scope.ServiceProvider.GetRequiredService <A>();
                aS.Should().BeSameAs(bS).And.BeSameAs(cS);
                if (scoped)
                {
                    a.Should().NotBeSameAs(aS);
                }
                else
                {
                    a.Should().BeSameAs(aS);
                }
            }
        }
Beispiel #3
0
        StObjContextRoot.ServiceRegister IStObjEngineTestHelperCore.GetFailedAutomaticServicesConfiguration(StObjCollector c,
                                                                                                            Func <StObjEngineConfiguration, StObjEngineConfiguration>?engineConfigurator,
                                                                                                            SimpleServiceContainer?startupServices)
        {
            IStObjMap map = DoCompileAndLoadStObjMap(c, engineConfigurator, useEmbeddedStObjMapIfPossible: false).Map;
            var       reg = new StObjContextRoot.ServiceRegister(TestHelper.Monitor, new ServiceCollection(), startupServices);

            reg.AddStObjMap(map).Should().BeFalse("Service configuration failed.");
            return(reg);
        }
        /// <summary>
        /// Configures this <see cref="IServiceCollection"/> by registering the <see cref="IStObjMap.StObjs"/> and
        /// the <paramref name="map"/> itself as Singletons, by calling <see cref="StObjContextRoot.RegisterStartupServicesMethodName"/>
        /// and then <see cref="StObjContextRoot.ConfigureServicesMethodName"/> on all the <see cref="IStObjObjectMap.FinalImplementations"/> that expose
        /// such methods and by registering the <see cref="IStObjServiceMap.SimpleMappings"/> and <see cref="IStObjServiceMap.ManualMappings"/> mappings.
        /// Any attempt to register an already registered service will be ignored and a warning will be emitted.
        /// <para>
        /// If the registration fails for any reason (file not found, type conflicts, etc.), a <see cref="CKException"/> is thrown.
        /// </para>
        /// </summary>
        /// <param name="services">This service collection to configure.</param>
        /// <param name="monitor">The monitor to use. Must not be null.</param>
        /// <param name="map">StObj map to register. Can not be null.</param>
        /// <param name="startupServices">
        /// Optional simple container that may provide startup services. This is not used to build IRealObject
        /// (they must be independent of any "dynamic" services), however registered services become available to
        /// any <see cref="StObjContextRoot.ConfigureServicesMethodName"/> methods by parameter injection.
        /// </param>
        /// <returns>This services collection.</returns>
        public static IServiceCollection AddStObjMap(this IServiceCollection services, IActivityMonitor monitor, IStObjMap map, SimpleServiceContainer?startupServices = null)
        {
            var reg = new StObjContextRoot.ServiceRegister(monitor, services, startupServices);

            if (!reg.AddStObjMap(map))
            {
                throw new CKException("AddStObjMap failed. The logs contains detailed information.");
            }
            return(services);
        }
 /// <summary>
 /// Configure the services (does nothing here: this just tests the parameter
 /// injection of the startup services).
 /// </summary>
 /// <param name="register">This one is the only required parameter.</param>
 /// <param name="ambientObjects">
 /// IStObjObjectMap is available: configuring services can rely on any IRealObject since they are already initialized (this
 /// is even available in the RegisterStartupServices).
 /// </param>
 /// <param name="superService">This is injected.</param>
 /// <param name="ext">This is injected.</param>
 /// <param name="optionalService">
 /// This is injected if it exists in the StartupServices: startup services can be optional.
 /// </param>
 void ConfigureServices(StObjContextRoot.ServiceRegister register,
                        IStObjObjectMap ambientObjects,
                        SuperStartupService superService,
                        TotallyExternalStartupServiceThatActAsAConfiguratorOfTheWholeSystem ext,
                        IOptionalStartupService?optionalService = null)
 {
     ambientObjects.Obtain <IA1>().Should().BeSameAs(this);
     superService.Should().NotBeNull();
     ext.Should().NotBeNull();
     superService.Talk(register.Monitor);
 }
Beispiel #6
0
        AutomaticServicesResult IStObjEngineTestHelperCore.CreateAutomaticServices(StObjCollector c,
                                                                                   Func <StObjEngineConfiguration, StObjEngineConfiguration>?engineConfigurator,
                                                                                   SimpleServiceContainer?startupServices,
                                                                                   Action <StObjContextRoot.ServiceRegister>?configureServices)
        {
            var loadResult = DoCompileAndLoadStObjMap(c, engineConfigurator, true);

            var reg = new StObjContextRoot.ServiceRegister(TestHelper.Monitor, new ServiceCollection(), startupServices);

            reg.AddStObjMap(loadResult.Map).Should().BeTrue("Service configuration succeed.");
            configureServices?.Invoke(reg);

            return(new AutomaticServicesResult(loadResult, reg, reg.Services.BuildServiceProvider()));
        }
Beispiel #7
0
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
        public void Deconstruct(out StObjCollectorResult collector, out IStObjMap map, out StObjContextRoot.ServiceRegister serviceRegister, out ServiceProvider services)
        {
            collector       = CollectorResult;
            map             = Map;
            serviceRegister = ServiceRegister;
            services        = Services;
        }
Beispiel #8
0
 internal AutomaticServicesResult(CompileAndLoadResult r, StObjContextRoot.ServiceRegister serviceRegister, ServiceProvider services)
 {
     CompileAndLoadResult = r;
     ServiceRegister      = serviceRegister;
     Services             = services;
 }
 /// <summary>
 /// Initializes a new <see cref="AutomaticServicesConfigurationEventArgs"/>.
 /// </summary>
 /// <param name="map">The current StObjMap. Must not be null.</param>
 /// <param name="serviceRegister">The service register.</param>
 public AutomaticServicesConfigurationEventArgs(IStObjMap map, StObjContextRoot.ServiceRegister serviceRegister)
 {
     Throw.CheckNotNullArgument(map);
     StObjMap        = map;
     ServiceRegister = serviceRegister;
 }
            void ConfigureServices(StObjContextRoot.ServiceRegister register, TotallyExternalStartupServiceThatActAsAConfiguratorOfTheWholeSystem conf)
            {
                var impl = conf.AlwaysUseAlice ? typeof(PrivateAlwaysAliceProvider) : typeof(PrivateAliceOrBobProvider);

                register.Register(typeof(IAliceOrBobProvider), impl, isScoped: true, allowMultipleRegistration: false);
            }