当前位置: 首页>>代码示例>>C#>>正文


C# ServiceCollection.BuildApiConfiguration方法代码示例

本文整理汇总了C#中ServiceCollection.BuildApiConfiguration方法的典型用法代码示例。如果您正苦于以下问题:C# ServiceCollection.BuildApiConfiguration方法的具体用法?C# ServiceCollection.BuildApiConfiguration怎么用?C# ServiceCollection.BuildApiConfiguration使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ServiceCollection的用法示例。


在下文中一共展示了ServiceCollection.BuildApiConfiguration方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ConfigurationRegistersHookPointsCorrectly

        public void ConfigurationRegistersHookPointsCorrectly()
        {
            IServiceCollection services = new ServiceCollection();
            var configuration = services.BuildApiConfiguration();

            Assert.Null(configuration.GetApiService<IHookA>());
            Assert.Null(configuration.GetApiService<IHookB>());

            var singletonHookPoint = new HookA();
            services.CutoffPrevious<IHookA>(singletonHookPoint);
            configuration = services.BuildApiConfiguration();
            Assert.Same(singletonHookPoint, configuration.GetApiService<IHookA>());
            Assert.Null(configuration.GetApiService<IHookB>());

            var multiCastHookPoint1 = new HookB();
            services.CutoffPrevious<IHookB>(multiCastHookPoint1);
            configuration = services.BuildApiConfiguration();
            Assert.Same(singletonHookPoint, configuration.GetApiService<IHookA>());
            Assert.Equal(multiCastHookPoint1, configuration.GetApiService<IHookB>());

            services = new ServiceCollection()
                .CutoffPrevious<IHookB>(multiCastHookPoint1)
                .ChainPrevious<IHookB, HookB>()
                .AddInstance(new HookB());
            configuration = services.BuildApiConfiguration();
            var multiCastHookPoint2 = configuration.GetApiService<HookB>();
            var handler = configuration.GetApiService<IHookB>();
            Assert.Equal(multiCastHookPoint2, handler);

            var delegateHandler = handler as HookB;
            Assert.NotNull(delegateHandler);
            Assert.Equal(multiCastHookPoint1, delegateHandler.InnerHandler);
        }
开发者ID:kosinsky,项目名称:RESTier,代码行数:33,代码来源:ApiConfiguration.Tests.cs

示例2: ContributorsAreCalledCorrectly

        public void ContributorsAreCalledCorrectly()
        {
            int i = 0;
            var services = new ServiceCollection()
                .AddContributor<ISomeService>((sp, next) => new SomeService()
                {
                    Next = next(),
                    Value = i++,
                })
                .AddContributor<ISomeService>((sp, next) => new SomeService()
                {
                    Next = next(),
                    Value = i++,
                })
                .ChainPrevious<ISomeService>(next => new SomeService()
                {
                    Next = next,
                    Value = i++,
                })
                .ChainPrevious<ISomeService>((sp, next) => new SomeService()
                {
                    Next = next,
                    Value = i++,
                })
                .ChainPrevious<ISomeService, SomeService>();

            var configuration = services.BuildApiConfiguration();
            var value = configuration.GetApiService<ISomeService>().Call();
            Assert.Equal("03210", value);
        }
开发者ID:kosinsky,项目名称:RESTier,代码行数:30,代码来源:ServiceConfiguration.Tests.cs

示例3: GetModelUsingDefaultModelHandler

        public async Task GetModelUsingDefaultModelHandler()
        {
            var services = new ServiceCollection();
            services.CutoffPrevious<IModelBuilder>(new TestModelProducer());
            services.ChainPrevious<IModelBuilder>(next => new TestModelExtender(2)
            {
                InnerHandler = next,
            });
            services.ChainPrevious<IModelBuilder>(next => new TestModelExtender(3)
            {
                InnerHandler = next,
            });

            var configuration = services.BuildApiConfiguration();
            var context = new ApiContext(configuration);

            var model = await context.GetModelAsync();
            Assert.Equal(4, model.SchemaElements.Count());
            Assert.NotNull(model.SchemaElements
                .SingleOrDefault(e => e.Name == "TestName"));
            Assert.NotNull(model.SchemaElements
                .SingleOrDefault(e => e.Name == "TestName2"));
            Assert.NotNull(model.SchemaElements
                .SingleOrDefault(e => e.Name == "TestName3"));
            Assert.NotNull(model.EntityContainer);
            Assert.NotNull(model.EntityContainer.Elements
                .SingleOrDefault(e => e.Name == "TestEntitySet"));
            Assert.NotNull(model.EntityContainer.Elements
                .SingleOrDefault(e => e.Name == "TestEntitySet2"));
            Assert.NotNull(model.EntityContainer.Elements
                .SingleOrDefault(e => e.Name == "TestEntitySet3"));
        }
开发者ID:kosinsky,项目名称:RESTier,代码行数:32,代码来源:DefaultModelHandler.Tests.cs

示例4: GetModelAsyncRetriableAfterFailure

        public async Task GetModelAsyncRetriableAfterFailure()
        {
            var services = new ServiceCollection();
            var service = new TestRetryModelBuilder();
            services.CutoffPrevious<IModelBuilder>(service);
            var configuration = services.BuildApiConfiguration();

            using (var wait = new ManualResetEventSlim(false))
            {
                var tasks = PrepareThreads(6, configuration, wait);
                wait.Set();

                await Task.WhenAll(tasks).ContinueWith(t =>
                {
                    Assert.True(t.IsFaulted);
                    Assert.True(tasks.All(e => e.IsFaulted));
                });
                Assert.Equal(1, service.CalledCount);

                tasks = PrepareThreads(150, configuration, wait);

                var models = await Task.WhenAll(tasks);
                Assert.Equal(2, service.CalledCount);
                Assert.True(models.All(e => object.ReferenceEquals(e, models[42])));
            }
        }
开发者ID:kosinsky,项目名称:RESTier,代码行数:26,代码来源:DefaultModelHandler.Tests.cs

示例5: ModelBuilderShouldBeCalledOnlyOnceIfSucceeded

        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])));
                }
            }
        }
开发者ID:kosinsky,项目名称:RESTier,代码行数:20,代码来源:DefaultModelHandler.Tests.cs

示例6: NextInjectedViaProperty

        public void NextInjectedViaProperty()
        {
            var services = new ServiceCollection()
                .AddContributor<ISomeService>((sp, next) => new SomeService()
                {
                    Next = next(),
                    Value = 1,
                })
                .ChainPrevious<ISomeService, SomeService>()
                .MakeTransient<ISomeService>();

            var configuration = services.BuildApiConfiguration();
            var value = configuration.GetApiService<ISomeService>().Call();
            Assert.Equal("01", value);

            // Test expression compilation.
            value = configuration.GetApiService<ISomeService>().Call();
            Assert.Equal("01", value);
        }
开发者ID:kosinsky,项目名称:RESTier,代码行数:19,代码来源:ServiceConfiguration.Tests.cs

示例7: NextInjectedWithInheritedField

        public void NextInjectedWithInheritedField()
        {
            var services = new ServiceCollection()
                .MakeTransient<ISomeService>()
                .AddContributor<ISomeService>((sp, next) => new SomeService()
                {
                    Value = 2,
                })
                .AddInstance(new SomeService()
                {
                    Value = 0,
                })
                .ChainPrevious<ISomeService, SomeService4>();

            var configuration = services.BuildApiConfiguration();
            var value = configuration.GetApiService<ISomeService>().Call();
            Assert.Equal("4200", value);

            // Test expression compilation
            value = configuration.GetApiService<ISomeService>().Call();
            Assert.Equal("4200", value);
            value = configuration.GetApiService<ISomeService>().Call();
            Assert.Equal("4200", value);
        }
开发者ID:kosinsky,项目名称:RESTier,代码行数:24,代码来源:ServiceConfiguration.Tests.cs

示例8: ThrowOnNoServiceFound

        public void ThrowOnNoServiceFound()
        {
            var services = new ServiceCollection()
                .AddContributor<ISomeService>((sp, next) => new SomeService()
                {
                    Value = 1,
                })
                .MakeTransient<ISomeService>()
                .AddContributor<string>((sp, next) =>
                {
                    return "0";
                })
                .MakeTransient<string>()
                .ChainPrevious<ISomeService, SomeService3>();

            var configuration = services.BuildApiConfiguration();
            Assert.Throws<InvalidOperationException>(() =>
            {
                configuration.GetApiService<ISomeService>();
            });
        }
开发者ID:kosinsky,项目名称:RESTier,代码行数:21,代码来源:ServiceConfiguration.Tests.cs

示例9: MultiInjectionViaConstructor

        public void MultiInjectionViaConstructor()
        {
            var services = new ServiceCollection()
                .AddContributor<ISomeService>((sp, next) => new SomeService()
                {
                    Value = 1,
                })
                .MakeTransient<ISomeService>()
                .AddContributor<string>((sp, next) =>
                {
                    return "0";
                })
                .MakeTransient<string>()
                .ChainPrevious<ISomeService, SomeService3>()
                .AddInstance<SomeService>(new SomeService()
                {
                    Value = 2,
                });

            var configuration = services.BuildApiConfiguration();
            var value = configuration.GetApiService<ISomeService>().Call();
            Assert.Equal("0122", value);

            // Test expression compilation
            value = configuration.GetApiService<ISomeService>().Call();
            Assert.Equal("0122", value);
            value = configuration.GetApiService<ISomeService>().Call();
            Assert.Equal("0122", value);
        }
开发者ID:kosinsky,项目名称:RESTier,代码行数:29,代码来源:ServiceConfiguration.Tests.cs

示例10: DefaultValueInConstructorUsedIfNoService

        public void DefaultValueInConstructorUsedIfNoService()
        {
            var services = new ServiceCollection()
                .AddContributor<ISomeService>((sp, next) => new SomeService()
                {
                    Value = 2,
                })
                .MakeTransient<ISomeService>()
                .ChainPrevious<ISomeService, SomeService2>();

            var configuration = services.BuildApiConfiguration();
            var value = configuration.GetApiService<ISomeService>().Call();
            Assert.Equal("42", value);

            value = configuration.GetApiService<ISomeService>().Call();
            Assert.Equal("42", value);
            value = configuration.GetApiService<ISomeService>().Call();
            Assert.Equal("42", value);
        }
开发者ID:kosinsky,项目名称:RESTier,代码行数:19,代码来源:ServiceConfiguration.Tests.cs

示例11: ServiceInjectedViaProperty

        public void ServiceInjectedViaProperty()
        {
            var first = new SomeService()
            {
                Value = 42,
            };
            var services = new ServiceCollection()
                .MakeTransient<ISomeService>()
                .AddContributor<ISomeService>((sp, next) => first)
                .ChainPrevious<ISomeService, SomeService2>()
                .AddInstance<string>("Text");

            var configuration = services.BuildApiConfiguration();
            var expected = "Text42";
            var value = configuration.GetApiService<ISomeService>().Call();
            Assert.Equal(expected, value);

            value = configuration.GetApiService<ISomeService>().Call();
            Assert.Equal(expected, value);

            value = configuration.GetApiService<ISomeService>().Call();
            Assert.Equal(expected, value);

            Assert.NotEqual(
                configuration.GetApiService<ISomeService>(),
                configuration.GetApiService<ISomeService>());
        }
开发者ID:kosinsky,项目名称:RESTier,代码行数:27,代码来源:ServiceConfiguration.Tests.cs

示例12: ContextApiScopeWorksCorrectly

        public void ContextApiScopeWorksCorrectly()
        {
            var services = new ServiceCollection()
                .MakeScoped<ISomeService>()
                .ChainPrevious<ISomeService>(next => new SomeService());

            var configuration = services.BuildApiConfiguration();
            var service1 = configuration.GetApiService<ISomeService>();

            var context = new ApiContext(configuration);
            var service2 = context.GetApiService<ISomeService>();

            Assert.NotEqual(service1, service2);

            var context3 = new ApiContext(configuration);
            var service3 = context3.GetApiService<ISomeService>();

            Assert.NotEqual(service3, service2);
        }
开发者ID:kosinsky,项目名称:RESTier,代码行数:19,代码来源:ServiceConfiguration.Tests.cs


注:本文中的ServiceCollection.BuildApiConfiguration方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。