diff --git a/.gitignore b/.gitignore index b8a6c1a..6c55c5b 100644 --- a/.gitignore +++ b/.gitignore @@ -24,3 +24,9 @@ /AspNetCore-Effective-Logging/BookClub.Logic/obj /AspNetCore-Effective-Logging/BookClub.Infrastructure/bin/Debug/netstandard2.0 /AspNetCore-Effective-Logging/BookClub.Infrastructure/obj +/AspNetCore-Effective-Logging/BookClub.API/bin/Debug/netcoreapp3.1 +/AspNetCore-Effective-Logging/BookClub.Data/bin/Debug/netstandard2.1 +/AspNetCore-Effective-Logging/BookClub.Entities/bin/Debug/netstandard2.1 +/AspNetCore-Effective-Logging/BookClub.Infrastructure/bin/Debug/netstandard2.1 +/AspNetCore-Effective-Logging/BookClub.Logic/bin/Debug/netstandard2.1 +/AspNetCore-Effective-Logging/BookClub.UI/bin/Debug/netcoreapp3.1 diff --git a/AspNetCore-Effective-Logging/BookClub.API/BookClub.API.csproj b/AspNetCore-Effective-Logging/BookClub.API/BookClub.API.csproj index 3e6b681..173ba4f 100644 --- a/AspNetCore-Effective-Logging/BookClub.API/BookClub.API.csproj +++ b/AspNetCore-Effective-Logging/BookClub.API/BookClub.API.csproj @@ -1,22 +1,17 @@  - netcoreapp2.2 - InProcess - BookClub.API + netcoreapp3.1 - - - - - - - + + + - + + diff --git a/AspNetCore-Effective-Logging/BookClub.API/Startup.cs b/AspNetCore-Effective-Logging/BookClub.API/Startup.cs index ec6f282..7425f9f 100644 --- a/AspNetCore-Effective-Logging/BookClub.API/Startup.cs +++ b/AspNetCore-Effective-Logging/BookClub.API/Startup.cs @@ -2,14 +2,13 @@ using System.Collections.Generic; using System.Data; using System.Data.SqlClient; +using System.Linq; using BookClub.Infrastructure.Middleware; using BookClub.Data; using BookClub.Logic; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Authorization; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -17,17 +16,19 @@ using BookClub.Infrastructure.Filters; using BookClub.Infrastructure; using Microsoft.Extensions.Logging; +using Microsoft.OpenApi.Models; namespace BookClub.API { public class Startup { - private readonly ILoggerFactory _loggerFactory; + //private readonly ILoggerFactory _loggerFactory; - public Startup(IConfiguration configuration, ILoggerFactory loggerFactory) + //public Startup(IConfiguration configuration, ILoggerFactory loggerFactory) + public Startup(IConfiguration configuration) { Configuration = configuration; - _loggerFactory = loggerFactory; + //_loggerFactory = loggerFactory; } public IConfiguration Configuration { get; } @@ -53,17 +54,28 @@ public void ConfigureServices(IServiceCollection services) services.AddSwaggerGen(c => { var oauthScopeDic = new Dictionary { {"api", "Access to the Book Club API"} }; - c.SwaggerDoc("v1", new Info { Title = "Book Club API", Version = "v1" }); - c.AddSecurityDefinition("oauth2", new OAuth2Scheme + c.SwaggerDoc("v1", new OpenApiInfo { Title = "Book Club API", Version = "v1" }); + c.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme { - Type = "oauth2", - Flow = "implicit", - AuthorizationUrl = "https://demo.identityserver.io/connect/authorize", - Scopes = oauthScopeDic + Type = SecuritySchemeType.OAuth2, + Flows = new OpenApiOAuthFlows + { + Implicit = new OpenApiOAuthFlow + { + AuthorizationUrl = new Uri("https://demo.identityserver.io/connect/authorize"), + Scopes = oauthScopeDic + } + } }); - c.AddSecurityRequirement(new Dictionary> + c.AddSecurityRequirement(new OpenApiSecurityRequirement { - { "oauth2", new [] { "api" }} + { + new OpenApiSecurityScheme + { + Reference = new OpenApiReference {Type = ReferenceType.SecurityScheme, Id = "oauth2"} + }, + oauthScopeDic.Keys.ToArray() + } }); }); @@ -73,10 +85,10 @@ public void ConfigureServices(IServiceCollection services) .RequireAuthenticatedUser(); options.Filters.Add(new AuthorizeFilter(builder.Build())); options.Filters.Add(typeof(TrackActionPerformanceFilter)); - }).SetCompatibilityVersion(CompatibilityVersion.Version_2_2); + }); } - public void Configure(IApplicationBuilder app, IHostingEnvironment env) + public void Configure(IApplicationBuilder app) { app.UseApiExceptionHandler(options => { @@ -84,7 +96,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env) options.DetermineLogLevel = DetermineLogLevel; }); app.UseHsts(); - + app.UseStaticFiles(); app.UseSwagger(); app.UseSwaggerUI(options => { @@ -94,7 +106,12 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env) app.UseAuthentication(); app.UseHttpsRedirection(); - app.UseMvc(); + app.UseRouting(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllers(); + }); } private LogLevel DetermineLogLevel(Exception ex) @@ -109,7 +126,7 @@ private LogLevel DetermineLogLevel(Exception ex) private void UpdateApiErrorResponse(HttpContext context, Exception ex, ApiError error) { - if (ex.GetType().Name == typeof(SqlException).Name) + if (ex.GetType().Name == nameof(SqlException)) { error.Detail = "Exception was a database exception!"; } diff --git a/AspNetCore-Effective-Logging/BookClub.Data/BookClub.Data.csproj b/AspNetCore-Effective-Logging/BookClub.Data/BookClub.Data.csproj index c7bc32b..0b17072 100644 --- a/AspNetCore-Effective-Logging/BookClub.Data/BookClub.Data.csproj +++ b/AspNetCore-Effective-Logging/BookClub.Data/BookClub.Data.csproj @@ -1,13 +1,13 @@ - netstandard2.0 + netstandard2.1 BookClub.Data - - + + diff --git a/AspNetCore-Effective-Logging/BookClub.Entities/BookClub.Entities.csproj b/AspNetCore-Effective-Logging/BookClub.Entities/BookClub.Entities.csproj index a5ec3f9..c4faa04 100644 --- a/AspNetCore-Effective-Logging/BookClub.Entities/BookClub.Entities.csproj +++ b/AspNetCore-Effective-Logging/BookClub.Entities/BookClub.Entities.csproj @@ -1,12 +1,12 @@ - netstandard2.0 + netstandard2.1 - - + + diff --git a/AspNetCore-Effective-Logging/BookClub.Infrastructure/BookClub.Infrastructure.csproj b/AspNetCore-Effective-Logging/BookClub.Infrastructure/BookClub.Infrastructure.csproj index d5611c6..4de8894 100644 --- a/AspNetCore-Effective-Logging/BookClub.Infrastructure/BookClub.Infrastructure.csproj +++ b/AspNetCore-Effective-Logging/BookClub.Infrastructure/BookClub.Infrastructure.csproj @@ -1,7 +1,7 @@ - netstandard2.0 + netstandard2.1 diff --git a/AspNetCore-Effective-Logging/BookClub.Logic/BookClub.Logic.csproj b/AspNetCore-Effective-Logging/BookClub.Logic/BookClub.Logic.csproj index 09d5f5d..1887c7f 100644 --- a/AspNetCore-Effective-Logging/BookClub.Logic/BookClub.Logic.csproj +++ b/AspNetCore-Effective-Logging/BookClub.Logic/BookClub.Logic.csproj @@ -1,11 +1,11 @@ - netstandard2.0 + netstandard2.1 - + diff --git a/AspNetCore-Effective-Logging/BookClub.Logic/BookLogic.cs b/AspNetCore-Effective-Logging/BookClub.Logic/BookLogic.cs index ea59941..fff03d3 100644 --- a/AspNetCore-Effective-Logging/BookClub.Logic/BookLogic.cs +++ b/AspNetCore-Effective-Logging/BookClub.Logic/BookLogic.cs @@ -1,6 +1,5 @@ using System.Collections.Generic; using System.Net.Http; -using Newtonsoft.Json; using System.Threading.Tasks; using BookClub.Data; using BookClub.Logic.Models; @@ -8,6 +7,7 @@ using System; using System.Linq; using Microsoft.Extensions.Logging; +using System.Net.Http.Json; namespace BookClub.Logic { @@ -54,14 +54,7 @@ private async Task GetBookModelFromBook(Book book) try { _logger.LogDebug("Calling Google API with ISBN {ISBN} and uri {GoogleUri}", book.Isbn, uri); - var response = await httpClient.GetAsync(uri); - if (!response.IsSuccessStatusCode) - { - throw new Exception($"Failed in Google API for ISBN: {book.Isbn} - responseCode = " + - $"{response.StatusCode}"); - } - var content = await response.Content.ReadAsStringAsync(); - var bookResponse = JsonConvert.DeserializeObject(content); + var bookResponse = await httpClient.GetFromJsonAsync(uri); var thisBook = bookResponse?.Items?.FirstOrDefault(); if (thisBook != null) diff --git a/AspNetCore-Effective-Logging/BookClub.UI/BookClub.UI.csproj b/AspNetCore-Effective-Logging/BookClub.UI/BookClub.UI.csproj index e607171..826b2ca 100644 --- a/AspNetCore-Effective-Logging/BookClub.UI/BookClub.UI.csproj +++ b/AspNetCore-Effective-Logging/BookClub.UI/BookClub.UI.csproj @@ -1,7 +1,7 @@ - netcoreapp2.2 + netcoreapp3.1 InProcess BookClub.UI 64a55203-6024-41a2-9efd-914e7fb0dbd5 @@ -9,14 +9,13 @@ - - - - - + + + + diff --git a/AspNetCore-Effective-Logging/BookClub.UI/Pages/Create.cshtml.cs b/AspNetCore-Effective-Logging/BookClub.UI/Pages/Create.cshtml.cs index 2b96a3b..62d222d 100644 --- a/AspNetCore-Effective-Logging/BookClub.UI/Pages/Create.cshtml.cs +++ b/AspNetCore-Effective-Logging/BookClub.UI/Pages/Create.cshtml.cs @@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.Extensions.Logging; using System.Net.Http; +using System.Net.Http.Json; using System.Threading.Tasks; namespace BookClub.UI.Pages diff --git a/AspNetCore-Effective-Logging/BookClub.UI/Startup.cs b/AspNetCore-Effective-Logging/BookClub.UI/Startup.cs index 96519ac..1318916 100644 --- a/AspNetCore-Effective-Logging/BookClub.UI/Startup.cs +++ b/AspNetCore-Effective-Logging/BookClub.UI/Startup.cs @@ -6,9 +6,7 @@ using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Authorization; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -64,17 +62,18 @@ public void ConfigureServices(IServiceCollection services) }; }); - services.AddMvc(config => + services.AddRazorPages(); + services.AddControllers(config => { var policy = new AuthorizationPolicyBuilder() .RequireAuthenticatedUser() .Build(); config.Filters.Add(new AuthorizeFilter(policy)); //config.Filters.Add(typeof(TrackPagePerformanceFilter)); - }).SetCompatibilityVersion(CompatibilityVersion.Version_2_2); + }); } - public void Configure(IApplicationBuilder app, IHostingEnvironment env) + public void Configure(IApplicationBuilder app) { app.UseExceptionHandler("/Error"); app.UseHsts(); @@ -86,7 +85,11 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env) app.UseStaticFiles(); app.UseCookiePolicy(); - app.UseMvc(); + app.UseRouting(); + app.UseEndpoints(endpoints => + { + endpoints.MapRazorPages(); + }); } private ClaimsPrincipal TransformClaims(ClaimsPrincipal principal)