本文整理汇总了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>();
}
示例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>();
}
示例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
}
示例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();
}
示例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>();
}
示例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
}
示例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
}
示例8: ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddScoped<ICodeFinderFactory, CodeFinderFactory>();
services.AddScoped<IFileCodeProcessor, FileCodeProcessor>();
}
示例9: RegisterRepositories
private static void RegisterRepositories(IServiceCollection services)
{
services.AddScoped<ITeamRepository, TeamRepository>();
services.AddScoped<IPlayerTypeRepository, PlayerTypeRepository>();
services.AddScoped<ICountryRepository, CountryRepository>();
services.AddScoped<IPlayerRepository, PlayerRepository>();
}
示例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>());
}
示例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
}
示例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>();
}
示例13: ConfigureServices
public override void ConfigureServices(IServiceCollection services)
{
services.AddScoped<SetupEventHandler>();
services.AddScoped<ISetupEventHandler>(sp => sp.GetRequiredService<SetupEventHandler>());
services.AddRecipeExecutionStep<SettingsStep>();
}
示例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>();
}
示例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();
}
}
}