Exemple #1
0
        public void RegisterSingleton()
        {
            var builder = new Microsoft.Extensions.Hosting.HostBuilder()
                          .ConfigureServices((hostingContext, services) =>
            {
                services.AddSingleton <ICalcModule>(AssemblyLoader.LoadDirectory("."));
                // Build an intermediate service provider
                var sp     = services.BuildServiceProvider();
                var module = sp.GetService <ICalcModule>();
                Assert.True(module.Add(2, 3) == 5);
            });

            var b = builder.Build();
            //b.Run();
        }
Exemple #2
0
        public void DefaultIHostEnvironmentValues()
        {
            var hostBuilder = new HostBuilder()
                              .ConfigureAppConfiguration((hostContext, appConfig) =>
            {
                var env = hostContext.HostingEnvironment;
                Assert.Equal(EnvironmentName.Production, env.EnvironmentName);
                Assert.Null(env.ApplicationName);
                Assert.Equal(AppContext.BaseDirectory, env.ContentRootPath);
                Assert.IsAssignableFrom <PhysicalFileProvider>(env.ContentRootFileProvider);
            });

            using (var host = hostBuilder.Build())
            {
                var env = host.Services.GetRequiredService <IHostEnvironment>();
                Assert.Equal(EnvironmentName.Production, env.EnvironmentName);
                Assert.Null(env.ApplicationName);
                Assert.Equal(AppContext.BaseDirectory, env.ContentRootPath);
                Assert.IsAssignableFrom <PhysicalFileProvider>(env.ContentRootFileProvider);
            }
        }
Exemple #3
0
        public void ConfigureServices_CanBeCalledMultipleTimes()
        {
            var callCount   = 0; // Verify ordering
            var hostBuilder = new HostBuilder()
                              .ConfigureServices((services) =>
            {
                Assert.Equal(0, callCount++);
                services.AddTransient <ServiceA>();
            })
                              .ConfigureServices((services) =>
            {
                Assert.Equal(1, callCount++);
                services.AddTransient <ServiceB>();
            });

            using (var host = hostBuilder.Build())
            {
                Assert.Equal(2, callCount);

                Assert.NotNull(host.Services.GetRequiredService <ServiceA>());
                Assert.NotNull(host.Services.GetRequiredService <ServiceB>());
            }
        }
Exemple #4
0
        public void CanConfigureAppConfigurationAndRetrieveFromDI()
        {
            var hostBuilder = new HostBuilder()
                              .ConfigureAppConfiguration((configBuilder) =>
            {
                configBuilder.AddInMemoryCollection(
                    new KeyValuePair <string, string>[]
                {
                    new KeyValuePair <string, string>("key1", "value1")
                });
            })
                              .ConfigureAppConfiguration((configBuilder) =>
            {
                configBuilder.AddInMemoryCollection(
                    new KeyValuePair <string, string>[]
                {
                    new KeyValuePair <string, string>("key2", "value2")
                });
            })
                              .ConfigureAppConfiguration((configBuilder) =>
            {
                configBuilder.AddInMemoryCollection(
                    new KeyValuePair <string, string>[]
                {
                    // Hides value2
                    new KeyValuePair <string, string>("key2", "value3")
                });
            });

            using (var host = hostBuilder.Build())
            {
                var config = host.Services.GetService <IConfiguration>();
                Assert.NotNull(config);
                Assert.Equal("value1", config["key1"]);
                Assert.Equal("value3", config["key2"]);
            }
        }
Exemple #5
0
        public void ConfigureCustomServiceProvider()
        {
            var hostBuilder = new HostBuilder()
                              .ConfigureServices((hostContext, s) =>
            {
                s.AddTransient <ServiceD>();
                s.AddScoped <ServiceC>();
            })
                              .UseServiceProviderFactory(new FakeServiceProviderFactory())
                              .ConfigureContainer <FakeServiceCollection>((container) =>
            {
                Assert.Null(container.State);
                container.State = "1";
            })
                              .ConfigureContainer <FakeServiceCollection>((container) =>
            {
                Assert.Equal("1", container.State);
                container.State = "2";
            });
            var host         = hostBuilder.Build();
            var fakeServices = host.Services.GetRequiredService <FakeServiceCollection>();

            Assert.Equal("2", fakeServices.State);
        }
Exemple #6
0
        private static void noIHostedServiceAdded()
        {
            IHostBuilder hostBuilder = new Microsoft.Extensions.Hosting.HostBuilder();

            // Vage interface, de implementatie(HostBuilder) roept al die configure stuff aan

            //hostBuilder.ConfigureAppConfiguration((context, builder) =>
            //{
            //    Console.WriteLine("yolo");
            //});

            //hostBuilder.ConfigureHostConfiguration();
            //hostBuilder.ConfigureContainer()
            //hostBuilder.ConfigureServices()

            // Op deze manieer in Build()

            /*
             *     BuildHostConfiguration();
             *     CreateHostingEnvironment();
             *     CreateHostBuilderContext();
             *     BuildAppConfiguration();
             *
             *      // Hier word de hosting environment + Host toegevoegd aan de services              }
             *   CreateServiceProvider();
             * /*
             *
             * public interface IHostingEnvironment
             * {
             *  /// <summary>
             *  /// Gets or sets the name of the application. This property is automatically set by the host to the assembly containing
             *  /// the application entry point.
             *  /// </summary>
             *  string ApplicationName { get; set; }
             *
             *  /// <summary>
             *  /// Gets or sets an <see cref="T:Microsoft.Extensions.FileProviders.IFileProvider" /> pointing at <see cref="P:Microsoft.Extensions.Hosting.IHostingEnvironment.ContentRootPath" />.
             *  /// </summary>
             *  IFileProvider ContentRootFileProvider { get; set; }
             *
             *  /// <summary>
             *  /// Gets or sets the absolute path to the directory that contains the application content files.
             *  /// </summary>
             *  string ContentRootPath { get; set; }
             *
             *  /// <summary>
             *  /// Gets or sets the name of the environment. The host automatically sets this property to the value of the
             *  /// of the "environment" key as specified in configuration.
             *  /// </summary>
             *  string EnvironmentName { get; set; }
             */

            IHost host = hostBuilder.Build();

            //neeueuuhhh. dus die Run() zit niet eens in de IHost
            // Het zit in de HostingAbstractionsHostExtensions (dus)

            /*
             *  public static void Run(this IHost host)
             *  {
             *      host.RunAsync().GetAwaiter().GetResult();
             *  }
             */

            // de default host is dus Microsoft.Extensions.Hosting.Internal.Host
            // je heb 2 Host.cs dus meh die andere is Microsoft.Extenstion.Hosting.Host DUS

            // enfin wat het doet is

            /*
             *  _hostedServices = Services.GetService<IEnumerable<IHostedService>>();
             *
             *  foreach (var hostedService in _hostedServices)
             *  {
             *      // Fire IHostedService.Start
             *      await hostedService.StartAsync(combinedCancellationToken).ConfigureAwait(false);
             *  }
             */

            host.Run();

            // en dat zijn er normaal gesproken op deze manier dus geen (hosted services)
        }