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


C# IServiceCollection.AddScoped方法代码示例

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


在下文中一共展示了IServiceCollection.AddScoped方法的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 Entity Framework services to the services container.
            services.AddEntityFramework()
                .AddSqlServer()
                .AddDbContext<GztNewsEntities>();
                  
            // Add Identity services to the services container.
            services.AddIdentity<ApplicationUser, ApplicationRole>()
                .AddEntityFrameworkStores<GztNewsEntities>()
                .AddDefaultTokenProviders();

            // Add MVC services to the services container.
            services.AddMvc();
            // Register application services.
            services.AddTransient<IEmailSender, AuthMessageSender>();
            services.AddTransient<ISmsSender, AuthMessageSender>();            
            services.AddTransient<ITodoRepository, TodoRepository>();

            services.AddScoped<ITestService, TestService>();
            services.AddScoped<ITestRepository, TestRepository>();
            services.AddScoped<IUnitOfWork, UnitOfWork>();
            services.AddScoped<IDbFactory, DbFactory>();


        }
开发者ID:marcosph,项目名称:DependencyInjectionASP.NET5,代码行数:27,代码来源:Startup.cs

示例2: ConfigureServices

        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
	        services.AddMvc().AddJsonOptions(opt => 
			opt.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver());
	        services.AddIdentity<WorldUser, IdentityRole>(
		        config =>
		        {
			        config.User.RequireUniqueEmail = true;
			        config.Password.RequiredLength = 5;
			        config.Cookies.ApplicationCookie.LoginPath = "/Auth/Login";
					config.Cookies.ApplicationCookie.Events = new CookieAuthenticationEvents()
					{
						OnRedirectToLogin = ctx =>
						{
							if (ctx.Request.Path.StartsWithSegments("/api") &&
							    ctx.Response.StatusCode == (int) HttpStatusCode.OK)
							{
								ctx.Response.StatusCode = (int) HttpStatusCode.Unauthorized;
							}else
							ctx.Response.Redirect(ctx.RedirectUri);
							return Task.FromResult(0);
						}
					};
		        })
		        .AddEntityFrameworkStores<WorldContext>();
			
			services.AddLogging();
	        services.AddEntityFramework().AddSqlServer().AddDbContext<WorldContext>();
	        services.AddScoped<IMailService, DebugMailService>();
	        services.AddTransient<WorldContextSeedData>();
	        services.AddScoped<IWorldRepository, WorldRepository>();
	        services.AddScoped<CoorService>();
        }
开发者ID:vickism,项目名称:ASP5-MVC6-Angular1-EF7-sample,代码行数:35,代码来源:Startup.cs

示例3: ConfigureServices

        // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc()
                .AddJsonOptions(opt =>
                {
                    opt.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); // makes api results use camel case
                });

            services.AddLogging();

            services.AddScoped<CoordService>();

            services.AddEntityFramework()
                .AddSqlServer()
                .AddDbContext<WorldContext>();

            services.AddTransient<WorldContextSeedData>();

            services.AddScoped<IWorldRepository, WorldRepository>();

#if DEBUG
            services.AddScoped<IMailService, DebugMailService>();
#else
            services.AddScoped<IMailService, MailService>(); // or something like that 
#endif
        }
开发者ID:FishFuzz99,项目名称:.NET-test,代码行数:27,代码来源: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)
        {
            // Add framework services.
            services.AddEntityFramework()
                .AddSqlServer()
                .AddDbContext<GcseChineseDbContext>(options =>
                    options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));

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

            services.AddMvc().AddJsonOptions(
                opt=> opt.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver()
                );

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

            // Add interfaces
            services.AddTransient<GcseSeededData>();
            services.AddScoped<IAssessmentRepository, AssessmentRepository>();
            services.AddScoped<IExampaperRepository, ExampaperRepository>();
            services.AddScoped<IThemeRepository, ThemeRepository>();
            services.AddScoped<ITopicRepository, TopicRepository>();
            services.AddLogging();
        }
开发者ID:misssoft,项目名称:GcseChineseApi,代码行数:29,代码来源:Startup.cs

示例5: ConfigureServices

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

            var connectionString = Configuration["db:bstu-mssql"];

            services.AddEntityFramework()
                .AddSqlServer()
                .AddDbContext<DataContext>(options => options.UseSqlServer(connectionString));

            services.AddScoped<IDataContext>(provider => provider.GetService<DataContext>());
            services.AddScoped<INewsRepository, NewsRepository>();
            services.AddScoped<IFeedNewsService, FeedNewsService>();
            services.AddScoped<IUpcomingNewsService, UpcomingNewsService>();

            services.Configure<FileSystem>(options =>
            {
                options.ThumbsPath = Configuration["fs:thumbs-path"];
                options.ThumbsFilename = Configuration["fs:thumbs-filename"];
            });

            if (Configuration["env"] == "dev")
            {
                //services.AddSingleton<IImageRepository, HttpImageRepository>();
                services.AddSingleton<IImageRepository, DirectImageRepository>();
            }
            else
            {
                services.AddSingleton<IImageRepository, FileImageRepository>();
            }

            services.AddSingleton<IImageCache, ImageCache>();
            services.AddSingleton<IImageService, ImageService>();
        }
开发者ID:accetone,项目名称:bstutimeline,代码行数:34,代码来源:Startup.cs

示例6: ConfigureServices

        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc()
                .AddJsonOptions(opt =>
                {
                    opt.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
                });

            services.AddIdentity<WorldUser, IdentityRole>(config =>
            {
                config.User.RequireUniqueEmail = true;
                config.Password.RequiredLength = 8;
                config.Cookies.ApplicationCookie.LoginPath = "/Auth/Login";
            })
            .AddEntityFrameworkStores<WorldContext>();

            services.AddLogging();

            services.AddEntityFramework()
                .AddSqlServer()
                .AddDbContext<WorldContext>();

            services.AddScoped<CoordService>();
            services.AddTransient<WorldContextSeedData>();
            services.AddScoped<IWorldRepository, WorldRepository>();

#if DEBUG
            services.AddScoped<IMailService, DebugMailService>();
#else
            services.AddScoped<IMailService, RealMailService>();
#endif
        }
开发者ID:andriybuday,项目名称:aspnet5,代码行数:34,代码来源:Startup.cs

示例7: ConfigureServices

        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();

            services.AddLogging();

            // Note: why do we need to configure DBContext lke this?  Can't we add scoped like below?
            services.AddEntityFramework()
                .AddSqlServer()
                .AddDbContext<WorldContext>();

            // Note: AddSingleton will share the object for the lifetime of the webserver
            //       AddScoped will share the object for the lifetime of the request
            //       AddTransient will give you a new object everytime
            //       AddInstance allows you create your own object and pass it in directly

            services.AddTransient<WorldContextSeedData>();
            services.AddScoped<IWorldRepository, WorldRepository>();

#if DEBUG
            services.AddScoped<IMailService, DebugMailService>();
#else
            // Add real mail service
#endif
        }
开发者ID:danielmackay,项目名称:ASP-NET-5,代码行数:27,代码来源:Startup.cs

示例8: ConfigureServices

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

            services.AddScoped<ICodeFinderFactory, CodeFinderFactory>();
            services.AddScoped<IFileCodeProcessor, FileCodeProcessor>();
        }
开发者ID:jagasafari,项目名称:CodeFinder,代码行数:7,代码来源:Startup.cs

示例9: RegisterRepositories

 private static void RegisterRepositories(IServiceCollection services)
 {
     services.AddScoped<ITeamRepository, TeamRepository>();
     services.AddScoped<IPlayerTypeRepository, PlayerTypeRepository>();
     services.AddScoped<ICountryRepository, CountryRepository>();
     services.AddScoped<IPlayerRepository, PlayerRepository>();
 }
开发者ID:yvaravko,项目名称:JuveV,代码行数:7,代码来源:Startup.cs

示例10: AddCoreServices

 public void AddCoreServices(IServiceCollection services)
 {
     services.AddScoped<IShellStateUpdater, ShellStateUpdater>();
     services.AddScoped<IShellStateManager, ShellStateManager>();
     services.AddScoped<ShellStateCoordinator>();
     services.AddScoped<IShellDescriptorManagerEventHandler>(sp => sp.GetRequiredService<ShellStateCoordinator>());
 }
开发者ID:jchenga,项目名称:Orchard2,代码行数:7,代码来源:ShellContainerFactory.cs

示例11: 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>();
#if DEBUG
      services.AddScoped<IEmailer, ConsoleEmailer>();
#else
  services.AddScoped<IEmailer, Emailer>();
#endif
    }
开发者ID:huytrongnguyen,项目名称:MyCountries,代码行数:30,代码来源:Startup.cs

示例12: ConfigureServices

        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
      
            services.AddMvc()
                .AddJsonOptions(opt =>
                {
                    opt.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
                });

            services.AddScoped<ICourseService, CourseService>();

            services.AddScoped<IUniversityRepository, UniversityRepository>();

            services.AddIdentity<UniversityUser, IdentityRole>(config =>
            {
                config.User.RequireUniqueEmail = true;
                config.Password.RequiredLength = 8;
                config.Cookies.ApplicationCookie.LoginPath = "/Auth/Login";
            })
            .AddEntityFrameworkStores<UniversityContext>();

            services.AddEntityFramework()
                .AddSqlServer()
                 .AddDbContext<UniversityContext>();

        }
开发者ID:DhivoGnani,项目名称:DhivoApp,代码行数:28,代码来源:Startup.cs

示例13: ConfigureServices

        public override void ConfigureServices(IServiceCollection services)
        {
            services.AddScoped<SetupEventHandler>();
            services.AddScoped<ISetupEventHandler>(sp => sp.GetRequiredService<SetupEventHandler>());

            services.AddRecipeExecutionStep<SettingsStep>();
        }
开发者ID:vairam-svs,项目名称:Orchard2,代码行数:7,代码来源: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)
        {
            // Add Entity Framework services to the services container.
            services.AddEntityFramework()
                .AddInMemoryDatabase()
                .AddDbContext<ApplicationDbContext>(options => options.UseInMemoryDatabase());

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

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

            // Uncomment the following line to add Web API services which makes it easier to port Web API 2 controllers.
            // You will also need to add the Microsoft.AspNet.Mvc.WebApiCompatShim package to the 'dependencies' section of project.json.
            // services.AddWebApiConventions();

            // Register application services.
            services.AddTransient<IEmailSender, AuthMessageSender>();
            services.AddTransient<ISmsSender, AuthMessageSender>();
            services.AddScoped<ICharacterRepository, CharacterRepository>();

            // Show different lifetime options
            services.AddTransient<IOperationTransient, Operation>();
            services.AddScoped<IOperationScoped, Operation>();
            services.AddSingleton<IOperationSingleton, Operation>();
            services.AddInstance<IOperationInstance>(new Operation(Guid.Empty));
            services.AddTransient<OperationService, OperationService>();
        }
开发者ID:dvincent,项目名称:Docs,代码行数:32,代码来源:Startup.cs

示例15: ConfigureServices

        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<AppSettings>(Configuration);

            // We re-register the Scenarios as an instance singleton here to avoid it being created again due to the
            // registration done in Program.Main
            services.AddSingleton(Scenarios);

            // Common DB services
            services.AddSingleton<IRandom, DefaultRandom>();
            services.AddSingleton<ApplicationDbSeeder>();
            services.AddEntityFrameworkSqlServer()
                .AddDbContext<ApplicationDbContext>();

            if (Scenarios.Any("Raw") || Scenarios.Any("Dapper"))
            {
                // TODO: Add support for plugging in different DbProviderFactory implementations via configuration
                services.AddSingleton<DbProviderFactory>(SqlClientFactory.Instance);
            }

            if (Scenarios.Any("Ef"))
            {
                services.AddScoped<EfDb>();
            }

            if (Scenarios.Any("Raw"))
            {
                services.AddScoped<RawDb>();
            }

            if (Scenarios.Any("Dapper"))
            {
                services.AddScoped<DapperDb>();
            }

            if (Scenarios.Any("Fortunes"))
            {
                services.AddWebEncoders();
            }

            if (Scenarios.Any("Mvc"))
            {
                var mvcBuilder = services
                    .AddMvcCore()
                    //.AddApplicationPart(typeof(Startup).GetTypeInfo().Assembly)
                    .AddControllersAsServices();

                if (Scenarios.MvcJson)
                {
                    mvcBuilder.AddJsonFormatters();
                }

                if (Scenarios.MvcViews || Scenarios.Any("MvcDbFortunes"))
                {
                    mvcBuilder
                        .AddViews()
                        .AddRazorViewEngine();
                }
            }
        }
开发者ID:sumeetchhetri,项目名称:FrameworkBenchmarks,代码行数:60,代码来源:Startup.cs


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