本文整理汇总了C#中ServiceCollection.GetRequiredService方法的典型用法代码示例。如果您正苦于以下问题:C# ServiceCollection.GetRequiredService方法的具体用法?C# ServiceCollection.GetRequiredService怎么用?C# ServiceCollection.GetRequiredService使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ServiceCollection
的用法示例。
在下文中一共展示了ServiceCollection.GetRequiredService方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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)));
}
示例2: NoCircularDependencySameTypeMultipleTimes
public void NoCircularDependencySameTypeMultipleTimes()
{
var serviceProvider = new ServiceCollection()
.AddTransient<NoCircularDependencySameTypeMultipleTimesA>()
.AddTransient<NoCircularDependencySameTypeMultipleTimesB>()
.AddTransient<NoCircularDependencySameTypeMultipleTimesC>()
.BuildServiceProvider();
var resolvedService = serviceProvider.GetRequiredService<NoCircularDependencySameTypeMultipleTimesA>();
Assert.NotNull(resolvedService);
}
示例3: NoCircularDependencyGeneric
public void NoCircularDependencyGeneric()
{
var serviceProvider = new ServiceCollection()
.AddSingleton(new SelfCircularDependencyGeneric<string>())
.AddTransient<SelfCircularDependencyGeneric<int>>()
.BuildServiceProvider();
// This will not throw because we are creating an instace of the first time
// using the parameterless constructor which has no circular dependency
var resolvedService = serviceProvider.GetRequiredService<SelfCircularDependencyGeneric<int>>();
Assert.NotNull(resolvedService);
}
示例4: SelfCircularDependencyGenericDirect
public void SelfCircularDependencyGenericDirect()
{
var serviceProvider = new ServiceCollection()
.AddTransient<SelfCircularDependencyGeneric<string>>()
.BuildServiceProvider();
var exception = Assert.Throws<InvalidOperationException>(() =>
serviceProvider.GetRequiredService<SelfCircularDependencyGeneric<string>>());
Assert.Equal(
Resources.FormatCircularDependencyException(typeof(SelfCircularDependencyGeneric<string>)),
exception.Message);
}
示例5: SelfCircularDependencyWithInterface
public void SelfCircularDependencyWithInterface()
{
var serviceProvider = new ServiceCollection()
.AddTransient<ISelfCircularDependencyWithInterface, SelfCircularDependencyWithInterface>()
.AddTransient<SelfCircularDependencyWithInterface>()
.BuildServiceProvider();
var exception = Assert.Throws<InvalidOperationException>(() =>
serviceProvider.GetRequiredService<SelfCircularDependencyWithInterface>());
Assert.Equal(
Resources.FormatCircularDependencyException(typeof(ISelfCircularDependencyWithInterface)),
exception.Message);
}
示例6: DirectCircularDependency
public void DirectCircularDependency()
{
var serviceProvider = new ServiceCollection()
.AddSingleton<DirectCircularDependencyA>()
.AddSingleton<DirectCircularDependencyB>()
.BuildServiceProvider();
var exception = Assert.Throws<InvalidOperationException>(() =>
serviceProvider.GetRequiredService<DirectCircularDependencyA>());
Assert.Equal(
Resources.FormatCircularDependencyException(typeof(DirectCircularDependencyA)),
exception.Message);
}
示例7: Can_specify_connection_in_OnConfiguring
public void Can_specify_connection_in_OnConfiguring()
{
var serviceProvider
= new ServiceCollection()
.AddScoped(p => new SqlConnection(SqlServerNorthwindContext.ConnectionString))
.AddDbContext<ConnectionInOnConfiguringContext>().BuildServiceProvider();
using (SqlServerNorthwindContext.GetSharedStore())
{
using (var context = serviceProvider.GetRequiredService<ConnectionInOnConfiguringContext>())
{
Assert.True(context.Customers.Any());
}
}
}
示例8: Throws_with_add_when_no_EF_services_use_Database
public void Throws_with_add_when_no_EF_services_use_Database()
{
var appServiceProivder = new ServiceCollection()
.AddDbContext<ConstructorTestContext1A>(
(p, b) => b.UseInternalServiceProvider(p))
.BuildServiceProvider();
using (var serviceScope = appServiceProivder
.GetRequiredService<IServiceScopeFactory>()
.CreateScope())
{
var context = serviceScope.ServiceProvider.GetService<ConstructorTestContext1A>();
Assert.Equal(
CoreStrings.NoEfServices,
Assert.Throws<InvalidOperationException>(() => context.Database.GetDbConnection()).Message);
}
}
示例9: Throws_with_add_when_no_provider_use_Database
public void Throws_with_add_when_no_provider_use_Database()
{
var appServiceProivder = new ServiceCollection()
.AddEntityFramework()
.AddDbContext<ConstructorTestContext1A>(
(p, b) => b.UseInternalServiceProvider(p))
.BuildServiceProvider();
using (var serviceScope = appServiceProivder
.GetRequiredService<IServiceScopeFactory>()
.CreateScope())
{
var context = serviceScope.ServiceProvider.GetService<ConstructorTestContext1A>();
Assert.Equal(
CoreStrings.NoProviderConfiguredFailedToResolveService(typeof(IRelationalConnection).FullName),
Assert.Throws<InvalidOperationException>(() => context.Database.GetDbConnection()).Message);
}
}
示例10: Can_replace_ModelSource_without_access_to_internals
[Fact] // Issue #943
public void Can_replace_ModelSource_without_access_to_internals()
{
var serviceProvider = new ServiceCollection()
.AddEntityFramework()
.AddInMemoryDatabase()
.AddDbContext<JustSomeContext>()
.ServiceCollection()
.AddSingleton<InMemoryModelSource, MyModelSource>()
.BuildServiceProvider();
using (var context = serviceProvider.GetRequiredService<JustSomeContext>())
{
var model = context.Model;
Assert.Equal("Us!", model["AllYourModelAreBelongTo"]);
Assert.Equal("Us!", model.GetEntityTypes().Single(e => e.DisplayName() == "Base")["AllYourBaseAreBelongTo"]);
Assert.Contains("Peak", model.GetEntityTypes().Select(e => e.DisplayName()));
}
}
示例11: Throws_with_add_when_non_relational_provider_in_use547424486
public void Throws_with_add_when_non_relational_provider_in_use547424486()
{
var appServiceProivder = new ServiceCollection()
.AddEntityFrameworkSqlServer()
.AddEntityFrameworkInMemoryDatabase()
.AddDbContext<ConstructorTestContext1A>(
(p, b) => b.UseInMemoryDatabase().UseInternalServiceProvider(p))
.BuildServiceProvider();
using (var serviceScope = appServiceProivder
.GetRequiredService<IServiceScopeFactory>()
.CreateScope())
{
var context = serviceScope.ServiceProvider.GetService<ConstructorTestContext1A>();
Assert.Equal(
RelationalStrings.RelationalNotInUse,
Assert.Throws<InvalidOperationException>(() => context.Database.GetDbConnection()).Message);
}
}
示例12: Can_depend_on_DbContextOptions
public void Can_depend_on_DbContextOptions()
{
var serviceProvider
= new ServiceCollection()
.AddScoped(p => new SqlConnection(SqlServerNorthwindContext.ConnectionString))
.AddDbContext<OptionsContext>()
.BuildServiceProvider();
using (SqlServerNorthwindContext.GetSharedStore())
{
using (var context = serviceProvider.GetRequiredService<OptionsContext>())
{
Assert.True(context.Customers.Any());
}
}
}
示例13: Can_register_multiple_context_types
public void Can_register_multiple_context_types()
{
var serviceProvider = new ServiceCollection()
.AddDbContext<MultipleContext1>()
.AddDbContext<MultipleContext2>()
.BuildServiceProvider();
using (SqlServerNorthwindContext.GetSharedStore())
{
using (var context = serviceProvider.GetRequiredService<MultipleContext1>())
{
Assert.True(context.Customers.Any());
}
using (var context = serviceProvider.GetRequiredService<MultipleContext2>())
{
Assert.False(context.Customers.Any());
}
}
}
示例14: Can_depend_on_non_generic_options_when_only_one_context
public void Can_depend_on_non_generic_options_when_only_one_context()
{
var serviceProvider
= new ServiceCollection()
.AddDbContext<NonGenericOptionsContext>()
.BuildServiceProvider();
using (SqlServerNorthwindContext.GetSharedStore())
{
using (var context = serviceProvider.GetRequiredService<NonGenericOptionsContext>())
{
Assert.True(context.Customers.Any());
}
}
}
示例15: Can_select_appropriate_provider_when_multiple_registered
public void Can_select_appropriate_provider_when_multiple_registered()
{
var serviceProvider
= new ServiceCollection()
.AddScoped<SomeService>()
.AddDbContext<MultipleProvidersContext>()
.BuildServiceProvider();
using (SqlServerNorthwindContext.GetSharedStore())
{
MultipleProvidersContext context1;
MultipleProvidersContext context2;
using (var serviceScope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
using (context1 = serviceScope.ServiceProvider.GetRequiredService<MultipleProvidersContext>())
{
context1.UseSqlServer = true;
Assert.True(context1.Customers.Any());
}
using (var context1B = serviceScope.ServiceProvider.GetRequiredService<MultipleProvidersContext>())
{
Assert.Same(context1, context1B);
}
var someService = serviceScope.ServiceProvider.GetRequiredService<SomeService>();
Assert.Same(context1, someService.Context);
}
using (var serviceScope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
using (context2 = serviceScope.ServiceProvider.GetRequiredService<MultipleProvidersContext>())
{
context2.UseSqlServer = false;
Assert.False(context2.Customers.Any());
}
using (var context2B = serviceScope.ServiceProvider.GetRequiredService<MultipleProvidersContext>())
{
Assert.Same(context2, context2B);
}
var someService = serviceScope.ServiceProvider.GetRequiredService<SomeService>();
Assert.Same(context2, someService.Context);
}
Assert.NotSame(context1, context2);
}
}