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


C# ServiceCollection.AddTransient方法代码示例

本文整理汇总了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"));
            }
        }
开发者ID:sherif-elmetainy,项目名称:DI,代码行数:29,代码来源:DecoratorTests.cs

示例2: GetDefaultServices

        public static IServiceCollection GetDefaultServices()
        {
            var services = new ServiceCollection();
             
            services.AddTransient<IRequestAuthorizerProvider, DefaultRequestAuthorizerProvider>();
            services.AddTransient<IRequestHandlerProvider, DefaultRequestHandlerProvider>();
            services.AddTransient<IRequestRuntimeProvider, DefaultRequestRuntimeProvider>();

            return services;
        }
开发者ID:rndthoughts,项目名称:Glimpse.Prototype,代码行数:10,代码来源:GlimpseWebServices.cs

示例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();
        }
开发者ID:CNinnovation,项目名称:WPFWorkshop,代码行数:11,代码来源:App.xaml.cs

示例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")));
 }
开发者ID:stormwild,项目名称:Identity,代码行数:13,代码来源:RoleStoreTest.cs

示例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();
 }
开发者ID:kazuk,项目名称:R4MVC,代码行数:13,代码来源:IocConfig.cs

示例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;
        }
开发者ID:randa1,项目名称:orleans,代码行数:16,代码来源:ConfigureStartupBuilder.cs

示例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;
        }
开发者ID:Kieranties,项目名称:MessagingPlayground,代码行数:36,代码来源:Program.cs

示例8: Invoke

            public Task Invoke(HttpContext httpContext)
            {
                var services = new ServiceCollection();
                services.AddTransient<TestService>();
                httpContext.RequestServices = services.BuildServiceProvider();

                return _next.Invoke(httpContext);
            }
开发者ID:Kagamine,项目名称:Hosting,代码行数:8,代码来源:TestServerTests.cs

示例9: DefineServices

 public IServiceCollection DefineServices()
 {
     var services = new ServiceCollection();
     services.AddSingleton<ICall, CallOne>();
     services.AddScoped<ICall, CallTwo>();
     services.AddTransient<ICall, CallThree>();
     
     return services;
 }
开发者ID:Tragetaschen,项目名称:Entropy,代码行数:9,代码来源:Startup.cs

示例10: ConfigureServices

        public void ConfigureServices()
        {
            ServiceCollection sc = new ServiceCollection();
            sc.AddEntityFramework()
                .AddSqlite()
                .AddDbContext<StarDbContext>();

            sc.AddTransient<StarDbContext>();

            sc.BuildServiceProvider();
        }
开发者ID:SharpStar,项目名称:Star2,代码行数:11,代码来源:Startup.cs

示例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();
            }
开发者ID:vebin,项目名称:Brochard,代码行数:13,代码来源:ServiceExtensions.cs

示例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;
        }
开发者ID:rndthoughts,项目名称:Glimpse.Prototype,代码行数:30,代码来源:GlimpseServices.cs

示例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();
        }
开发者ID:Nosmadas,项目名称:DocumentDbFun,代码行数:16,代码来源:Program.cs

示例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);
        }
开发者ID:night-king,项目名称:OrchardNoCMS,代码行数:50,代码来源:ShellContainerFactory.cs

示例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;
        }
开发者ID:rndthoughts,项目名称:Glimpse.Prototype,代码行数:16,代码来源:GlimpseAgentWebServices.cs


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