public void ConfigureServices (Microsoft.Extensions.DependencyInjection.IServiceCollection services) { services.AddControllers(); // Note: services.AddTransient <Microsoft.AspNetCore.Http.IHttpContextAccessor, Microsoft.AspNetCore.Http.HttpContextAccessor>(); // Note: //services.AddTransient<Dtx.Logging.ILogger, Dtx.Logging.NLogAdapter>(); // Compile Error! //services.AddTransient<Dtx.Logging.ILogger<>, Dtx.Logging.NLogAdapter<>>(); // Compile Error! //services.AddSingleton // (serviceType: typeof(Dtx.Logging.ILogger<>), // implementationType: typeof(Dtx.Logging.NLogAdapter<>)); // Runtime Error! services.AddTransient (serviceType: typeof(Dtx.Logging.ILogger <>), implementationType: typeof(Dtx.Logging.NLogAdapter <>)); //services.AddTransient // (serviceType: typeof(Dtx.Logging.ILogger<>), // implementationType: typeof(Dtx.Logging.Log4NetAdapter<>)); }
// method runs after Constructor is done in first run // parameter of type IServiceCollection is passed into this method // doesn't return anything public void ConfigureServices(Microsoft.Extensions.DependencyInjection.IServiceCollection services) { services.AddDbContext <Models.ApplicationDbContext>(options => options.UseSqlServer(Configuration["Data:BilliardStoreProducts:ConnectionString"])); services.AddDbContext <Models.AppIdentityDbContext>(options => options.UseSqlServer(Configuration["Data:BilliardStoreIdentity:ConnectionString"])); services.AddIdentity <Microsoft.AspNetCore.Identity.IdentityUser, Microsoft.AspNetCore.Identity.IdentityRole>() .AddEntityFrameworkStores <BilliardStore.Models.AppIdentityDbContext>() .AddDefaultTokenProviders(); services.AddTransient <Models.IProductRepository, Models.EFProductRepository>(); services.AddScoped <Models.Cart>(sp => Models.SessionCart.GetCart(sp)); services.AddSingleton <Microsoft.AspNetCore.Http.IHttpContextAccessor, Microsoft.AspNetCore.Http.HttpContextAccessor>(); services.AddTransient <Models.IOrderRepository, Models.EFOrderRepository>(); services.AddTransient <SendGrid.ISendGridClient>((s) => { return(new SendGrid.SendGridClient(Configuration.GetValue <string>("SendGrid:Key"))); }); services.AddTransient <Braintree.IBraintreeGateway>((s) => { return(new Braintree.BraintreeGateway( Configuration.GetValue <string>("Braintree:Environment"), Configuration.GetValue <string>("Braintree:MerchantId"), Configuration.GetValue <string>("Braintree:PublicKey"), Configuration.GetValue <string>("Braintree:PrivateKey"))); }); services.AddTransient <SmartyStreets.IClient <SmartyStreets.USStreetApi.Lookup> >((s) => { return(new SmartyStreets.ClientBuilder( Configuration.GetValue <string>("SmartyStreets:AuthId"), Configuration.GetValue <string>("SmartyStreets:AuthToken") ).BuildUsStreetApiClient()); }); services.AddMvc(); services.AddMemoryCache(); services.AddSession(); } // makes EFProductRepository take the place of IProductRepository in the code
public static void RegisterServices (Microsoft.Extensions.DependencyInjection.IServiceCollection services) { // Domain Bus //services.AddTransient // <Domain.Core.Bus.IEventBus, Bus.RabbitMQEventBusSimple>(); // Updated! services.AddSingleton <Domain.Core.Bus.IEventBus, Bus.RabbitMQEventBus>(sp => { Microsoft.Extensions.DependencyInjection.IServiceScopeFactory serviceScopeFactory = sp.GetRequiredService <Microsoft.Extensions.DependencyInjection.IServiceScopeFactory>(); return(new Bus.RabbitMQEventBus(sp.GetService <MediatR.IMediator>(), serviceScopeFactory)); }); // Application Service(s) services.AddTransient <Banking.Application.Interfaces.IAccountService, Banking.Application.Services.AccountService>(); // Data services.AddTransient <Banking.Data.Context.DatabaseContext>(); services.AddTransient <Banking.Domain.Interfaces.IAccountRepository, Banking.Data.Repositories.AccountRepository>(); }
public static ISqlRepositoryBuilder AddSqlRepository(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, Action <SqlRepositoryOptions> configure) { services.Configure(configure); services.AddSingleton(typeof(OfaSchlupfer.Model.IExternalRepositoryModelType), typeof(SqlRepositoryModelType)); services.AddTransient(typeof(SqlRepositoryModel), typeof(SqlRepositoryImplementation)); return(new SqlRepositoryBuilder(services)); }
/// <summary> /// 添加Unit Of Work /// </summary> /// <param name="services"></param> public static void AddUow(this Microsoft.Extensions.DependencyInjection.IServiceCollection services) { #region UOW services.TryAddSingleton <IUnitOfWorkDefaultOptions, UnitOfWorkDefaultOptions>(); services.AddTransient <ICurrentUnitOfWorkProvider, AsyncLocalCurrentUnitOfWorkProvider>(); services.AddTransient <UnitOfWorkBase, EfCoreUnitOfWork>(); services.AddTransient <IUnitOfWork, EfCoreUnitOfWork>(); services.AddTransient <IUnitOfWorkManager, UnitOfWorkManager>(); #endregion #region EntityFrameworkCore services.TryAddSingleton <IDbContextTypeMatcher, DbContextTypeMatcher>(); services.AddTransient <IEfCoreTransactionStrategy, DbContextEfCoreTransactionStrategy>(); services.AddTransient <IDbContextResolver, DefaultDbContextResolver>(); services.AddTransient(typeof(IDbContextProvider <>), typeof(UnitOfWorkDbContextProvider <>)); #endregion }
public static void AddMicroORM(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, string connectionString, ILog logger = null) { services. AddTransient <ILog>(p => logger ?? new ConsoleLogWriter()). AddSingleton <SQLDatabaseOptions>((p) => new SQLDatabaseOptions() { ConnectionString = connectionString }). AddTransient <Factory>(); }
public void Configure(Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection) { serviceCollection.AddTransient <ContentController>(); serviceCollection.AddScoped <IItemFactory, ItemFactory>(); serviceCollection.AddScoped <IItemSearchFactory, ItemSearchFactory>(); serviceCollection.AddScoped <IContextProvider, ContextProvider>(); serviceCollection.AddSingleton <IModelAssemblyProvider, ModelAssemblyProvider>(); serviceCollection.AddSingleton <ISpawnProvider, SpawnProvider>(); serviceCollection.AddSingleton <ITemplateMapProvider, TemplateMapProvider>(); serviceCollection.AddScoped <ISearchResultsAdapter, SearchResultsAdapter>(); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(Microsoft.Extensions.DependencyInjection.IServiceCollection services) { services.AddControllers(); // 添加MVC服务 services.AddControllersWithViews(); // 注册服务 #region 依赖注入 #region 注册方法注解 // Singleton:单例服务,从当前服务容器中获取这个类型的实例永远是同一个实例; // Scoped:每个作用域生成周期内创建一个实例; // Transient:每一次请求服务都创建一个新实例; #endregion services.AddSingleton <ILogService, SysLogService>(); //services.AddScoped<ILogService, SysLogService>(); //services.AddTransient<ILogService, SysLogService>(); services.AddTransient <IOperationTransient, Operation>(); services.AddScoped <IOperationScoped, Operation>(); services.AddSingleton <IOperationSingleton, Operation>(); services.AddSingleton <IOperationSingletonInstance>(new Operation(Guid.Empty)); services.AddTransient <OperationService, OperationService>(); var serviceCollection = new ServiceCollection() .AddTransient <ILogService, SysLogService>() .AddSingleton <ILogService, SysLogService>() .AddScoped <ILogService, SysLogService>(); #endregion }
public void ConfigureRepositoryServices(Microsoft.Extensions.DependencyInjection.IServiceCollection services) { //services.AddDbContext<SupplyOfProductsContext>(options => // options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection") //)); // Add Repositories. services.AddSingleton <IGenericContext, SupplyOfProductsContext>(); services.AddTransient <IProductRepository, ProductRepository>(); services.AddTransient <IProductSupplyRepository, ProductSupplyRepository>(); services.AddTransient <IWorkerRepository, WorkerRepository>(); services.AddTransient <IWorkerInWorkPlaceRepository, WorkerInWorkPlaceRepository>(); services.AddTransient <IWorkPlaceRepository, WorkPlaceRepository>(); services.AddTransient <IProductStockRepository, ProductStockRepository>(); services.AddTransient <ISupplyScheduledRepository, SupplyScheduledRepository>(); services.AddTransient <IConfigSupplyRepository, ConfigSupplyRepository>(); }
public static void ConfigureServices(IConfigurationRoot configurationRoot, Microsoft.Extensions.DependencyInjection.IServiceCollection services, ILogger logger) { var billingDbSection = configurationRoot.GetSection("BillingDb"); if (billingDbSection == null) { logger.AddCustomEvent(LogLevel.ConfigurationError, "BillingManager_Startup", "Missing Section BillingDb"); throw new InvalidConfigurationException(new IoT.Logging.Error() { ErrorCode = "CFG9991", Message = "Missing Section BillingDb" }); } var connectionSettings = new ConnectionSettings() { Uri = billingDbSection["ServerURL"], ResourceName = billingDbSection["InitialCatalog"], UserName = billingDbSection["UserName"], Password = billingDbSection["Password"], }; if (string.IsNullOrEmpty(connectionSettings.Uri)) { logger.AddCustomEvent(LogLevel.ConfigurationError, "BillingManager_Startup", "Missing BillingDb__ServerURL"); throw new InvalidConfigurationException(new IoT.Logging.Error() { ErrorCode = "CFG9999", Message = "Missing BillingDb__ServerURL" }); } if (string.IsNullOrEmpty(connectionSettings.ResourceName)) { logger.AddCustomEvent(LogLevel.ConfigurationError, "BillingManager_Startup", "Missing BillingDb__InitialCatalog"); throw new InvalidConfigurationException(new IoT.Logging.Error() { ErrorCode = "CFG9999", Message = "Missing BillingDb__InitialCatalog" }); } if (string.IsNullOrEmpty(connectionSettings.UserName)) { logger.AddCustomEvent(LogLevel.ConfigurationError, "BillingManager_Startup", "Missing BillingDb__UserName"); throw new InvalidConfigurationException(new IoT.Logging.Error() { ErrorCode = "CFG9999", Message = "Missing BillingDb__UserName" }); } if (string.IsNullOrEmpty(connectionSettings.Password)) { logger.AddCustomEvent(LogLevel.ConfigurationError, "BillingManager_Startup", "Missing BillingDb__Password"); throw new InvalidConfigurationException(new IoT.Logging.Error() { ErrorCode = "CFG9999", Message = "Missing BillingDb__Password" }); } var connectionString = $"Server=tcp:{connectionSettings.Uri},1433;Initial Catalog={connectionSettings.ResourceName};Persist Security Info=False;User ID={connectionSettings.UserName};Password={connectionSettings.Password};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"; services.AddTransient <ISubscriptionRepo, SubscriptionRepo>(); services.AddTransient <IRDBMSManager, RDBMSManager>(); services.AddDbContext <UserAdminDataContext>(options => options.UseSqlServer(connectionString, moreOptions => moreOptions.EnableRetryOnFailure())); }
public void ConfigureServices (Microsoft.Extensions.DependencyInjection.IServiceCollection services) { // Cross-Origin Resource Sharing (CORS) services.AddCors(options => { options.AddPolicy(AdminCorsPolicy, builder => { builder .WithOrigins("http://localhost:1220") .AllowAnyHeader() .AllowAnyMethod() //.AllowCredentials() ; }); options.AddPolicy(OthersCorsPolicy, builder => { builder .AllowAnyOrigin() .AllowAnyHeader() .AllowAnyMethod() //.AllowCredentials() ; }); }); //services.AddControllers(); services.AddControllers().AddJsonOptions(options => { options.JsonSerializerOptions.MaxDepth = 5; options.JsonSerializerOptions.PropertyNamingPolicy = null; }); //services.AddDbContext<Data.DatabaseContext>(options => //{ // options.UseSqlServer // (connectionString: "Password=1234512345;Persist Security Info=True;User ID=SA;Initial Catalog=DtxSecurity;Data Source=."); //}); //services.AddDbContext<Data.DatabaseContext>(options => //{ // options.UseSqlServer // (connectionString: Configuration.GetSection(key: "ConnectionStrings").GetSection(key: "MyConnectionString"); //}); //services.AddTransient<Data.IUnitOfWork, Data.UnitOfWork>(); services.AddTransient <Data.IUnitOfWork, Data.UnitOfWork>(sp => { Data.Tools.Options options = new Data.Tools.Options { InMemoryDatabase = false, ConnectionString = Configuration.GetSection(key: "ConnectionStrings").GetSection(key: "MyConnectionString").Value, }; return(new Data.UnitOfWork(options: options)); }); }
public void AddTransient(Type serviceType, Func <IServiceProvider, object> implementationFactory) { _serviceCollection.AddTransient(serviceType, implementationFactory); }
private void ConfigureMatterPackages(Microsoft.Extensions.DependencyInjection.IServiceCollection services) { services.AddTransient <IHttpContextAccessor, HttpContextAccessor>(); }
public void ConfigureServices (Microsoft.Extensions.DependencyInjection.IServiceCollection services) { // Cross-Origin Resource Sharing (CORS) services.AddCors(options => { options.AddPolicy(ADMIN_CORS_POLICY, builder => { builder .WithOrigins("http://localhost:5001") .AllowAnyHeader() .AllowAnyMethod() //.AllowCredentials() ; }); options.AddPolicy(OTHERS_CORS_POLICY, builder => { builder .AllowAnyOrigin() .AllowAnyHeader() .AllowAnyMethod() //.AllowCredentials() ; }); }); //services.AddControllers(); services.AddControllers().AddJsonOptions(options => { options.JsonSerializerOptions.MaxDepth = 5; options.JsonSerializerOptions.PropertyNamingPolicy = null; }); //services.AddDbContext<Data.DatabaseContext>(options => //{ // options.UseSqlServer // (connectionString: "Password=1234512345;Persist Security Info=True;User ID=SA;Initial Catalog=DtxSecurity;Data Source=."); //}); //services.AddDbContext<Data.DatabaseContext>(options => //{ // options.UseSqlServer // (connectionString: Configuration.GetSection(key: "ConnectionStrings").GetSection(key: "MyConnectionString"); //}); //services.AddTransient<Data.IUnitOfWork, Data.UnitOfWork>(); services.AddTransient <Data.IUnitOfWork, Data.UnitOfWork>(sp => { Data.Tools.Options options = new Data.Tools.Options { Provider = (Data.Tools.Enums.Provider) System.Convert.ToInt32(Configuration.GetSection(key: "databaseProvider").Value), //using Microsoft.EntityFrameworkCore; //ConnectionString = // Configuration.GetConnectionString().GetSection(key: "MyConnectionString").Value, ConnectionString = Configuration.GetSection(key: "ConnectionStrings").GetSection(key: "MyConnectionString").Value, }; return(new Data.UnitOfWork(options: options)); }); //services.AddTransient<Dtx.ILogger, Dtx.Logger>(); }
public static void ConfigureServices (Microsoft.Extensions.Configuration.IConfiguration configuration, Microsoft.Extensions.DependencyInjection.IServiceCollection services) { // ************************************************** services.AddTransient <Microsoft.AspNetCore.Http.IHttpContextAccessor, Microsoft.AspNetCore.Http.HttpContextAccessor>(); services.AddTransient (serviceType: typeof(Dtx.Logging.ILogger <>), implementationType: typeof(Dtx.Logging.NLogAdapter <>)); // ************************************************** // ************************************************** // AddMediatR -> Extension Method -> using MediatR; // GetTypeInfo -> Extension Method -> using System.Reflection; services.AddMediatR (typeof(Application.Logs.MappingProfile).GetTypeInfo().Assembly); // AddValidatorsFromAssembly -> Extension Method -> using FluentValidation; services.AddValidatorsFromAssembly (assembly: typeof(Application.Logs.Commands.CreateLogCommandValidator).Assembly); services.AddTransient (typeof(MediatR.IPipelineBehavior <,>), typeof(Dtx.Mediator.ValidationBehavior <,>)); // ************************************************** // ************************************************** // using Microsoft.Extensions.DependencyInjection; services.AddAutoMapper (profileAssemblyMarkerTypes: typeof(Application.Logs.MappingProfile)); // ************************************************** // ************************************************** services.AddTransient <Persistence.IUnitOfWork, Persistence.UnitOfWork>(current => { string databaseConnectionString = configuration .GetSection(key: "ConnectionStrings") .GetSection(key: "CommandsConnectionString") .Value; string databaseProviderString = configuration .GetSection(key: "CommandsDatabaseProvider") .Value; Dtx.Persistence.Enums.Provider databaseProvider = (Dtx.Persistence.Enums.Provider) System.Convert.ToInt32(databaseProviderString); Dtx.Persistence.Options options = new Dtx.Persistence.Options { Provider = databaseProvider, ConnectionString = databaseConnectionString, }; return(new Persistence.UnitOfWork(options: options)); }); // ************************************************** // ************************************************** services.AddTransient <Persistence.IQueryUnitOfWork, Persistence.QueryUnitOfWork>(current => { string databaseConnectionString = configuration .GetSection(key: "ConnectionStrings") .GetSection(key: "QueriesConnectionString") .Value; string databaseProviderString = configuration .GetSection(key: "QueriesDatabaseProvider") .Value; Dtx.Persistence.Enums.Provider databaseProvider = (Dtx.Persistence.Enums.Provider) System.Convert.ToInt32(databaseProviderString); Dtx.Persistence.Options options = new Dtx.Persistence.Options { Provider = databaseProvider, ConnectionString = databaseConnectionString, }; return(new Persistence.QueryUnitOfWork(options: options)); }); // ************************************************** }
public void Configure(Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection) { serviceCollection.AddTransient <UnusedCouponsController>(); serviceCollection.AddSingleton <CouponsServiceProvider>(); }