本文整理汇总了C#中ServiceCollection.AddTransient方法的典型用法代码示例。如果您正苦于以下问题:C# ServiceCollection.AddTransient方法的具体用法?C# ServiceCollection.AddTransient怎么用?C# ServiceCollection.AddTransient使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ServiceCollection
的用法示例。
在下文中一共展示了ServiceCollection.AddTransient方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetDefaultServices
public static IEnumerable<ServiceDescriptor> GetDefaultServices()
{
var services = new ServiceCollection();
//
// Discovery & Reflection.
//
// TODO: consider making above singleton
services.AddTransient<IAssemblyProvider, DefaultAssemblyProvider>();
services.AddTransient<ITypeActivator, DefaultTypeActivator>();
services.AddTransient<ITypeSelector, DefaultTypeSelector>();
services.AddTransient<ITypeService, DefaultTypeService>();
//
// Extensions
//
services.AddSingleton<IExtensionProvider<IRegisterServices>, DefaultExtensionProvider<IRegisterServices>>();
#if DNX
services.AddSingleton<IExtensionProvider<IRegisterMiddleware>, DefaultExtensionProvider<IRegisterMiddleware>>();
#endif
//
// Context.
//
services.AddSingleton(typeof(IContextData<>), typeof(ContextData<>));
services.AddSingleton<IGlimpseContextAccessor, DefaultGlimpseContextAccessor>();
//
// JSON.Net.
//
services.AddTransient<JsonSerializer, JsonSerializer>();
services.AddSingleton<IJsonSerializerProvider, DefaultJsonSerializerProvider>();
return services;
}
示例2: RegisterServices
private IServiceProvider RegisterServices()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddTransient<BooksViewModel>();
serviceCollection.AddTransient<BookViewModel>();
serviceCollection.AddSingleton<IBooksService, BooksService>();
// serviceCollection.AddSingleton<IBooksRepository, BooksSampleRepository>();
return serviceCollection.BuildServiceProvider();
}
示例3: OnStartup
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
IServiceCollection services = new ServiceCollection();
services.AddTransient<ChatViewModel>();
services.AddTransient<GroupChatViewModel>();
services.AddSingleton<IMessagingService, MessagingService>();
Container = services.BuildServiceProvider();
}
示例4: RegisterDIContainer
private void RegisterDIContainer()
{
var services = new ServiceCollection();
services.AddTransient<BooksViewModel>();
services.AddTransient<BookViewModel>();
services.AddSingleton<IBooksRepository, BooksRepository>();
services.AddSingleton<IEventAggregator, EventAggregator>();
Container = services.BuildServiceProvider();
}
示例5: RemoveTransientShouldRemoveServiceByTypeOnlyInterface
public void RemoveTransientShouldRemoveServiceByTypeOnlyInterface()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddTransient<IInjectedService, InjectedService>();
serviceCollection.AddTransient<IInjectedService, ReplaceableInjectedService>();
Assert.NotNull(serviceCollection.BuildServiceProvider().GetService<IInjectedService>());
serviceCollection.RemoveTransient(typeof(IInjectedService));
Assert.Null(serviceCollection.BuildServiceProvider().GetService<IInjectedService>());
}
示例6: SurveyStoreTests
public SurveyStoreTests()
{
var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
optionsBuilder.UseInMemoryDatabase();
_options = optionsBuilder.Options;
_serviceCollection = new ServiceCollection();
_serviceCollection
.AddEntityFramework()
.AddInMemoryDatabase();
_serviceCollection.AddTransient<ApplicationDbContext>(provider => new ApplicationDbContext(provider, _options));
_serviceCollection.AddTransient<SqlServerSurveyStore>();
}
开发者ID:Azure-Samples,项目名称:guidance-identity-management-for-multitenant-apps,代码行数:13,代码来源:SurveyStoreTests.cs
示例7: RethrowOriginalExceptionFromConstructor
public void RethrowOriginalExceptionFromConstructor()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddTransient<ClassWithThrowingEmptyCtor>();
serviceCollection.AddTransient<ClassWithThrowingCtor>();
serviceCollection.AddTransient<IFakeService, FakeService>();
var provider = serviceCollection.BuildServiceProvider();
var ex1 = Assert.Throws<Exception>(() => provider.GetService<ClassWithThrowingEmptyCtor>());
Assert.Equal(nameof(ClassWithThrowingEmptyCtor), ex1.Message);
var ex2 = Assert.Throws<Exception>(() => provider.GetService<ClassWithThrowingCtor>());
Assert.Equal(nameof(ClassWithThrowingCtor), ex2.Message);
}
示例8: RegisterSystemTypes
private static IServiceCollection RegisterSystemTypes()
{
//
// Register the system classes and grains in this method.
//
IServiceCollection serviceCollection = new ServiceCollection();
serviceCollection.AddTransient<ManagementGrain>();
serviceCollection.AddTransient<GrainBasedMembershipTable>();
serviceCollection.AddTransient<GrainBasedReminderTable>();
serviceCollection.AddTransient<PubSubRendezvousGrain>();
serviceCollection.AddTransient<MemoryStorageGrain>();
return serviceCollection;
}
示例9: RegisterServices
private IServiceProvider RegisterServices()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton<IMessageContract, MyMessageService>();
serviceCollection.AddTransient<MainViewModel>();
return serviceCollection.BuildServiceProvider();
}
示例10: BuildServiceProvider
private static IServiceProvider BuildServiceProvider()
{
var services = new ServiceCollection();
services.AddTransient(_ => GatherParameters())
.AddLogging()
.AddMapBuilding()
.AddMapProcessing()
.AddMapPainting();
return services.BuildServiceProvider();
}
示例11: PopulatingTheContainerMoreThanOnceThrows
public void PopulatingTheContainerMoreThanOnceThrows()
{
var services = new ServiceCollection();
services.AddTransient<IFakeService, FakeService>();
var container = new Container();
container.Configure(config => config.Populate(services));
Assert.Throws<InvalidOperationException>(() => container.Populate(services));
}
开发者ID:structuremap,项目名称:StructureMap.Microsoft.DependencyInjection,代码行数:12,代码来源:StructureMapContainerTests.cs
示例12: WrappingServiceProvider
// Need full wrap for generics like IOptions
public WrappingServiceProvider(IServiceProvider fallback, IServiceCollection replacedServices)
{
var services = new ServiceCollection();
var manifest = fallback.GetRequiredService<IRuntimeServices>();
foreach (var service in manifest.Services) {
services.AddTransient(service, sp => fallback.GetService(service));
}
services.Add(replacedServices);
_services = services.BuildServiceProvider();
}
示例13: RemoveShouldRemoveServiceByGenericOnlyInterface
public void RemoveShouldRemoveServiceByGenericOnlyInterface()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddTransient<IInjectedService, InjectedService>();
serviceCollection.AddSingleton<IInjectedService, ReplaceableInjectedService>();
Assert.NotNull(serviceCollection.BuildServiceProvider().GetService<IInjectedService>());
serviceCollection.Remove<IInjectedService>();
Assert.Null(serviceCollection.BuildServiceProvider().GetService<IInjectedService>());
}
示例14: RemoveShouldRemoveServiceByGenericAndImplementation
public void RemoveShouldRemoveServiceByGenericAndImplementation()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddTransient<IInjectedService, InjectedService>();
serviceCollection.AddSingleton<IInjectedService, ReplaceableInjectedService>();
Assert.NotNull(serviceCollection.BuildServiceProvider().GetService<IInjectedService>());
serviceCollection.Remove<IInjectedService, ReplaceableInjectedService>();
Assert.IsAssignableFrom<InjectedService>(serviceCollection.BuildServiceProvider().GetService<IInjectedService>());
}
示例15: InitializeServices
private void InitializeServices()
{
const string ConnectionString = @"server=(localdb)\MSSQLLocalDb;database=Books;trusted_connection=true";
var services = new ServiceCollection();
services.AddTransient<BooksService>();
services.AddDbContext<BooksContext>(options =>
options.UseSqlServer(ConnectionString));
Container = services.BuildServiceProvider();
}