本文整理汇总了C#中Microsoft.Framework.DependencyInjection.ServiceCollection.AddTransient方法的典型用法代码示例。如果您正苦于以下问题:C# ServiceCollection.AddTransient方法的具体用法?C# ServiceCollection.AddTransient怎么用?C# ServiceCollection.AddTransient使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.Framework.DependencyInjection.ServiceCollection
的用法示例。
在下文中一共展示了ServiceCollection.AddTransient方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestHelloNonGenericServiceDecoratorNoInterface
public void TestHelloNonGenericServiceDecoratorNoInterface()
{
var services = new ServiceCollection();
services.AddInstance<IHelloService>(new HelloService());
services.AddSingleton<IHelloService>(sp => new HelloService());
services.AddScoped<IHelloService>(sp => new HelloService());
services.AddTransient<IHelloService>(sp => new HelloService());
services.AddSingleton<IHelloService, HelloService>();
services.AddScoped<IHelloService, HelloService>();
services.AddTransient<IHelloService, HelloService>();
services.AddDecorator(typeof(IHelloService), (sp, s) => new HelloServiceDecoratorNoInterface((IHelloService)s));
var provider = services.BuildServiceProvider();
var helloServices = provider.GetRequiredServices<IHelloService>();
Assert.NotNull(helloServices);
var collection = helloServices as IHelloService[] ?? helloServices.ToArray();
Assert.Equal(7, collection.Length);
Assert.NotEmpty(collection);
foreach (var helloService in collection)
{
Assert.NotNull(helloService);
Assert.Equal("Decorated without interface: Hello world.", helloService.SayHello("world"));
}
}
示例2: GetDefaultServices
public static IServiceCollection GetDefaultServices()
{
var services = new ServiceCollection();
services.AddTransient<IRequestAuthorizerProvider, DefaultRequestAuthorizerProvider>();
services.AddTransient<IRequestHandlerProvider, DefaultRequestHandlerProvider>();
services.AddTransient<IRequestRuntimeProvider, DefaultRequestRuntimeProvider>();
return services;
}
示例3: RegisterServices
private void RegisterServices()
{
ServiceCollection services = new ServiceCollection();
services.AddSingleton<IBooksService, BooksService>();
services.AddSingleton<IMessagingService, WPFMessagingService>();
services.AddTransient<BooksViewModel>();
services.AddTransient<BookViewModel>();
services.AddTransient<RandomViewModel>();
Container = services.BuildServiceProvider();
}
示例4: CanCreateRoleWithSingletonManager
public async Task CanCreateRoleWithSingletonManager()
{
var services = new ServiceCollection();
services.AddEntityFramework().AddInMemoryStore();
services.AddTransient<InMemoryContext>();
services.AddTransient<IRoleStore<IdentityRole>, RoleStore<IdentityRole, InMemoryContext>>();
services.AddIdentity<IdentityUser, IdentityRole>();
services.AddSingleton<RoleManager<IdentityRole>>();
var provider = services.BuildServiceProvider();
var manager = provider.GetRequiredService<RoleManager<IdentityRole>>();
Assert.NotNull(manager);
IdentityResultAssert.IsSuccess(await manager.CreateAsync(new IdentityRole("someRole")));
}
示例5: RegisterServices
public static IServiceProvider RegisterServices(IEnumerable<IViewLocator> viewLocators, IEnumerable<IStaticFileLocator> staticFileLocators)
{
// register types for IServiceProvider here
var serviceCollection = new ServiceCollection();
serviceCollection.AddInstance(typeof(IEnumerable<IViewLocator>), viewLocators);
serviceCollection.AddInstance(typeof(IEnumerable<IStaticFileLocator>), staticFileLocators);
serviceCollection.AddTransient<IViewLocatorService, ViewLocatorService>();
serviceCollection.AddTransient<IStaticFileGeneratorService, StaticFileGeneratorService>();
serviceCollection.AddTransient<IControllerRewriterService, ControllerRewriterService>();
serviceCollection.AddTransient<IControllerGeneratorService, ControllerGeneratorService>();
serviceCollection.AddTransient<R4MvcGenerator, R4MvcGenerator>();
return serviceCollection.BuildServiceProvider();
}
示例6: 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;
}
示例7: BuildServiceProvider
// Composition root
private IServiceProvider BuildServiceProvider(Options options, IConfigurationSection queueConfig)
{
var services = new ServiceCollection().AddLogging();
services.AddSingleton<IMessageHandlerFactory, MessageHandlerFactory>();
switch (options.QueueType)
{
case "zeromq":
services.AddZeroMq(queueConfig);
break;
case "msmq":
services.AddMsmq(queueConfig);
break;
case "azure":
services.AddAzure(queueConfig);
break;
default:
throw new Exception($"Could not resolve queue type {options.QueueType}");
}
if (!string.IsNullOrWhiteSpace(options.Handler))
{
services.AddTransient(typeof(IMessageHandler), Type.GetType(options.Handler));
}
var provider = services.BuildServiceProvider();
// configure
var loggerFactory = provider.GetRequiredService<ILoggerFactory>();
loggerFactory.MinimumLevel = LogLevel.Debug;
loggerFactory.AddConsole(loggerFactory.MinimumLevel);
return provider;
}
示例8: Invoke
public Task Invoke(HttpContext httpContext)
{
var services = new ServiceCollection();
services.AddTransient<TestService>();
httpContext.RequestServices = services.BuildServiceProvider();
return _next.Invoke(httpContext);
}
示例9: DefineServices
public IServiceCollection DefineServices()
{
var services = new ServiceCollection();
services.AddSingleton<ICall, CallOne>();
services.AddScoped<ICall, CallTwo>();
services.AddTransient<ICall, CallThree>();
return services;
}
示例10: ConfigureServices
public void ConfigureServices()
{
ServiceCollection sc = new ServiceCollection();
sc.AddEntityFramework()
.AddSqlite()
.AddDbContext<StarDbContext>();
sc.AddTransient<StarDbContext>();
sc.BuildServiceProvider();
}
示例11: 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();
}
示例12: GetDefaultServices
public static IEnumerable<ServiceDescriptor> GetDefaultServices()
{
var services = new ServiceCollection();
//
// Discovery & Reflection.
//
services.AddTransient<ITypeActivator, DefaultTypeActivator>();
services.AddTransient<ITypeSelector, DefaultTypeSelector>();
services.AddTransient<IAssemblyProvider, DefaultAssemblyProvider>();
services.AddTransient<ITypeService, DefaultTypeService>();
// TODO: consider making above singleton
//
// Context.
//
services.AddTransient(typeof(IContextData<>), typeof(ContextData<>));
//
// Messages.
//
services.AddSingleton<IMessageConverter, DefaultMessageConverter>();
//
// JSON.Net.
//
services.AddTransient<JsonSerializer, JsonSerializer>();
return services;
}
示例13: Main
// So ServiceProvider = Default IServiceProvider
// Extensions are in Microsoft.Framework.DependencyInjection.Interfaces.ServiceProviderExtensions
public void Main(string[] args)
{
IConfiguration configuration = new ConfigurationBuilder(Environment.CurrentDirectory).AddJsonFile("config.json").Build();
var azureConfig = ConfigurationBinder.Bind<AzureConfig>(configuration.GetConfigurationSection("Azure"));
IServiceCollection services = new ServiceCollection();
services.AddTransient((service) => azureConfig);
services.AddDocumentDbFun();
var provider = services.BuildServiceProvider();
var docDbRepo = provider.GetService<IDocumentDbRepository>();
//Task.WaitAll(docDbRepo.CreateDocDb());
var documents = docDbRepo.ReadDocuments();
}
示例14: CreateContainer
public IServiceProvider CreateContainer(ShellSettings settings, ShellBlueprint blueprint) {
ServiceCollection serviceCollection = new ServiceCollection();
serviceCollection.AddScoped<IOrchardShell, DefaultOrchardShell>();
serviceCollection.AddScoped<IRouteBuilder, DefaultShellRouteBuilder>();
serviceCollection.AddInstance(settings);
serviceCollection.AddInstance(blueprint.Descriptor);
serviceCollection.AddInstance(blueprint);
serviceCollection.AddMvc();
serviceCollection.Configure<RazorViewEngineOptions>(options => {
var expander = new ModuleViewLocationExpander();
options.ViewLocationExpanders.Add(expander);
});
var p = _serviceProvider.GetService<IOrchardLibraryManager>();
serviceCollection.AddInstance<IAssemblyProvider>(new DefaultAssemblyProviderTest(p, _serviceProvider, _serviceProvider.GetService<IAssemblyLoaderContainer>()));
foreach (var dependency in blueprint.Dependencies) {
foreach (var interfaceType in dependency.Type.GetInterfaces()
.Where(itf => typeof(IDependency).IsAssignableFrom(itf))) {
Logger.Debug("Type: {0}, Interface Type: {1}", dependency.Type, interfaceType);
if (typeof(ISingletonDependency).IsAssignableFrom(interfaceType)) {
serviceCollection.AddSingleton(interfaceType, dependency.Type);
}
else if (typeof(IUnitOfWorkDependency).IsAssignableFrom(interfaceType)) {
serviceCollection.AddScoped(interfaceType, dependency.Type);
}
else if (typeof (ITransientDependency).IsAssignableFrom(interfaceType)) {
serviceCollection.AddTransient(interfaceType, dependency.Type);
}
else {
serviceCollection.AddScoped(interfaceType, dependency.Type);
}
}
}
//foreach (var item in blueprint.Controllers) {
// var serviceKeyName = (item.AreaName + "/" + item.ControllerName).ToLowerInvariant();
// var serviceKeyType = item.Type;
// serviceCollection.AddScoped(serviceKeyType);
//}
return BuildFallbackServiceProvider(
serviceCollection,
_serviceProvider);
}
示例15: GetDefaultServices
public static IServiceCollection GetDefaultServices()
{
var services = new ServiceCollection();
//
// Options
//
services.AddTransient<IConfigureOptions<GlimpseAgentWebOptions>, GlimpseAgentWebOptionsSetup>();
services.AddSingleton<IRequestIgnorerUriProvider, DefaultRequestIgnorerUriProvider>();
services.AddSingleton<IRequestIgnorerStatusCodeProvider, DefaultRequestIgnorerStatusCodeProvider>();
services.AddSingleton<IRequestIgnorerContentTypeProvider, DefaultRequestIgnorerContentTypeProvider>();
services.AddSingleton<IRequestIgnorerProvider, DefaultRequestIgnorerProvider>();
services.AddSingleton<IRequestProfilerProvider, DefaultRequestProfilerProvider>();
return services;
}