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


C# IServiceCollection.AddTransient方法代码示例

本文整理汇总了C#中IServiceCollection.AddTransient方法的典型用法代码示例。如果您正苦于以下问题:C# IServiceCollection.AddTransient方法的具体用法?C# IServiceCollection.AddTransient怎么用?C# IServiceCollection.AddTransient使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IServiceCollection的用法示例。


在下文中一共展示了IServiceCollection.AddTransient方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ConfigureServices

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.AddApplicationInsightsTelemetry(Configuration);

            services.AddEntityFramework()
                .AddSqlServer()
                .AddDbContext<ApplicationDbContext>(options =>
                    options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));

            //var connection = @"Server=DESKTOP-2N2TC81;Database=NeedleWork2016;Trusted_Connection=True;";
            var connection = @"Data Source=10.10.200.62;Initial Catalog=NeedleWork2016;Persist Security Info=True;User ID=needlework2016;Password=Qwerty1!";
            services.AddEntityFramework()
                .AddDbContext<NeedleWork2016Context>(options =>
                    options.UseSqlServer(connection));

            services.AddIdentity<ApplicationUser, IdentityRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders();

            services.AddMvc();
            services.AddMvc();
            services.AddMvc(options =>
            {
                options.Filters.Add(new ConvertToPdfFilterAttribute());
                options.Filters.Add(new CultureAttribute());
            });

            services.AddTransient<IRepositoryContainer, RepositoryContainer>();
            services.AddTransient<IPaletteRepository, PaletteRepository>();
        }
开发者ID:VladZernov,项目名称:needlework,代码行数:32,代码来源:Startup.cs

示例2: ConfigureServices

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.AddApplicationInsightsTelemetry(Configuration);

            services.AddEntityFramework()
                .AddSqlServer()
                .AddDbContext<ApplicationDbContext>(options =>
                    options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));

            services.AddIdentity<ApplicationUser, IdentityRole>(o =>
            {
                o.Password.RequireDigit = false;
                o.Password.RequireLowercase = false;
                o.Password.RequireUppercase = false;
                o.Password.RequireNonLetterOrDigit = false; ;
                o.Password.RequiredLength = 6;
            })
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

            services.AddMvc();

            // Add application services.
            services.AddTransient<IEmailSender, AuthMessageSender>();
            services.AddTransient<ISmsSender, AuthMessageSender>();
            services.AddScoped<IArticleRepository, ArticleRepository>();
        }
开发者ID:levinhtin,项目名称:aspnet-blog,代码行数:29,代码来源:Startup.cs

示例3: ConfigureServices

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.AddEntityFramework()
                .AddSqlite()
                .AddDbContext<ApplicationDbContext>();

            services.AddTransient<ApplicationDbContext, ApplicationDbContext>();


            services.AddIdentity<ApplicationUser, IdentityRole>(opt =>
            {
                opt.Password.RequireDigit = false;
                opt.Password.RequireNonLetterOrDigit = false;
                opt.Password.RequireUppercase = false;
                opt.Password.RequireLowercase = false;
                opt.User.AllowedUserNameCharacters = opt.User.AllowedUserNameCharacters + '+';
            })
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders();

            services.AddMvc();

            // Add application services.
            services.AddTransient<IEmailSender, AuthMessageSender>();
            services.AddTransient<ISmsSender, AuthMessageSender>();

            services.AddSingleton(CreateJSEngine);

        }
开发者ID:sitharus,项目名称:MHUI,代码行数:31,代码来源:Startup.cs

示例4: ConfigureServices

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            
            services.AddSignalR(options =>
            {
                options.Hubs.EnableDetailedErrors = true;
                options.Hubs.EnableJavaScriptProxies = true;
  
        
            });
            // Add framework services.
            services.AddApplicationInsightsTelemetry(Configuration);

            services.AddEntityFramework()
                .AddSqlServer()
                .AddDbContext<ApplicationDbContext>(options =>
                    options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));

            services.AddIdentity<ApplicationUser, IdentityRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders();

            services.AddMvc();

            // Add application services.
            services.AddTransient<IEmailSender, AuthMessageSender>();
            services.AddTransient<ISmsSender, AuthMessageSender>();
        }
开发者ID:ysfkel,项目名称:SignalRChatApp,代码行数:29,代码来源:Startup.cs

示例5: ConfigureServices

        public void ConfigureServices(IServiceCollection services)
        {
            // only allow authenticated users
            var defaultPolicy = new AuthorizationPolicyBuilder()
                .RequireAuthenticatedUser()
                .Build();

            services.AddMvc(setup =>
            {
                setup.Filters.Add(new AuthorizeFilter(defaultPolicy));
            });

            services.AddAuthorization(options =>
            {
                // some examples
                options.AddPolicy("SalesOnly", policy =>
                {
                    policy.RequireClaim("department", "sales");
                });
                options.AddPolicy("SalesSenior", policy =>
                {
                    policy.RequireClaim("department", "sales");
                    policy.RequireClaim("status", "senior");
                });
                options.AddPolicy("DevInterns", policy =>
                {
                    policy.RequireClaim("department", "development");
                    policy.RequireClaim("status", "intern");
                });
                options.AddPolicy("Over18", policy =>
                {
                    policy.RequireDelegate((context, requirement) =>
                    {
                        var age = context.User.FindFirst("age")?.Value ?? "0";
                        if (int.Parse(age) >= 18)
                        {
                            context.Succeed(requirement);
                        }
                        else
                        {
                            context.Fail();
                        }
                    });
                });

                // custom policy
                options.AddPolicy("CxO", policy =>
                {
                    policy.RequireJobLevel(JobLevel.CxO);
                });
            });

            // register resource authorization handlers
            services.AddTransient<IAuthorizationHandler, CustomerAuthorizationHandler>();
            services.AddTransient<IAuthorizationHandler, ProductAuthorizationHandler>();

            // register data access services
            services.AddTransient<IPermissionService, PermissionService>();
            services.AddTransient<IOrganizationService, OrganizationService>();
        }
开发者ID:freemsly,项目名称:AspNet5SecuritySamples,代码行数:60,代码来源:Startup.cs

示例6: ConfigureServices

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();

            services.AddCaching();
            services.AddSession();

            services.AddSwaggerGen();

            services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
            services.Configure<MvcOptions>(options =>
            {
                //options.RespectBrowserAcceptHeader = true;
            });
            services.AddEntityFramework()
                .AddInMemoryDatabase()
                .AddDbContext<ApplicationDbContext>(options => { options.UseInMemoryDatabase(); });

            services.AddIdentity<ApplicationUser, IdentityRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddUserStore<ApplicationUserStore<ApplicationUser>>()
                .AddDefaultTokenProviders();

            services.AddTransient<ApplicationUserManager>();
            services.AddTransient<ApplicationSignInManager>();
            services.AddTransient<IUserImageProvider, GravatarProvider>();

            services.AddAuthentication();

            // https://github.com/aspnet/Entropy/blob/dev/samples/Logging.Elm/Startup.cs
            services.AddElm();
        }
开发者ID:CWISoftware,项目名称:accounts,代码行数:32,代码来源:Startup.cs

示例7: ConfigureServices

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Add Cors
            services.AddCors(options =>
            {
                options.AddPolicy("AllowAll", p => p.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().AllowCredentials());
            });

            // Add framework services.
            services.AddMvc();

            // Add options DI.
            services.Configure<WeatherApisConfig>(Configuration.GetSection("WeatherApisConfig"));

            // Add Implementations DI
            services.AddTransient<IWeatherApiFactory, WeatherApiFactory>();
            services.AddTransient<ICache<IList<Forecast>>, MemoryCache<IList<Forecast>>>();

            services.AddTransient<IRestClient<ForecastIoEntities.EndPointResponse>,
                HttpRestApiClient<ForecastIoEntities.EndPointResponse>>();
            services.AddTransient<IRestClient<WundegroundEntities.EndPointResponse>,
                HttpRestApiClient<WundegroundEntities.EndPointResponse>>();
            services.AddTransient<IRestClient<WorldWeatherOnlineEntities.EndPointResponse>,
                HttpRestApiClient<WorldWeatherOnlineEntities.EndPointResponse>>();

            // Add Memory Cache Implementation
            services.AddMemoryCache();
        }
开发者ID:Cr4ck3rs,项目名称:ChathamExam,代码行数:29,代码来源:Startup.cs

示例8: ConfigureServices

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.AddEntityFramework()
                .AddSqlServer()
                .AddDbContext<ApplicationDbContext>(options =>
                    options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));

            services.AddIdentity<ApplicationUser, IdentityRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders();

            // Add MVC services to the services container.
            services.AddLocalization(options => options.ResourcesPath = "Resources");

            
            services
                .AddMvc()
                .AddViewLocalization(options => options.ResourcesPath = "Resources")
                .AddDataAnnotationsLocalization();

            services.AddScoped<LanguageCookieActionFilter>();

            // Add application services.
            services.AddTransient<IEmailSender, AuthMessageSender>();
            services.AddTransient<ISmsSender, AuthMessageSender>();
        }
开发者ID:chemitaxis,项目名称:Localization.StackOverflow,代码行数:28,代码来源:Startup.cs

示例9: ConfigureServices

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.AddEntityFramework()
                .AddSqlite()
                .AddDbContext<ApplicationDbContext>(options =>
                    options.UseSqlite(Configuration["Data:DefaultConnection:ConnectionString"]));

            services.AddIdentity<ApplicationUser, IdentityRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders() ;
                
            services.AddAuthorization(options =>
            {
                options.AddPolicy("ArmDisarm", policy => policy.RequireClaim("http://schemas.microsoft.com/ws/2008/06/identity/claims/role", "ArmDisarm"));
            });
    
            services.AddMvc();

            // Setup the alarm system
            AlarmSystem alarmSystem = AlarmSystemRepository.Get();
            alarmSystem.ZoneChange += SmartThingsRepository.AlarmSystem_ZoneChange;
            alarmSystem.PartitionChange += SmartThingsRepository.AlarmSystem_PartitionChange;
            alarmSystem.StartSession();

            // Add the alarm system as a service available to the controllers
            services.AddInstance<AlarmSystem>(alarmSystem);
            services.AddTransient<SampleDataInitializer>();
            
            // Add application services.
            services.AddTransient<IEmailSender, AuthMessageSender>();
            services.AddTransient<ISmsSender, AuthMessageSender>();
        }
开发者ID:R-OG,项目名称:j64.AlarmServer,代码行数:34,代码来源:Startup.cs

示例10: ConfigureServices

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.AddApplicationInsightsTelemetry(Configuration);

            services.AddEntityFramework()
                .AddSqlServer()
                .AddDbContext<ApplicationDbContext>(options =>
                    options.UseSqlServer(Configuration["Data:TJCMVCOnlyContext:ConnectionString"]));

            services.AddIdentity<ApplicationUser, IdentityRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders();

            services.AddMvc();

            // Add application services.
            services.AddTransient<IEmailSender, AuthMessageSender>();
            services.AddTransient<ISmsSender, AuthMessageSender>();

            services.AddEntityFramework()
                .AddSqlServer()
                .AddDbContext<TJCMVCOnlyContext>(options =>
                    options.UseSqlServer(Configuration["Data:TJCMVCOnlyContext:ConnectionString"]));
        }
开发者ID:tkezyo,项目名称:LearningSpace,代码行数:26,代码来源:Startup.cs

示例11: ConfigureServices

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            var connection = @"Server=dataserver;Database=TechLiquid;Trusted_Connection=True;MultipleActiveResultSets = True";
            services.AddEntityFramework()
                .AddSqlServer()
                .AddDbContext<TechLiquidDbContext>(options => options.UseSqlServer(connection));


            services.AddIdentity<ApplicationUser, IdentityRole>()
                .AddEntityFrameworkStores<TechLiquidDbContext>()
                .AddDefaultTokenProviders();

            // Register the service and implementation for the database context
            //services.AddScoped<ITechLiquidDbContext>(provider => provider.GetService<TechLiquidDbContext>());

            services.AddMvc();
            services.AddCaching(); // Adds a default in-memory implementation of IDistributedCache

            services.AddSession(o =>
            {
                o.IdleTimeout = TimeSpan.FromSeconds(10);
            });

            // Add application services.
            services.AddTransient<IEmailSender, AuthMessageSender>();
            services.AddTransient<ISmsSender, AuthMessageSender>();
        }
开发者ID:eatonjd,项目名称:TLAuctionv5,代码行数:29,代码来源:Startup.cs

示例12: ConfigureServices

    // This method gets called by the runtime.
    public void ConfigureServices(IServiceCollection services)
    {
      // Add EF services to the services container.
      services.AddEntityFramework()
          .AddSqlServer()
          .AddDbContext<MyCountriesContext>(options =>
              options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));

      // Add Identity services to the services container.
      services.AddIdentity<ApplicationUser, IdentityRole>()
          .AddEntityFrameworkStores<MyCountriesContext>()
          .AddDefaultTokenProviders();

      // Add MVC services to the services container.
      services.AddMvc()
        .AddJsonOptions(opts =>
        {
          opts.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
        });

      // Add other services
      services.AddTransient<SampleDataInitializer>();
      services.AddScoped<IMyCountriesRepository, MyCountriesRepository>();

      services.AddTransient<IEmailSender, AuthMessageSender>();
      services.AddTransient<ISmsSender, AuthMessageSender>();
    }
开发者ID:princeppy,项目名称:MyCountries,代码行数:28,代码来源:Startup.cs

示例13: ConfigureServices

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            services.AddIdentity<ApplicationUser, IdentityRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders();

            services.AddMvc();

            // Add application services.
            services.AddTransient<IEmailSender, AuthMessageSender>();
            services.AddTransient<ISmsSender, AuthMessageSender>();

            // Get the configuration info
            j64HarmonyGateway j64Config = j64HarmonyGatewayRepository.Read();
            services.AddSingleton<j64HarmonyGateway>(j64Config);

            // Get an auth token from the harmony "cloud"
            Hub myHub = new Hub();
            bool connected = myHub.StartNewConnection(j64Config.Email, j64Config.Password, j64Config.HubAddress, j64Config.HubPort);

            // Add the hub as a service available to all of the controllers
            services.AddSingleton<Hub>(myHub);
        }
开发者ID:joejarvis64,项目名称:j64.Harmony,代码行数:28,代码来源:Startup.cs

示例14: ConfigureServices

		// This method gets called by the runtime. Use this method to add services to the container.
		public void ConfigureServices(IServiceCollection services)
		{
			// Registers MongoDB conventions for ignoring default and blank fields
			// NOTE: if you have registered default conventions elsewhere, probably don't need to do this
			RegisterClassMap<ApplicationUser, IdentityRole, string>.Init();

			// Add Mongo Identity services to the services container.
			services.AddIdentity<ApplicationUser, IdentityRole>()
				.AddMongoDBIdentityStores<ApplicationDbContext, ApplicationUser, IdentityRole, string>(options =>
				{
					options.ConnectionString = Configuration["Data:DefaultConnection:ConnectionString"];        // No default, must be configured if using (eg "mongodb://localhost:27017")
					// options.Client = [IMongoClient];									// Defaults to: uses either Client attached to [Database] (if supplied), otherwise it creates a new client using [ConnectionString]
					// options.DatabaseName = [string];									// Defaults to: "AspNetIdentity"
					// options.Database = [IMongoDatabase];								// Defaults to: Creating Database using [DatabaseName] and [Client]

					// options.UserCollectionName = [string];							// Defaults to: "AspNetUsers"
					// options.RoleCollectionName = [string];							// Defaults to: "AspNetRoles"
					// options.UserCollection = [IMongoCollection<TUser>];				// Defaults to: Creating user collection in [Database] using [UserCollectionName] and [CollectionSettings]
					// options.RoleCollection = [IMongoCollection<TRole>];				// Defaults to: Creating user collection in [Database] using [RoleCollectionName] and [CollectionSettings]
					// options.CollectionSettings = [MongoCollectionSettings];			// Defaults to: { WriteConcern = WriteConcern.WMajority } => Used when creating default [UserCollection] and [RoleCollection]

					// options.EnsureCollectionIndexes = [bool];						// Defaults to: false => Used to ensure the User and Role collections have been created in MongoDB and indexes assigned. Only runs on first calls to user and role collections.
					// options.CreateCollectionOptions = [CreateCollectionOptions];		// Defaults to: { AutoIndexId = true } => Used when [EnsureCollectionIndexes] is true and User or Role collections need to be created.
					// options.CreateIndexOptions = [CreateIndexOptions];				// Defaults to: { Background = true, Sparse = true } => Used when [EnsureCollectionIndexes] is true and any indexes need to be created.
				})
				.AddDefaultTokenProviders();


			// Add MVC services to the services container.
			services.AddMvc();

			// Add application services.
			services.AddTransient<IEmailSender, AuthMessageSender>();
			services.AddTransient<ISmsSender, AuthMessageSender>();
		}
开发者ID:freemsly,项目名称:SaanSoft.AspNet.Identity3.MongoDB,代码行数:36,代码来源:Startup.cs

示例15: ConfigureServices

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {  
            // Add Scoped = Resolve dependency injection
            services.AddScoped<LibraryDbContext, LibraryDbContext>();
            services.AddScoped<IMediatheekService, MediatheekService>();
            services.AddScoped<IRandomUserMeService, RandomUserMeService>();
            services.AddScoped<IRandomTextService, RandomTextService>();
            
            // Add Entity Framework services to the services container.
            services.AddEntityFramework()
                .AddSqlite()
                .AddDbContext<LibraryDbContext>(options =>
                    options.UseSqlite(Configuration["Data:DefaultConnection:ConnectionString"]));
                    
            // Add Identity services to the services container.
            services.AddIdentity<ApplicationUser, ApplicationRole>()
                .AddEntityFrameworkStores<LibraryDbContext>()
                .AddDefaultTokenProviders();

            services.AddMvc();
            
            // Sessions and caching
            services.AddCaching();
            services.AddSession();
            
            // Ultimate reporting tool
            //services.AddGlimpse();
            
            // Add application services.
            services.AddTransient<IEmailSender, AuthMessageSender>();
            services.AddTransient<ISmsSender, AuthMessageSender>();
        }
开发者ID:gdm-201516-prodev3,项目名称:wdad3,代码行数:33,代码来源:Startup.cs


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