/// <summary> /// This method gets called by the runtime. Use this method to add services to the container. /// </summary> /// <param name="services"></param> /// <returns></returns> public IServiceProvider ConfigureServices(IServiceCollection services) { var allowSpecificOrigins = JsonConfig.GetSection("AllowSpecificOrigins:domains").Get <IEnumerable <string> >(); services.AddCors(options => { options.AddPolicy(AllowSpecificOrigins, builder => { builder .AllowAnyHeader() .AllowAnyMethod() .AllowAnyOrigin() .WithExposedHeaders() .WithOrigins(allowSpecificOrigins.ToArray()); }); }); services.AddMvc(options => { options.Filters.Add(typeof(CustomExceptionFilterAttribute)); options.Filters.Add(typeof(ProcessTimeAttribute)); }); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); ConfigureAuth(services); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Version = "v1", Title = "Renting Master Services", Description = "Servicio Rest de Master OnPremise" }); c.AddSecurityDefinition("Bearer", new ApiKeyScheme { In = "header", Description = "Please enter JWT with Bearer into field", Name = "Authorization", Type = "apiKey" }); c.AddSecurityRequirement(new Dictionary <string, IEnumerable <string> > { { "Bearer", Enumerable.Empty <string>() } }); c.CustomSchemaIds(x => x.FullName); // Set the comments path for the Swagger JSON and UI. var xmlFile = "Renting.MasterServices.Api.xml"; var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); c.IncludeXmlComments(xmlPath); }); CreateDependencyInjection(services); return(new AutofacServiceProvider(ApplicationContainer)); }
/// <summary> /// /// </summary> /// <param name="services"></param> public void CreateDependencyInjection(IServiceCollection services) { // create a Autofac container builder ContainerBuilder builder = new ContainerBuilder(); // read service collection to Autofac builder.Populate(services); var mapperConfiguration = new MapperConfiguration(configurationExpresion => { configurationExpresion.AddProfile(new MappingProfile()); }); //Log4Net var log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); builder.Register(c => log).As <ILog>(); //Renting.TokenGenerator var kvc = new RentingKeyVaultCredentials { AppId = JsonConfig.GetSection("kvc:AppId").Value, AppSecret = JsonConfig.GetSection("kvc:AppSecret").Value, KeyVaultUri = JsonConfig.GetSection("kvc:KeyVaultUri").Value }; builder.RegisterType <TokenCredentialsFetchRepository>().As <ITokenCredentialsFetchRepository>() .WithParameter("keyVaultCredential", kvc); builder.RegisterType <JwtFetchRepository>().As <IJwtFetchRepository>(); builder.RegisterType <JwtTokenFetcher>().As <IJwtTokenFetcher>(); //AutoMapper builder.Register(componentContext => mapperConfiguration.CreateMapper()).As <IMapper>(); //DataContext ConfigDataContext(builder); //Repository ConfigRepository(builder); //Services ConfigService(builder, JsonConfig); }