// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddCors(options => options.AddDefaultPolicy(builder => { builder.AllowAnyOrigin() .AllowAnyHeader() .AllowAnyMethod(); } )); services.AddControllers(); //Adiciona a conexão ao banco var connection = Configuration["MySQLConnection:MySQLConnectionString"]; services.AddDbContext <MySQLContext>(options => options.UseMySql(connection)); if (Environment.IsDevelopment()) { MigrateDatabase(connection); } services.AddMvc(options => { options.RespectBrowserAcceptHeader = true; //Adiciona formato XML a aplicação options.FormatterMappings.SetMediaTypeMappingForFormat("xml", MediaTypeHeaderValue.Parse("application/xml")); options.FormatterMappings.SetMediaTypeMappingForFormat("json", MediaTypeHeaderValue.Parse("application/json")); }) .AddXmlSerializerFormatters(); var filterOptions = new HyperMediaFilterOptions(); filterOptions.ContentResponseEnricherList.Add(new PersonEnricher()); services.AddSingleton(filterOptions); //Adiciona versionamento a API services.AddApiVersioning(); //Injecao de Dependencia services.AddScoped <IPersonBusiness, PersonBusiness>(); services.AddScoped <IBookBusiness, BookBusiness>(); services.AddScoped(typeof(IRepository <>), typeof(GenericRepository <>)); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "RestWithASPNET5", Version = "v1" }); }); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddCors(options => options.AddDefaultPolicy(builder => { builder.AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader(); })); services.AddControllers(); var connection = Configuration["MySqlConnection:MySqlConnectionString"]; if (Environment.IsDevelopment()) { MigrateDataBase(connection); } services.AddMvc(options => { options.RespectBrowserAcceptHeader = true; options.FormatterMappings.SetMediaTypeMappingForFormat("xml", MediaTypeHeaderValue.Parse("application/xml")); options.FormatterMappings.SetMediaTypeMappingForFormat("json", MediaTypeHeaderValue.Parse("application/json")); }).AddXmlSerializerFormatters(); services.AddDbContext <MySqlContext>(options => options.UseMySql(connection, ServerVersion.AutoDetect(connection))); var filterOptions = new HyperMediaFilterOptions(); filterOptions.ContentResponseEnricherList.Add(new PersonEnricher()); filterOptions.ContentResponseEnricherList.Add(new BookEnricher()); services.AddSingleton(filterOptions); services.AddApiVersioning(); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "REST API's From 0 to Azure With Docker", Version = "v1", Description = "API RESTFul developed in course 'REST API's From 0 to Azure with ASP.NET 5' ", Contact = new OpenApiContact { Name = "Vially Israel", Url = new Uri("https://github.com/Laoviah") } } ); }); services.AddScoped <IPersonBusiness, PersonBusinessImplementation>(); services.AddScoped <IBooksBusiness, BooksBusinessImplementation>(); services.AddScoped(typeof(IRepository <>), typeof(GenericRepository <>)); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers(); var connection = Configuration["MySQLConnection:MySQLConnectionString"]; services.AddDbContext <MySQLContext>(options => options.UseMySql(connection)); if (Environment.IsDevelopment()) { MigrateDatabase(connection); } services.AddMvc(options => { options.RespectBrowserAcceptHeader = true; options.FormatterMappings.SetMediaTypeMappingForFormat("xml", MediaTypeHeaderValue.Parse("application/xml")); options.FormatterMappings.SetMediaTypeMappingForFormat("json", MediaTypeHeaderValue.Parse("application/json")); }) .AddXmlSerializerFormatters(); var filterOptions = new HyperMediaFilterOptions(); filterOptions.ContentResponseEnricherList.Add(new PersonEnricher()); filterOptions.ContentResponseEnricherList.Add(new BooksEnricher()); services.AddSingleton(filterOptions); //Versioning API services.AddApiVersioning(); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "REST API Books", Version = "v1", Description = "API RESTFul developed in course 'REST API's From 0 to Azure with ASP.NET Core 5 and Docker'", Contact = new OpenApiContact { Name = "Márcio Vasconcelos", Url = new Uri("https://github.com/marciovasconcelosjr") } }); }); //Dependency Injection services.AddScoped <IPersonBusiness, PersonBusinessImplementation>(); services.AddScoped <IBooksBusiness, BooksBusinessImplementation>(); services.AddScoped(typeof(IRepository <>), typeof(GenericRepository <>)); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { var connectionString = configuration["MySqlConnection:MySqlConnectionString"]; services.AddDbContext <MySQLContext>(options => options.UseMySql(connectionString)); if (this.environment.IsDevelopment()) { try { var evolveConnection = new MySql.Data.MySqlClient.MySqlConnection(connectionString); var evolve = new Evolve.Evolve("evolve.json", evolveConnection, msg => logger.LogInformation(msg)) { // Where are the magrations: Locations = new List <string> { "Database/migrations" }, // Does not clean database IsEraseDisabled = true }; evolve.Migrate(); } catch (Exception exception) { this.logger.LogCritical("Database migration failed: ", exception); throw; } } services.AddMvc(options => { options.RespectBrowserAcceptHeader = true; options.FormatterMappings.SetMediaTypeMappingForFormat("xml", MediaTypeHeaderValue.Parse("text/xml")); options.FormatterMappings.SetMediaTypeMappingForFormat("json", MediaTypeHeaderValue.Parse("application/json")); }).AddXmlSerializerFormatters(); var filterOptions = new HyperMediaFilterOptions(); filterOptions.ObjectContentResponseEnricherList.Add(new PersonEnricher()); services.AddSingleton(filterOptions); services.AddApiVersioning(); /* Dependency Injection:as * * This mapping between the interface and the concrete type defines, * that everytime you request a type of IPersonService, you'll get a new instance of the PersonServiceImpl. */ services.AddScoped <IPersonService, PersonServiceImpl>(); services.AddScoped <IBookService, BookServiceImpl>(); services.AddScoped <IPersonRepository, PersonRepositoryImpl>(); services.AddScoped(typeof(IRepository <>), typeof(GenericRepository <>)); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers(); var connection = Configuration["MySQLConnection:MySQLConnectionString"]; services.AddDbContext <MySQLContext>(options => options.UseMySql(connection)); if (Environment.IsDevelopment()) { MigrateDatabase(connection); } services.AddMvc(options => { options.RespectBrowserAcceptHeader = true; options.FormatterMappings.SetMediaTypeMappingForFormat("xml", MediaTypeHeaderValue.Parse("application/xml")); options.FormatterMappings.SetMediaTypeMappingForFormat("json", MediaTypeHeaderValue.Parse("application/json")); }) .AddXmlSerializerFormatters(); var filterOptions = new HyperMediaFilterOptions(); filterOptions.ContentResponseEnricherList.Add(new PersonEnricher()); filterOptions.ContentResponseEnricherList.Add(new BookEnricher()); services.AddSingleton(filterOptions); //Versionamento API services.AddApiVersioning(); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "REST API's do 0 ao Azure", Version = "v1", Description = "API RESTful desenvolvida no curso REST API's do 0 ao Azure", Contact = new OpenApiContact { Name = "Heverton Reis", Url = new Uri("https://github.com/hevertonreis-hsrn") } }); }); //Inje��o de Depend�ncia services.AddScoped <IPersonBusiness, PersonBusinessImplementation>(); services.AddScoped <IBookBusiness, BookBusinessImplementation>(); services.AddScoped(typeof(IRepository <>), typeof(GenericRepository <>)); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { //Add banco var connection = Configuration["MySqlConnectionString:MySqlConnectionString"]; services.AddDbContext <MySQLContext>(options => options.UseMySql(connection)); //Se algum cliente antigo precisar de um formato xml services.AddMvc( options => { options.RespectBrowserAcceptHeader = true; options.FormatterMappings.SetMediaTypeMappingForFormat("xml", MediaTypeHeaderValue.Parse("txt/xml")); options.FormatterMappings.SetMediaTypeMappingForFormat("json", MediaTypeHeaderValue.Parse("application/json")); }).AddXmlSerializerFormatters(); //Adicionando HATEOAS Hypermedia no startup var filterOptions = new HyperMediaFilterOptions(); filterOptions.ObjectContentResponseEnricherList.Add(new PersonEnricher()); services.AddSingleton(filterOptions); //Swagger documentacao da api services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo { Title = "RESTful API with ASP.NET Core 2.0", Version = "V1" }); }); //Add versionador services.AddApiVersioning(); //Injecao de dependencias services.AddScoped <IPersonBusinnes, PersonBusinnesImpl>(); services.AddScoped <IBookBusinnes, BookBusinnesImpl>(); services.AddScoped <IPersonRepository, PersonRepository>(); services.AddScoped <IBookBusinnes, BookBusinnesImpl>(); services.AddScoped <IBookRepository, BookRepository>(); //repositorio generico services.AddScoped(typeof(IRepository <>), typeof(GenericRepository <>)); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { // Pega a Connection String no arquivo appsettings.json var connectionString = Configuration["MySqlConnection:MySqlConnectionString"]; services.AddDbContext <MySqlContext>(opcoes => opcoes.UseMySql(connectionString)); ExecutarMigration(connectionString); // O Content Negociation é uma forma da API prover diversos formatos na resposta. // Pacote: Microsoft.AspNetCore.Mvc.Formatters.Xml // Ver mais detalhes em: https://blog.jeremylikness.com/5-rest-api-designs-in-dot-net-core-1-29a8527e999chttps://blog.jeremylikness.com/5-rest-api-designs-in-dot-net-core-1-29a8527e999c services.AddMvc(opcoes => { opcoes.RespectBrowserAcceptHeader = true; // Aceita o que vier no cabeçalho da requisição. opcoes.FormatterMappings.SetMediaTypeMappingForFormat("xml", MediaTypeHeaderValue.Parse("text/xml")); opcoes.FormatterMappings.SetMediaTypeMappingForFormat("json ", MediaTypeHeaderValue.Parse("application/json")); }) .AddXmlSerializerFormatters(); // Define as opções do filtro HATEOAS. var opcoesFiltro = new HyperMediaFilterOptions(); opcoesFiltro.ObjectContentResponseEnricherList.Add(new PessoaEnricher()); opcoesFiltro.ObjectContentResponseEnricherList.Add(new LivroEnricher()); services.AddSingleton(opcoesFiltro); // Versionamento da API. // Referênica: https://github.com/Microsoft/aspnet-api-versioning/wiki/versioning-via-the-url-path // Pacote: Microsoft.AspNetCore.Mvc.Versioning services.AddApiVersioning( option => option.ReportApiVersions = true // Permite que a API retorne versões no cabeçalho de resposta. ); // Swagger é um framework para documentar os nossos endpoints. // Bibliotecas: Swashbuckle.AspNetCore e Swashbuckle.AspNetCore.Annotations services.AddSwaggerGen(opcoes => { opcoes.SwaggerDoc( "v1", new Info { Title = "RESTful API com ASP.NET Core", Version = "v1" }); }); // Injeção de dependência services.AddScoped <IPessoaService, PessoaService>(); services.AddScoped <ILivroService, LivroService>(); services.AddScoped(typeof(IRepository <>), typeof(GenericRepository <>)); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { var connectionString = Configuration["MySqlConnection:MySqlConnectionString"]; services.AddDbContext <MySQLContext>(options => options.UseMySql(connectionString)); if (this.environment.IsDevelopment()) { try { var evolveConnection = new MySql.Data.MySqlClient.MySqlConnection(connectionString); var evolve = new Evolve.Evolve("Evolve.json", evolveConnection, msg => this.logger.LogInformation(msg)) { Locations = new List <string> { "db/migrations" }, IsEraseDisabled = true, }; evolve.Migrate(); } catch (Exception ex) { this.logger.LogCritical("Database migration failed.", ex); throw; } } services.AddMvc(options => { options.RespectBrowserAcceptHeader = true; options.FormatterMappings.SetMediaTypeMappingForFormat("xml", MediaTypeHeaderValue.Parse("text/xml")); options.FormatterMappings.SetMediaTypeMappingForFormat("json", MediaTypeHeaderValue.Parse("application/json")); }).AddXmlSerializerFormatters(); var filterOptions = new HyperMediaFilterOptions(); filterOptions.ObjectContentResponseEnricherList.Add(new PersonEnricher()); services.AddApiVersioning(option => option.ReportApiVersions = true); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Title = "RESTful API With ASP.NET Core 2.0", Version = "v1" }); }); //Dependency Injection services.AddScoped <IPersonBusiness, PersonBusiness>(); services.AddScoped <IPersonRepository, PersonRepository>(); services.AddScoped <IBookBusiness, BookBusiness>(); services.AddScoped(typeof(IRepository <>), typeof(GenericRepository <>)); services.AddSingleton(filterOptions); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { var connectionString = _configuration["MySqlConnection:MySqlConnectionString"]; services.AddDbContext <MySqlContext>(options => options.UseMySql(connectionString)); if (_environment.IsDevelopment()) { try { var evolveConnection = new MySql.Data.MySqlClient.MySqlConnection(connectionString); var evolve = new Evolve.Evolve("Evolve.json", evolveConnection, msg => _logger.LogInformation(msg)) { Locations = new List <string> { "db/migrations" }, IsEraseDisabled = true, }; evolve.Migrate(); } catch (System.Exception ex) { _logger.LogCritical("DataBase migrations failed.", ex); throw; } } services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); //Enricher var filterOptions = new HyperMediaFilterOptions(); filterOptions.ObjectContentResponseEnricherList.Add(new PersonEnricher()); filterOptions.ObjectContentResponseEnricherList.Add(new BookEnricher()); services.AddSingleton(filterOptions); services.AddApiVersioning(); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Title = "RESTful API With ASP.NET Core 2.1", Version = "v1" }); }); //Dependency Injection services.AddScoped <Business.IPersonBusiness, Business.Implementations.PersonBusinessImpl>(); services.AddScoped <Repository.IPersonRepository, Repository.Implementations.PersonRepositoryImpl>(); services.AddScoped <Business.IBookBusiness, Business.Implementations.BookBusinessImpl>(); services.AddScoped(typeof(Repository.Generic.IRepository <>), typeof(Repository.Generic.GenericRepository <>)); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { var connectionString = _configuration["MySqlConnection:MySqlConnectionString"]; services.AddDbContext <MySqlContext>(options => options.UseMySQL(connectionString)); if (_environment.IsDevelopment()) { try { var evolveConnection = new MySql.Data.MySqlClient.MySqlConnection(connectionString); var evolve = new Evolve.Evolve("evolve.json", evolveConnection, msg => _logger.LogInformation(msg)) { Locations = new List <string> { "db/migrations" }, IsEraseDisabled = true, }; evolve.Migrate(); } catch (Exception ex) { _logger.LogCritical("Database migration failed.", ex); throw; } } //Add framework services services.AddMvc(options => { options.RespectBrowserAcceptHeader = true; options.FormatterMappings.SetMediaTypeMappingForFormat("xml", MediaTypeHeaderValue.Parse("text/xml")); options.FormatterMappings.SetMediaTypeMappingForFormat("json", MediaTypeHeaderValue.Parse("application/json")); } ).AddXmlSerializerFormatters().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); var filterOptions = new HyperMediaFilterOptions(); filterOptions.ObjectContentResponseEnricherList.Add(new PersonEnricher()); services.AddSingleton(filterOptions); services.AddApiVersioning(); //Dependency Injection services.AddScoped <IPersonBusiness, PersonBusinessImpl>(); services.AddScoped <IBookBusiness, BookBusinessImpl>(); services.AddScoped <IPersonRepository, PersonRepositoryImpl>(); services.AddScoped(typeof(IRepository <>), typeof(GenericRepository <>)); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { var connectionString = _configuration["MySqlConnection:MySqlConnectionString"]; services.AddDbContext <MySqlContext>(options => options.UseMySQL(connectionString)); ExecuteMigrations(connectionString); Authentication(services); // Enables the use of the token as a means of // authorizing access to this project's resources services.AddAuthorization(auth => { auth.AddPolicy("Bearer", new AuthorizationPolicyBuilder() .AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme) .RequireAuthenticatedUser().Build()); }); //Add framework services services.AddMvc(options => { options.RespectBrowserAcceptHeader = true; options.FormatterMappings.SetMediaTypeMappingForFormat("xml", MediaTypeHeaderValue.Parse("text/xml")); options.FormatterMappings.SetMediaTypeMappingForFormat("json", MediaTypeHeaderValue.Parse("application/json")); } ).AddXmlSerializerFormatters().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); var filterOptions = new HyperMediaFilterOptions(); filterOptions.ObjectContentResponseEnricherList.Add(new PersonEnricher()); services.AddSingleton(filterOptions); services.AddApiVersioning(option => option.ReportApiVersions = true); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Title = "RESTful API With ASP.NET CORE 2.0", Version = "v1" }); }); //Dependency Injection services.AddScoped <IPersonBusiness, PersonBusinessImpl>(); services.AddScoped <IBookBusiness, BookBusinessImpl>(); services.AddScoped <IFileBusiness, FileBusinessImpl>(); services.AddScoped <ILoginBusiness, LoginBusinessImpl>(); services.AddScoped <IUserRepository, UserRepositoryImpl>(); services.AddScoped <IPersonRepository, PersonRepositoryImpl>(); services.AddScoped(typeof(IRepository <>), typeof(GenericRepository <>)); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { var connectionString = _configuration["MysqlConnection:MysqlConnectionString"]; services.AddDbContext <MysqlContext>(options => options.UseMySql(connectionString)); //Executando o Migrations ExecuteMigrations(connectionString); services.AddMvc(options => { options.RespectBrowserAcceptHeader = true; options.FormatterMappings.SetMediaTypeMappingForFormat("xml", MediaTypeHeaderValue.Parse("text/xml")); options.FormatterMappings.SetMediaTypeMappingForFormat("json", MediaTypeHeaderValue.Parse("json/xml")); }).SetCompatibilityVersion(CompatibilityVersion.Version_2_1) .AddXmlSerializerFormatters(); //HEATOAS var filterOptions = new HyperMediaFilterOptions(); filterOptions.ObjectContentResponseEnricherList.Add(new PersonEnricher()); services.AddSingleton(filterOptions); //SWAGGER services.AddSwaggerGen(c => { //v1 é a versão utilizada c.SwaggerDoc("v1", new Info { Title = "Restfull API With ASP.NET Core 2" + "v1" }); }); //Serviço de versionamento services.AddApiVersioning(); //Aqui estamos tratando a injeção de dependencias / Dependency Injection services.AddScoped <IPersonBusiness, PersonBusinessImp>(); services.AddScoped <IBookBusiness, BookBusinessImp>(); services.AddScoped <ILoginBusiness, LoginBusinessImp>(); services.AddScoped <IUserRepository, UserRepositoryImp>(); //Injeção de dependencia do nosso generics services.AddScoped(typeof(IRepository <>), typeof(GenericRepository <>)); // Método responsável por controlar a autenticação de usuários - TOKEN AutenticacaoDeUsuario(services); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { // injetando a string de conexao do banco na classe de contexto do EntityFramework var connection = _configuration["ConnectionString:SqlServerStringConnection"]; services.AddDbContext <SqlContext>(options => options.UseSqlServer(connection, opt => opt.EnableRetryOnFailure())); // execulta metodo para rodar migrations ExeculteMigrations(connection); // Autenticação Authentication(services); // Configuracoes do Data Content services.AddMvc(options => { options.RespectBrowserAcceptHeader = true; options.FormatterMappings.SetMediaTypeMappingForFormat("xml", MediaTypeHeaderValue.Parse("text/xml")); options.FormatterMappings.SetMediaTypeMappingForFormat("json", MediaTypeHeaderValue.Parse("application/json")); }).AddXmlSerializerFormatters(); // configuracoes do HATEOS var filterOptions = new HyperMediaFilterOptions(); filterOptions.ObjectContentResponseEnricherList.Add(new PersonEnricher()); filterOptions.ObjectContentResponseEnricherList.Add(new BookEnricher()); services.AddSingleton(filterOptions); // configuracoes do swagger services.AddApiVersioning(option => option.ReportApiVersions = true); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Title = "Restiful API with ASP.NET Core 2.0", Version = "v1" }); }); //Injeçoes de dependencia services.AddScoped <IPersonBusiness, PersonBusinessImp>(); services.AddScoped <IBookBusiness, BookBusinessImp>(); services.AddScoped <IPersonRepository, PersonRepositoryImp>(); services.AddScoped <ILoginBusiness, LoginBusinessImp>(); services.AddScoped <IFileBusiness, FileBusinessImp>(); services.AddScoped <IUsersRepository, UsersRepositoryImp>(); services.AddScoped(typeof(IRepository <>), typeof(GenericRepository <>)); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers(); var connection = Configuration["MySQLConection:MySQLConectionString"]; if (Environment.IsDevelopment()) { MigrateDatabase(connection); } services.AddDbContext <MySQLContext>(options => options.UseMySql(connection)); //Versioning API services.AddApiVersioning(); //Swagger Support services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "APIRest_ASPNET5", Version = "v1", Description = "Just implementing API exemples", Contact = new OpenApiContact { Name = "Yan Andrey", Url = new Uri("https://github.com/yanandrey") } }); }); //Dependency Injection services.TryAddSingleton <IHttpContextAccessor, HttpContextAccessor>(); services.AddScoped <IClientBusiness, ClientBusinessImplementation>(); services.AddScoped <IVehicleBusiness, VehicleBusinessImplementation>(); services.AddScoped <IFileBusiness, FileBusinessImplementation>(); services.AddScoped(typeof(IRepository <>), typeof(GenericRepository <>)); services.AddScoped <IClientRepository, ClientRepository>(); services.AddScoped <IVehicleRepository, VehicleRepository>(); //HATEOAS Support var filterOptions = new HyperMediaFilterOptions(); filterOptions.ContentResponseEnricherList.Add(new ClientEnricher()); filterOptions.ContentResponseEnricherList.Add(new VehicleEnricher()); services.AddSingleton(filterOptions); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddCors(); services.AddApiVersioning(config => { config.DefaultApiVersion = new ApiVersion(1, 0); config.AssumeDefaultVersionWhenUnspecified = true; }); var filterOptions = new HyperMediaFilterOptions(); filterOptions.ContentResponseEnricherList.Add(new UserEnricher()); services.AddSingleton(filterOptions); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { //Adding Migrations Support ExecuteMigrations(services); ConfigureAuthentication(services); services.AddMvc(options => { options.RespectBrowserAcceptHeader = true; options.FormatterMappings.SetMediaTypeMappingForFormat("xml", MediaTypeHeaderValue.Parse("text/xml")); options.FormatterMappings.SetMediaTypeMappingForFormat("json", MediaTypeHeaderValue.Parse("application/json")); })//; //Para passar a saída do brownse para XML descomentar a linha abaixo .AddXmlSerializerFormatters(); var filterOptions = new HyperMediaFilterOptions(); filterOptions.ObjectContentResponseEnricherList.Add(new PersonEnricher()); filterOptions.ObjectContentResponseEnricherList.Add(new BookEnricher()); services.AddSingleton(filterOptions); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); //Versioning services.AddApiVersioning(option => option.ReportApiVersions = true); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Title = "RESTful API With ASP.NET Core 2.1", Version = "v1" }); }); //Dependency Injection services.AddScoped <IPersonBusiness, PersonBusinessImpl>(); services.AddScoped <IBookBusiness, BookBusinessImpl>(); services.AddScoped <ILoginBusiness, LoginBusinessImpl>(); services.AddScoped <IFileBusiness, FileBusinessImpl>(); services.AddScoped <IUserRepository, UserRepositoryImpl>(); services.AddScoped <IPersonRepository, PersonRepositoryImpl>(); services.AddScoped(typeof(IRepository <>), typeof(GenericRepository <>)); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { var connectionString = _configuration["MySqlConnection:MySqlConnectionString"]; services.AddDbContext <MySqlContext>(options => options.UseMySql(connectionString)); if (_environment.IsDevelopment()) { try { var evolveConnection = new MySql.Data.MySqlClient.MySqlConnection(connectionString); var evolve = new Evolve.Evolve(evolveConnection, msg => _logger.LogInformation(msg)) { Locations = new List <string> { "db/migrations" }, IsEraseDisabled = true, }; evolve.Migrate(); } catch (Exception ex) { _logger.LogCritical("Database migragion failed.", ex.ToString()); throw; } } var filteroptions = new HyperMediaFilterOptions(); filteroptions.ObjectContentResponseEnricherList.Add(new PersonEnricher()); services.AddSingleton(filteroptions); services.AddApiVersioning(); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); services.AddSwaggerGen(); //Injeção de dependência services.AddScoped <IPersonBusiness, PersonBusinessImpl>(); services.AddScoped <IBooksBusiness, BooksBusinessImpl>(); services.AddScoped <IRepository, PersonRepositoryImpl>(); //Injeção de dependência do GenericService services.AddScoped(typeof(IRepository <>), typeof(GenericRepository <>)); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { var connection = _configuration["MySQLConnection:MySQLConnectionString"]; services.AddDbContext <MySQLContext>(options => options.UseMySql(connection)); if (_hostingEnvironment.IsDevelopment()) { try { var envolveConnection = new MySql.Data.MySqlClient.MySqlConnection(connection); var evolve = new Evolve.Evolve(envolveConnection, msg => _logger.LogInformation(msg)) { Locations = new List <string> { "db/migrations", "db/dataset" }, IsEraseDisabled = true }; evolve.Migrate(); } catch (System.Exception ex) { _logger.LogCritical($"Migration Database Failed.====>{ex.Message}", ex.Message); } } var filterOptions = new HyperMediaFilterOptions(); filterOptions.ObjectContentResponseEnricherList.Add(new PersonEnricher()); services.AddSingleton(filterOptions); services.AddScoped <IPersonBusiness, PersonBusiness>(); services.AddScoped <IPersonRepository, PersonRepository>(); services.AddScoped <IBookBusiness, BookBusiness>(); services.AddScoped(typeof(IRepository <>), typeof(GenericRepository <>)); services.AddMvc(); services.AddApiVersioning(); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "Hateoas", Version = "v1" }); }); var filterOptions = new HyperMediaFilterOptions(); filterOptions.ContentResponseEnricherList.Add(new PersonEnricher()); services.AddSingleton(filterOptions); services.AddScoped <IPersonBusiness, PersonBusiness>(); services.AddScoped <IPersonRepository, PersonRepository>(); }
private static void InjecaoDependencia(IServiceCollection services) { // Injeção de Dependencia var filterOptions = new HyperMediaFilterOptions(); filterOptions.ObjectContentResponseEnricherList.Add(new PersonEnricher()); services.AddSingleton(filterOptions); // Injeção de Dependencia services.AddScoped <IPersonBusiness, PersonBusinessImpl>(); services.AddScoped <IBookBusiness, BookBusinessImpl>(); services.AddScoped <ILoginBusiness, LoginBusinessImpl>(); // Repositorio services.AddScoped <IUserRepository, UserRepositoryImpl>(); services.AddScoped <IPersonRepository, PersonRepositoryImpl>(); // Injeção de Dependencia do Generics services.AddScoped(typeof(IRepository <>), typeof(GenericRepository <>)); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers(); // Faz a conexão com o BD var connection = Configuration["ConnectionStrings:MySQLConnectionString"]; services.AddDbContext <MySQLContext>(options => options.UseMySql(connection)); if (Environment.IsDevelopment()) { MigrateDatabase(connection); } services.AddMvc(options => { options.RespectBrowserAcceptHeader = true; options.FormatterMappings.SetMediaTypeMappingForFormat("xml", MediaTypeHeaderValue.Parse("application/xml")); options.FormatterMappings.SetMediaTypeMappingForFormat("json", MediaTypeHeaderValue.Parse("application/json")); }) .AddXmlSerializerFormatters(); var filterOptions = new HyperMediaFilterOptions(); filterOptions.ContentResponseEnricherList.Add(new PersonEnricher()); filterOptions.ContentResponseEnricherList.Add(new BookEnricher()); services.AddSingleton(filterOptions); services.AddApiVersioning(); // Faz o versionamento, mas é preciso instalar o pacote Microsoft.AspNetCore.Mvc.Versioning // Injeção de dependência *person* services.AddScoped <IPersonBusiness, PersonBusinessImplementation>(); // Injeção de dependência *books* services.AddScoped <IBookBusiness, BookBusinessImplementation>(); // Injeção de dependência *repositório genérico* services.AddScoped(typeof(IRepository <>), typeof(GenericRepository <>)); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddDbContext <utilidadesContext> (options => options.UseMySQL(_connectionString) ); ConfigureAutorization(services); services.AddMvc(options => { options.RespectBrowserAcceptHeader = true; options.FormatterMappings.SetMediaTypeMappingForFormat("xml", MediaTypeHeaderValue.Parse("text/xml")); options.FormatterMappings.SetMediaTypeMappingForFormat("json", MediaTypeHeaderValue.Parse("application/json")); }) .AddXmlSerializerFormatters() .SetCompatibilityVersion(CompatibilityVersion.Version_2_1); var filterOptions = new HyperMediaFilterOptions(); filterOptions.ObjectContentResponseEnricherList.Add(new UsersTypeEnricher()); filterOptions.ObjectContentResponseEnricherList.Add(new UserEnricher()); services.AddSingleton(filterOptions); services.AddApiVersioning(); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Title = "Sistema de Utilidades", Version = "v1" }); }); //Dependency Injection services.AddScoped <IUserBusiness, UserBusinessImpl> (); services.AddScoped <IUsersTypeBusiness, UsersTypeBusinessImpl> (); services.AddScoped <ILoginBusiness, LoginBusinessImpl> (); services.AddScoped <IUserRepository, UserRepositoryImpl> (); services.AddScoped <IUsersTypeRepository, UsersTypeRepositoryImpl> (); //Dependency Injection of Generic Repository services.AddScoped(typeof(IRepository <>), typeof(GenericRepository <>)); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); services.AddDbContext <Context>(options => options.UseSqlServer(Configuration.GetConnectionString("ConDb"))); services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>(); services.AddSession(); services.AddSession(options => { options.IdleTimeout = TimeSpan.FromDays(1); }); services.AddScoped(typeof(IRepository <>), typeof(Repository <>)); services.AddScoped <SchedulingConverter>(); services.AddScoped <ComputerConverter>(); services.AddScoped <IUserRepository, UserRepository>(); services.AddScoped <IComputerRepository, ComputerRepository>(); services.AddScoped <ISchedulingRepository, SchedulingRepository>(); services.AddScoped <IComputerBusiness, ComputerBusiness>(); services.AddScoped <LoginBusiness>(); services.AddScoped <ISchedulingBusiness, SchedulingBusiness>(); services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) .AddCookie(options => { options.LoginPath = "/users/Login"; }); services.AddSwaggerGen(x => { x.SwaggerDoc("v1", new Info { Title = "Access Console Api", Version = "v1" }); x.ResolveConflictingActions(apiDescriptions => apiDescriptions.First()); }); var filterOptions = new HyperMediaFilterOptions(); filterOptions.ObjectContentResponseEnricherList.Add(new SchedulingEnricher()); filterOptions.ObjectContentResponseEnricherList.Add(new ComputerEnricher()); services.AddSingleton(filterOptions); services.AddCors(); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers(); var connection = Configuration["MySQLConnection:MySQLConnectionString"]; services.AddDbContext <MySQLContext>(options => options.UseMySql(connection)); if (Environment.IsDevelopment()) { MigrationDatabase(connection); } services.AddMvc(options => { options.RespectBrowserAcceptHeader = true; options.FormatterMappings.SetMediaTypeMappingForFormat("xml", MediaTypeHeaderValue.Parse("application/xml")); options.FormatterMappings.SetMediaTypeMappingForFormat("json", MediaTypeHeaderValue.Parse("application/json")); }) .AddXmlSerializerFormatters(); var filterOptions = new HyperMediaFilterOptions(); filterOptions.ContentResponseEnricherList.Add(new PersonEnricher()); filterOptions.ContentResponseEnricherList.Add(new BookEnricher()); services.AddSingleton(filterOptions); //Versioning API services.AddApiVersioning(); //Dependency Injection services.AddScoped <IPersonBusiness, PersonBusinessImplementation>(); services.AddScoped <IBookBusiness, BookBusinessImplementation>(); services.AddScoped(typeof(IRepository <>), typeof(GenericRepository <>)); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { //Migrations ExecuteMigrations(services); //Authentication ExecuteAuthentication(services); //Content Negotiation services.AddMvc(options => { options.RespectBrowserAcceptHeader = true; options.FormatterMappings.SetMediaTypeMappingForFormat("xml", MediaTypeHeaderValue.Parse("text/xml")); options.FormatterMappings.SetMediaTypeMappingForFormat("json", MediaTypeHeaderValue.Parse("application/json")); }).AddXmlSerializerFormatters().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); //HATEOAS var filterOptions = new HyperMediaFilterOptions(); filterOptions.ObjectContentResponseEnricherList.Add(new PersonEnricher()); filterOptions.ObjectContentResponseEnricherList.Add(new BookEnricher()); services.AddSingleton(filterOptions); //API Versioning services.AddApiVersioning(); //Swagger services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Title = "RESTful API With ASP .NET CORE 2.0", Version = "v1" }); }); //DependencyInjection ExecuteDependencyInjection(services); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { var connection = _configuration["MySqlConnection:MySqlConnectionString"]; services.AddDbContext <MySQLContext>(options => options.UseMySql(connection)); ExecuteMigrations(connection); var signingConfigurations = new SigningConfigurations(); services.AddSingleton(signingConfigurations); var tokenConfigurations = new TokenConfiguration(); new ConfigureFromConfigurationOptions <TokenConfiguration>( _configuration.GetSection("TokenConfigurations") ) .Configure(tokenConfigurations); services.AddSingleton(tokenConfigurations); services.AddAuthentication(authOptions => { authOptions.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; authOptions.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }).AddJwtBearer(bearerOptions => { var paramsValidation = bearerOptions.TokenValidationParameters; paramsValidation.IssuerSigningKey = signingConfigurations.Key; paramsValidation.ValidAudience = tokenConfigurations.Audience; paramsValidation.ValidIssuer = tokenConfigurations.Issuer; paramsValidation.ValidateIssuerSigningKey = true; paramsValidation.ValidateLifetime = true; paramsValidation.ClockSkew = TimeSpan.Zero; }); services.AddAuthorization(auth => { auth.AddPolicy("Bearer", new AuthorizationPolicyBuilder() .AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme) .RequireAuthenticatedUser().Build()); }); services.AddTransient <IAdreessAppService, AdreessAppService>(); services.AddTransient <IBalanceAppService, BalanceAppService>(); services.AddTransient <IProductAppService, ProductAppService>(); services.AddTransient <IRequestAppService, RequestAppService>(); services.AddTransient <IUserAppService, UserAppService>(); services.AddTransient(typeof(IBaseAppService <>), typeof(BaseAppService <>)); services.AddTransient <IAdreessServiceDomain, AdreessServiceDomain>(); services.AddTransient <IBalanceServiceDomain, BalanceServiceDomain>(); services.AddTransient <IProductServiceDomain, ProductServiceDomain>(); services.AddTransient <IRequestServiceDomain, RequestServiceDomain>(); services.AddTransient <IUserServiceDomain, UserServiceDomain>(); services.AddTransient(typeof(IBaseServiceDomain <>), typeof(BaseServiceDomain <>)); services.AddTransient <IAdreessRepository, AdreessRepository>(); services.AddTransient <IBalanceRepository, BalanceRepository>(); services.AddTransient <IProductRepository, ProductRepository>(); services.AddTransient <IRequestRepository, RequestRepository>(); services.AddTransient <IUserRepository, UserRepository>(); services.AddTransient(typeof(IBaseRepository <>), typeof(BaseRepository <>)); services.AddMvc(options => { options.RespectBrowserAcceptHeader = true; options.FormatterMappings.SetMediaTypeMappingForFormat("xml", MediaTypeHeaderValue.Parse("text/xml")); options.FormatterMappings.SetMediaTypeMappingForFormat("json", MediaTypeHeaderValue.Parse("application/json")); }) .AddXmlSerializerFormatters(); var filterOptions = new HyperMediaFilterOptions(); filterOptions.ObjectContentResponseEnricherList.Add(new AdreessEnricher()); filterOptions.ObjectContentResponseEnricherList.Add(new BalanceEnricher()); filterOptions.ObjectContentResponseEnricherList.Add(new ProductErincher()); filterOptions.ObjectContentResponseEnricherList.Add(new RequestErincher()); services.AddSingleton(filterOptions); //services.AddApiVersioning(); services.AddApiVersioning(option => option.ReportApiVersions = true); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Title = "WEB API DOTZ", Version = "v1" }); c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First()); }); //services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { var tokenConfigurations = new TokenConfiguration(); new ConfigureFromConfigurationOptions <TokenConfiguration> (Configuration.GetSection("TokenConfiguration")).Configure(tokenConfigurations); services.AddSingleton(tokenConfigurations); services.AddAuthentication(option => { option.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; option.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }) .AddJwtBearer(option => { option.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidateIssuerSigningKey = true, ValidIssuer = tokenConfigurations.Issuer, ValidAudience = tokenConfigurations.Audience, IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(tokenConfigurations.Secret)) }; }); services.AddAuthorization(auth => { auth.AddPolicy("Bearer", new AuthorizationPolicyBuilder() .AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme) .RequireAuthenticatedUser() .Build()); }); services.AddControllers(); //Adiciona a conexão ao banco var connection = Configuration["MySQLConnection:MySQLConnectionString"]; services.AddDbContext <MySQLContext>(options => options.UseMySql(connection)); if (Environment.IsDevelopment()) { MigrateDatabase(connection); } services.AddMvc(options => { options.RespectBrowserAcceptHeader = true; //Adiciona formato XML a aplicação options.FormatterMappings.SetMediaTypeMappingForFormat("xml", MediaTypeHeaderValue.Parse("application/xml")); options.FormatterMappings.SetMediaTypeMappingForFormat("json", MediaTypeHeaderValue.Parse("application/json")); }) .AddXmlSerializerFormatters(); var filterOptions = new HyperMediaFilterOptions(); filterOptions.ContentResponseEnricherList.Add(new PersonEnricher()); services.AddSingleton(filterOptions); services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>(); //Adiciona versionamento a API services.AddApiVersioning(); //Injecao de Dependencia //Business services.AddScoped <IPersonBusiness, PersonBusiness>(); services.AddScoped <IBookBusiness, BookBusiness>(); services.AddScoped <ILoginBusiness, LoginBusiness>(); services.AddScoped <IFileBusiness, FileBusiness>(); //Repositories services.AddScoped(typeof(IRepository <>), typeof(GenericRepository <>)); services.AddScoped <IUserRepository, UserRepository>(); services.AddScoped <IPersonRepository, PersonRepository>(); //Services services.AddTransient <ITokenService, TokenService>(); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "RestWithASPNET5", Version = "v1" }); }); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { var tokenConfigurations = new TokenConfiguration(); new ConfigureFromConfigurationOptions <TokenConfiguration>( Configuration.GetSection("TokenConfigurations") ) .Configure(tokenConfigurations); services.AddSingleton(tokenConfigurations); services.AddAuthentication(options => { options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }) .AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidateIssuerSigningKey = true, ValidIssuer = tokenConfigurations.Issuer, ValidAudience = tokenConfigurations.Audience, IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(tokenConfigurations.Secret)) }; }); services.AddAuthorization(auth => { auth.AddPolicy("Bearer", new AuthorizationPolicyBuilder() .AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme) .RequireAuthenticatedUser().Build()); }); services.AddCors(option => option.AddDefaultPolicy(builder => { builder.AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader(); })); services.AddControllers(); var connection = Configuration["PostgreeConnection:PostgreeConnectionString"]; services.AddDbContext <PostgreSQLContext>(options => options.UseNpgsql(connection)); if (Environment.IsDevelopment()) { MigrateDatabase(connection); } services.AddMvc(options => { options.RespectBrowserAcceptHeader = true; options.FormatterMappings.SetMediaTypeMappingForFormat("xml", MediaTypeHeaderValue.Parse("application/xml")); options.FormatterMappings.SetMediaTypeMappingForFormat("json", MediaTypeHeaderValue.Parse("application/json")); }) .AddXmlSerializerFormatters(); var filterOptions = new HyperMediaFilterOptions(); filterOptions.ContentResponseEnricherList.Add(new PersonEnricher()); filterOptions.ContentResponseEnricherList.Add(new BookEnricher()); services.AddSingleton(filterOptions); //Versioning API services.AddApiVersioning(); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "Rest API's From 0 to Azure with ASP.NET CORE 5 and Docker", Version = "v1", Description = "API RESTful developed in course 'Rest API's From 0 to Azure with ASP.NET CORE 5 and Docker'", Contact = new OpenApiContact { Name = "Claudio Oliveira", Url = new Uri("https://github.com/ClaudioMauricioOliveira") } }); }); // Dependency Injection services.TryAddSingleton <IHttpContextAccessor, HttpContextAccessor>(); services.AddScoped <IPersonBusiness, PersonBusinessImplementation>(); services.AddScoped <IBookBusiness, BookBusinessImplementation>(); services.AddScoped <ILoginBusiness, LoginBusinessImplementation>(); services.AddScoped <IFileBusiness, FileBusinessImplementation>(); services.AddTransient <ITokenService, TokenService>(); services.AddScoped <IUserRepository, UserRepository>(); services.AddScoped <IPersonRepository, PersonRepository>(); services.AddScoped(typeof(IRepository <>), typeof(GenericRepository <>)); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { var connectionString = Configuration["MySqlConnection:MySqlConnectionString"]; services.AddDbContext <MySqlContext>(options => options.UseMySql(connectionString)); var signingConfigurations = new SigningConfigurations(); services.AddSingleton(signingConfigurations); var tokenConfigurations = new TokenConfiguration(); new ConfigureFromConfigurationOptions <TokenConfiguration>( Configuration.GetSection("TokenConfigurations") ) .Configure(tokenConfigurations); services.AddSingleton(tokenConfigurations); services.AddAuthentication(authOptions => { authOptions.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; authOptions.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }).AddJwtBearer(bearerOptions => { var paramsValidation = bearerOptions.TokenValidationParameters; paramsValidation.IssuerSigningKey = signingConfigurations.Key; paramsValidation.ValidAudience = tokenConfigurations.Audience; paramsValidation.ValidIssuer = tokenConfigurations.Issuer; // Validates the signing of a received token paramsValidation.ValidateIssuerSigningKey = true; // Checks if a received token is still valid paramsValidation.ValidateLifetime = true; // Tolerance time for the expiration of a token (used in case // of time synchronization problems between different // computers involved in the communication process) paramsValidation.ClockSkew = TimeSpan.Zero; }); // Enables the use of the token as a means of // authorizing access to this project's resources services.AddAuthorization(auth => { auth.AddPolicy("Bearer", new AuthorizationPolicyBuilder() .AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme) .RequireAuthenticatedUser().Build()); }); services .AddControllers(options => { options.RespectBrowserAcceptHeader = true; options.FormatterMappings.SetMediaTypeMappingForFormat("xml", MediaTypeHeaderValue.Parse("text/xml")); options.FormatterMappings.SetMediaTypeMappingForFormat("json", MediaTypeHeaderValue.Parse("application/json")); }) .AddXmlSerializerFormatters(); var filterOptions = new HyperMediaFilterOptions(); filterOptions.ObjectContentResponseEnricherList.Add(new PersonEnricher()); filterOptions.ObjectContentResponseEnricherList.Add(new BookEnricher()); services.AddSingleton(filterOptions); services.AddScoped <IBookBusiness, BookBusinessImpl>(); services.AddScoped <IPersonBusiness, PersonBusinessImpl>(); services.AddScoped <ILoginBusiness, LoginBusinessImpl>(); services.AddScoped <IUserRepository, UserRepositoryImpl>(); services.AddScoped <IPersonRepository, PersonRepositoryImpl>(); services.AddScoped(typeof(IRepository <>), typeof(GenericRepository <>)); services.AddApiVersioning(option => option.ReportApiVersions = true); services.AddSwaggerGen(s => { s.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo { Title = "RestFull API with ASP.NET Core 3.1", Version = "v1" }); }); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.Configure <IISServerOptions>(options => { options.AllowSynchronousIO = true; }); var connectionString = _configuration["MySqlConnection:MySqlConnectionString"]; services.AddDbContext <MySQLContext>(options => options.UseMySql(connectionString)); services.AddCors(); ExecuteMigrations(connectionString); //Token JWT var signingConfigurations = new SigningConfigurations(); services.AddSingleton(signingConfigurations); var tokenConfigurations = new TokenConfigurations(); new ConfigureFromConfigurationOptions <TokenConfigurations>( _configuration.GetSection("TokenConfigurations") ) .Configure(tokenConfigurations); services.AddSingleton(tokenConfigurations); services.AddAuthentication(authOptions => { authOptions.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; authOptions.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }).AddJwtBearer(bearerOptions => { bearerOptions.RequireHttpsMetadata = false; bearerOptions.SaveToken = true; var paramsValidation = bearerOptions.TokenValidationParameters; paramsValidation.IssuerSigningKey = signingConfigurations.Key; paramsValidation.ValidAudience = tokenConfigurations.Audience; paramsValidation.ValidIssuer = tokenConfigurations.Issuer; paramsValidation.ValidateIssuerSigningKey = true; paramsValidation.ValidateLifetime = true; paramsValidation.ClockSkew = TimeSpan.Zero; }); services.AddAuthorization(auth => { auth.AddPolicy("Bearer", new AuthorizationPolicyBuilder() .AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme) .RequireAuthenticatedUser().Build()); }); //FIM TOKEN services.AddControllers(options => { options.RespectBrowserAcceptHeader = true; options.ReturnHttpNotAcceptable = true; }).AddXmlSerializerFormatters(); var filterOptions = new HyperMediaFilterOptions(); filterOptions.ObjectContentResponseEnricherList.Add(new BookEnricher()); services.AddSingleton(filterOptions); services.AddScoped <IPersonBusiness, PersonBusinessImpl>(); services.AddScoped <IPersonRepository, PersonRepositoryImpl>(); services.AddScoped <IPerson2Repository, Person2RepositoryImpl>(); services.AddScoped <IBookBusiness, BookBusinessImpl>(); services.AddScoped <IPerson2Business, Person2BusinessImpl>(); services.AddScoped <IUserRepository, UserRepositoryImpl>(); services.AddScoped <ILoginBusiness, LoginBusinessImpl>(); services.AddScoped <IFileBusiness, FileBusinessImpl>(); object p = services.AddApiVersioning(); services.AddScoped(typeof(IRepository <>), typeof(GenericRepository <>)); services.AddMvc(options => options.EnableEndpointRouting = false); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "Nóis que voa bruxão (tá facil)", Version = "v1", Description = "Exemplo de API REST", Contact = new OpenApiContact { Name = "Carlos Joaquim", Url = new Uri("https://www.linkedin.com/public-profile/in/carlos-joaquim-ferreira-da-silva-a309644a/?challengeId=AQFDn4tQvKSlmQAAAXTSCnKYde18nQKrWE4O521kNmW9_z3Xk-2MhZ_ESXMYBEUExYgfpOcXRtlSJHjrRj0OlWKCuM1dNq163A&submissionId=23e1b6df-f7c9-3816-d67c-ea6057a40e7a") } }); }); }