本文整理汇总了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());
}
}
示例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)));
}
示例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]);
}
示例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()));
}
}
示例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);
}
}
示例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);
}
}