From 2ce8d60af0cce29d249f32582198e7cefcc090d2 Mon Sep 17 00:00:00 2001 From: SharifovDeveloper Date: Tue, 2 Apr 2024 16:59:01 +0500 Subject: [PATCH 1/2] Added ConfigureServicesExtensions and Intalled Serilog,Serilog.file and console --- .../Extensions/ConfigureServicesExtensions.cs | 67 ++++++++++++++++++- Inflow.Api/Inflow.Api.csproj | 4 ++ Inflow.Domain/Inflow.Domain.csproj | 1 + .../Inflow.Infrastructure.csproj | 1 + Inflow.Service/Inflow.Service.csproj | 1 + 5 files changed, 72 insertions(+), 2 deletions(-) diff --git a/Inflow.Api/Extensions/ConfigureServicesExtensions.cs b/Inflow.Api/Extensions/ConfigureServicesExtensions.cs index ede9df6..d84769f 100644 --- a/Inflow.Api/Extensions/ConfigureServicesExtensions.cs +++ b/Inflow.Api/Extensions/ConfigureServicesExtensions.cs @@ -1,7 +1,70 @@ -namespace Inflow.Api.Extensions +using DiyorMarket.Infrastructure.Persistence.Repositories; +using DiyorMarket.Services; +using Inflow.Domain.Intefaces.Services; +using Inflow.Domain.Interfaces.Repositories; +using Inflow.Domain.Interfaces.Services; +using Inflow.Infrastructure; +using Microsoft.EntityFrameworkCore; +using Serilog; + +namespace Inflow.Api.Extensions { - public class ConfigureServicesExtensions + public static class ConfigureServicesExtensions { + public static IServiceCollection ConfigureRepositories(this IServiceCollection services) + { + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddSingleton(); + + services.AddControllers() + .AddNewtonsoftJson(options => + options.SerializerSettings.ReferenceLoopHandling = + Newtonsoft.Json.ReferenceLoopHandling.Ignore + ); + + return services; + } + + public static IServiceCollection ConfigureLogger(this IServiceCollection services) + { + Log.Logger = new LoggerConfiguration() + .MinimumLevel.Information() + .WriteTo.Console() + .WriteTo.File("logs/logs.txt", rollingInterval: RollingInterval.Day) + .WriteTo.File("logs/error_.txt", Serilog.Events.LogEventLevel.Error, rollingInterval: RollingInterval.Day) + .CreateLogger(); + + return services; + } + + public static IServiceCollection ConfigureDatabaseContext(this IServiceCollection services) + { + var builder = WebApplication.CreateBuilder(); + + services.AddDbContext(options => + options.UseSqlServer(builder.Configuration.GetConnectionString("DiyorMarketConection"))); + + return services; + } } } + diff --git a/Inflow.Api/Inflow.Api.csproj b/Inflow.Api/Inflow.Api.csproj index 4145a61..8b1b7f0 100644 --- a/Inflow.Api/Inflow.Api.csproj +++ b/Inflow.Api/Inflow.Api.csproj @@ -20,6 +20,9 @@ runtime; build; native; contentfiles; analyzers; buildtransitive + + + @@ -27,6 +30,7 @@ + diff --git a/Inflow.Domain/Inflow.Domain.csproj b/Inflow.Domain/Inflow.Domain.csproj index 4f1f464..dbed63f 100644 --- a/Inflow.Domain/Inflow.Domain.csproj +++ b/Inflow.Domain/Inflow.Domain.csproj @@ -15,6 +15,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/Inflow.Infrastructure/Inflow.Infrastructure.csproj b/Inflow.Infrastructure/Inflow.Infrastructure.csproj index 71cc139..30129fb 100644 --- a/Inflow.Infrastructure/Inflow.Infrastructure.csproj +++ b/Inflow.Infrastructure/Inflow.Infrastructure.csproj @@ -9,6 +9,7 @@ + diff --git a/Inflow.Service/Inflow.Service.csproj b/Inflow.Service/Inflow.Service.csproj index 0e51d6a..081379c 100644 --- a/Inflow.Service/Inflow.Service.csproj +++ b/Inflow.Service/Inflow.Service.csproj @@ -9,6 +9,7 @@ + From 5304664a52731d7bfeb1521bf12e45ef4f0e93b0 Mon Sep 17 00:00:00 2001 From: SharifovDeveloper Date: Tue, 2 Apr 2024 17:18:30 +0500 Subject: [PATCH 2/2] Installed Serilog.Asp.NetCore ngp , Authentication.JwtToken and updated program cs --- Inflow.Api/Inflow.Api.csproj | 2 ++ Inflow.Api/Program.cs | 49 +++++++++++++++++++++++++++++++++--- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/Inflow.Api/Inflow.Api.csproj b/Inflow.Api/Inflow.Api.csproj index 8b1b7f0..8c6b36a 100644 --- a/Inflow.Api/Inflow.Api.csproj +++ b/Inflow.Api/Inflow.Api.csproj @@ -11,6 +11,7 @@ + @@ -21,6 +22,7 @@ + diff --git a/Inflow.Api/Program.cs b/Inflow.Api/Program.cs index 48863a6..977f2df 100644 --- a/Inflow.Api/Program.cs +++ b/Inflow.Api/Program.cs @@ -1,14 +1,56 @@ +using Inflow.Api.Extensions; +using Inflow.Api.Middlewares; +using Microsoft.AspNetCore.StaticFiles; +using Microsoft.IdentityModel.Tokens; +using Newtonsoft.Json.Serialization; +using Serilog; +using System.Text; + var builder = WebApplication.CreateBuilder(args); -// Add services to the container. +builder.Host.UseSerilog(); + +builder.Services.AddControllers() + .AddNewtonsoftJson(options => + { + options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); + options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; + }) + .AddXmlSerializerFormatters(); -builder.Services.AddControllers(); -// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); +builder.Services.AddSingleton(); +builder.Services.ConfigureLogger(); +builder.Services.ConfigureRepositories(); +builder.Services.ConfigureDatabaseContext(); +builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); +builder.Services.AddAuthentication("Bearer") + .AddJwtBearer(options => + { + options.TokenValidationParameters = new() + { + ValidateIssuer = true, + ValidateAudience = true, + ValidateIssuerSigningKey = true, + ValidIssuer = "anvar-api", + ValidAudience = "anvar-mobile", + IssuerSigningKey = new SymmetricSecurityKey( + Encoding.UTF8.GetBytes("anvarSekretKalitSozMalades")), + ValidateLifetime = true, + }; + }); var app = builder.Build(); +using (var scope = app.Services.CreateScope()) +{ + var services = scope.ServiceProvider; + builder.Services.SeedDatabase(services); +} + +app.UseMiddleware(); + // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { @@ -18,6 +60,7 @@ app.UseHttpsRedirection(); +app.UseAuthentication(); app.UseAuthorization(); app.MapControllers();