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


C# ServiceCollection.GetRequiredService方法代码示例

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


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

示例1: Duplicate_entries_are_not_created_for_navigations_to_dependant

        public void Duplicate_entries_are_not_created_for_navigations_to_dependant()
        {
            var serviceProvider = new ServiceCollection()
                .AddEntityFramework()
                .AddInMemoryStore()
                .AddDbContext<GoTContext>()
                .ServiceCollection()
                .BuildServiceProvider();

            using (var context = serviceProvider.GetRequiredService<GoTContext>())
            {
                context.ConfigAction = (modelBuilder) =>
                    {

                        modelBuilder.Entity<Person>().Reference(p => p.SiblingReverse).InverseCollection(p => p.Siblings).Required(false);
                        modelBuilder.Entity<Person>().Reference(p => p.Lover).InverseReference(p => p.LoverReverse).Required(false);
                        return 0;
                    };

                var model = context.Model;
                var entityType = model.EntityTypes.First();

                Assert.Equal("'Person' {'LoverId'} -> 'Person' {'Id'}", entityType.GetForeignKeys().First().ToString());
                Assert.Equal("'Person' {'SiblingReverseId'} -> 'Person' {'Id'}", entityType.GetForeignKeys().Skip(1).First().ToString());
            }
        }
开发者ID:thegido,项目名称:EntityFramework,代码行数:26,代码来源:NavigationTest.cs

示例2: Non_generic_GetRequiredService_throws_useful_exception_if_service_not_registered

        public void Non_generic_GetRequiredService_throws_useful_exception_if_service_not_registered()
        {
            var serviceProvider = new ServiceCollection().BuildServiceProvider();

            Assert.Throws<InvalidOperationException>(
                () => serviceProvider.GetRequiredService(typeof(IPilkington)));
        }
开发者ID:thegido,项目名称:EntityFramework,代码行数:7,代码来源:ServiceProviderExtensionsTest.cs

示例3: ServicesArePerRequest

        public async Task ServicesArePerRequest()
        {
            var services = new ServiceCollection()
                .AddScoped<ITestService, TestService>()
                .BuildServiceProvider();
            var builder = new ApplicationBuilder(services);

            builder.Use(async (ctx, next) =>
            {
                var serviceScopeFactory = services.GetRequiredService<IServiceScopeFactory>();
                using (var serviceScope = serviceScopeFactory.CreateScope())
                {
                    var priorApplicationServices = ctx.ApplicationServices;
                    var priorRequestServices = ctx.ApplicationServices;
                    ctx.ApplicationServices = services;
                    ctx.RequestServices = serviceScope.ServiceProvider;
                    try
                    {
                        await next();
                    }
                    finally
                    {
                        ctx.ApplicationServices = priorApplicationServices;
                        ctx.RequestServices = priorRequestServices;
                    }
                }
            });

            var testServicesA = new List<ITestService>();
            builder.Use(async (HttpContext ctx, Func<Task> next, ITestService testService) =>
            {
                testServicesA.Add(testService);
                await next();
            });

            var testServicesB = new List<ITestService>();
            builder.Use<ITestService>(async (ctx, next, testService) =>
            {
                testServicesB.Add(testService);
                await next();
            });

            var app = builder.Build();
            await app(new DefaultHttpContext());
            await app(new DefaultHttpContext());

            Assert.Equal(2, testServicesA.Count);
            Assert.IsType<TestService>(testServicesA[0]);
            Assert.IsType<TestService>(testServicesA[1]);

            Assert.Equal(2, testServicesB.Count);
            Assert.IsType<TestService>(testServicesB[0]);
            Assert.IsType<TestService>(testServicesB[1]);

            Assert.Same(testServicesA[0], testServicesB[0]);
            Assert.Same(testServicesA[1], testServicesB[1]);

            Assert.NotSame(testServicesA[0], testServicesA[1]);
            Assert.NotSame(testServicesB[0], testServicesB[1]);
        }
开发者ID:EgoDust,项目名称:HttpAbstractions,代码行数:60,代码来源:UseWithServicesTests.cs

示例4: Can_replace_ModelSource_without_access_to_internals

        [Fact] // Issue #943
        public void Can_replace_ModelSource_without_access_to_internals()
        {
            var serviceProvider = new ServiceCollection()
                .AddEntityFramework()
                .AddInMemoryStore()
                .AddDbContext<JustSomeContext>()
                .ServiceCollection()
                .AddSingleton<IInMemoryModelSource, MyModelSource>()
                .BuildServiceProvider();

            using (var context = serviceProvider.GetRequiredService<JustSomeContext>())
            {
                var model = context.Model;

                Assert.Equal("Us!", model["AllYourModelAreBelongTo"]);
                Assert.Equal("Us!", model.EntityTypes.Single(e => e.DisplayName() == "Base")["AllYourBaseAreBelongTo"]);
                Assert.Contains("Peak", model.EntityTypes.Select(e => e.DisplayName()));
            }
        }
开发者ID:thegido,项目名称:EntityFramework,代码行数:20,代码来源:ModelSourceTest.cs

示例5: Context_cannot_be_used_in_OnConfiguring

        public void Context_cannot_be_used_in_OnConfiguring()
        {
            var serviceProvider = new ServiceCollection()
                .AddEntityFramework()
                .AddInMemoryStore()
                .AddDbContext<UseInOnConfiguringContext>()
                .ServiceCollection()
                .BuildServiceProvider();

            using (var context = serviceProvider.GetRequiredService<UseInOnConfiguringContext>())
            {
                Assert.Equal(
                    Strings.RecursiveOnConfiguring,
                    Assert.Throws<InvalidOperationException>(() => context.Products.ToList()).Message);
            }
        }
开发者ID:thegido,项目名称:EntityFramework,代码行数:16,代码来源:DbContextTest.cs

示例6: Model_cannot_be_used_in_OnModelCreating

        public void Model_cannot_be_used_in_OnModelCreating()
        {
            var serviceProvider = new ServiceCollection()
                .AddEntityFramework()
                .AddInMemoryDatabase()
                .AddDbContext<UseModelInOnModelCreatingContext>()
                .GetService()
                .BuildServiceProvider();

            using (var context = serviceProvider.GetRequiredService<UseModelInOnModelCreatingContext>())
            {
                Assert.Equal(
                    Strings.RecursiveOnModelCreating,
                    Assert.Throws<InvalidOperationException>(() => context.Model).Message);
            }
        }
开发者ID:rbenhassine2,项目名称:EntityFramework,代码行数:16,代码来源:DbContextTest.cs


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