Пример #1
0
        public void BuildsServices()
        {
            // arrange
            var services = new ServiceCollection();
            var context  = new HostBuilderContext(new Dictionary <object, object> {
                { "SomeProperty", "SomeValue" }
            });
            var builder = new OutkeepClientBuilder();

            // act
            builder.ConfigureServices((context, services) =>
            {
                services.Configure <SomeOptions>(options =>
                {
                    options.SomeProperty = context.Properties["SomeProperty"].ToString();
                });
            });

            // assert no services added yet
            Assert.Empty(services);

            // act
            builder.Build(context, services);

            // assert hosted service added
            Assert.Contains(services, x => x.ServiceType == typeof(IHostedService) && x.ImplementationType == typeof(OutkeepClientHostedService) && x.Lifetime == ServiceLifetime.Singleton);

            // assert options service added
            Assert.Contains(services, x => x.ServiceType == typeof(IOptions <>));

            // assert options value set
            Assert.Equal("SomeValue", services.BuildServiceProvider().GetRequiredService <IOptions <SomeOptions> >().Value.SomeProperty);
        }
Пример #2
0
        public static IHostBuilder UseOutkeepClient(this IHostBuilder builder, Action <HostBuilderContext, IOutkeepClientBuilder> configure)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            return(builder.ConfigureServices((context, services) =>
            {
                OutkeepClientBuilder outkeep;
                if (context.Properties.TryGetValue(HostBuilderContextKey, out var existing))
                {
                    outkeep = (OutkeepClientBuilder)existing;
                }
                else
                {
                    outkeep = new OutkeepClientBuilder();
                    context.Properties[HostBuilderContextKey] = outkeep;
                }

                configure(context, outkeep);

                outkeep.Build(context, services);
            }));
        }