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


C# ServiceCollection.AddTransient方法代码示例

本文整理汇总了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;
        }
开发者ID:mike-kaufman,项目名称:Glimpse.Prototype,代码行数:35,代码来源:CommonServices.cs

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

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

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

示例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>());
        }
开发者ID:Cream2015,项目名称:MyTested.AspNetCore.Mvc,代码行数:12,代码来源:ServiceCollectionExtensionsTests.cs

示例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);
        }
开发者ID:stevenliujw,项目名称:DependencyInjection,代码行数:15,代码来源:ServiceProviderContainerTests.cs

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

示例9: RegisterServices

        private IServiceProvider RegisterServices()
        {
            var serviceCollection = new ServiceCollection();
            serviceCollection.AddSingleton<IMessageContract, MyMessageService>();
            serviceCollection.AddTransient<MainViewModel>();


            return serviceCollection.BuildServiceProvider();
        }
开发者ID:CNinnovation,项目名称:WPFWorkshopFeb2016,代码行数:9,代码来源:App.xaml.cs

示例10: BuildServiceProvider

        private static IServiceProvider BuildServiceProvider()
        {
            var services = new ServiceCollection();
            services.AddTransient(_ => GatherParameters())
                .AddLogging()
                .AddMapBuilding()
                .AddMapProcessing()
                .AddMapPainting();

            return services.BuildServiceProvider();
        }
开发者ID:CatSkald,项目名称:Roguelike,代码行数:11,代码来源:Program.cs

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

示例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>());
        }
开发者ID:Cream2015,项目名称:MyTested.AspNetCore.Mvc,代码行数:12,代码来源:ServiceCollectionExtensionsTests.cs

示例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>());
        }
开发者ID:Cream2015,项目名称:MyTested.AspNetCore.Mvc,代码行数:12,代码来源:ServiceCollectionExtensionsTests.cs

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


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