示例#1
0
 // 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