Example #1
0
            protected override IServiceCollection ConfigureApi(IServiceCollection services)
            {
                var service = new TestSingleCallModelBuilder();

                services.AddService <IModelBuilder>((sp, next) => service);
                return(services);
            }
            public static new IServiceCollection ConfigureApi(Type apiType, IServiceCollection services)
            {
                ApiBase.ConfigureApi(apiType, services);
                var service = new TestSingleCallModelBuilder();

                services.AddService <IModelBuilder>((sp, next) => service);
                return(services);
            }
            public static new IServiceCollection ConfigureApi(Type apiType, IServiceCollection services)
            {
                var service = new TestSingleCallModelBuilder();
                services.AddService<IModelBuilder>((sp, next) => service);

                services.AddScoped(apiType, apiType)
                    .AddScoped(typeof(ApiBase), apiType)
                    .AddScoped<ApiContext>();

                services.TryAddSingleton<ApiConfiguration>();
                return services;
            }
        public async Task ModelBuilderShouldBeCalledOnlyOnceIfSucceeded()
        {
            var services = new ServiceCollection();
            var service  = new TestSingleCallModelBuilder();

            services.CutoffPrevious <IModelBuilder>(service);
            var configuration = services.BuildApiConfiguration();

            using (var wait = new ManualResetEventSlim(false))
            {
                for (int i = 0; i < 2; i++)
                {
                    var tasks = PrepareThreads(50, configuration, wait);
                    wait.Set();

                    var models = await Task.WhenAll(tasks);

                    Assert.Equal(1, service.CalledCount);
                    Assert.True(models.All(e => object.ReferenceEquals(e, models[42])));
                }
            }
        }
        public async Task ModelBuilderShouldBeCalledOnlyOnceIfSucceeded()
        {
            var services = new ServiceCollection();
            var service = new TestSingleCallModelBuilder();
            services.CutoffPrevious<IModelBuilder>(service);
            var configuration = services.BuildApiConfiguration();

            using (var wait = new ManualResetEventSlim(false))
            {
                for (int i = 0; i < 2; i++)
                {
                    var tasks = PrepareThreads(50, configuration, wait);
                    wait.Set();

                    var models = await Task.WhenAll(tasks);
                    Assert.Equal(1, service.CalledCount);
                    Assert.True(models.All(e => object.ReferenceEquals(e, models[42])));
                }
            }
        }