From 360456f051669f30f6688429308393cdd7fc24b7 Mon Sep 17 00:00:00 2001 From: Unai Zorrilla Castro Date: Tue, 29 Aug 2017 11:01:27 +0200 Subject: [PATCH] Migrated Marketing.API and Locations.API to .NETCoreApp2.0. Update Startup and Program to new .NETCoreApp2.0 templates --- .../Controllers/HomeController.cs | 6 +- .../Repositories/ILocationsRepository.cs | 2 +- .../Services/LocationsService.cs | 2 +- .../Locations.API/Locations.API.csproj | 22 +------ .../Location/Locations.API/Model/Locations.cs | 2 +- .../Location/Locations.API/Program.cs | 21 ++++-- .../Location/Locations.API/Startup.cs | 59 +++++------------ .../ViewModel/LocationRequest.cs | 7 +- .../Marketing.API/Marketing.API.csproj | 28 +------- .../Marketing/Marketing.API/Program.cs | 21 ++++-- .../Marketing/Marketing.API/Startup.cs | 66 +++++++------------ 11 files changed, 80 insertions(+), 156 deletions(-) diff --git a/src/Services/Location/Locations.API/Controllers/HomeController.cs b/src/Services/Location/Locations.API/Controllers/HomeController.cs index e7cea3cca..15cb07404 100644 --- a/src/Services/Location/Locations.API/Controllers/HomeController.cs +++ b/src/Services/Location/Locations.API/Controllers/HomeController.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc; // For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 diff --git a/src/Services/Location/Locations.API/Infrastructure/Repositories/ILocationsRepository.cs b/src/Services/Location/Locations.API/Infrastructure/Repositories/ILocationsRepository.cs index e2d7f9ea2..cd50fa987 100644 --- a/src/Services/Location/Locations.API/Infrastructure/Repositories/ILocationsRepository.cs +++ b/src/Services/Location/Locations.API/Infrastructure/Repositories/ILocationsRepository.cs @@ -1,9 +1,9 @@ namespace Microsoft.eShopOnContainers.Services.Locations.API.Infrastructure.Repositories { using Microsoft.eShopOnContainers.Services.Locations.API.Model; - using ViewModel; using System.Collections.Generic; using System.Threading.Tasks; + using ViewModel; public interface ILocationsRepository { diff --git a/src/Services/Location/Locations.API/Infrastructure/Services/LocationsService.cs b/src/Services/Location/Locations.API/Infrastructure/Services/LocationsService.cs index 81bc16489..771a075d2 100644 --- a/src/Services/Location/Locations.API/Infrastructure/Services/LocationsService.cs +++ b/src/Services/Location/Locations.API/Infrastructure/Services/LocationsService.cs @@ -1,9 +1,9 @@ namespace Microsoft.eShopOnContainers.Services.Locations.API.Infrastructure.Services { - using Microsoft.eShopOnContainers.Services.Locations.API.IntegrationEvents.Events; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; using Microsoft.eShopOnContainers.Services.Locations.API.Infrastructure.Exceptions; using Microsoft.eShopOnContainers.Services.Locations.API.Infrastructure.Repositories; + using Microsoft.eShopOnContainers.Services.Locations.API.IntegrationEvents.Events; using Microsoft.eShopOnContainers.Services.Locations.API.Model; using Microsoft.eShopOnContainers.Services.Locations.API.ViewModel; using System; diff --git a/src/Services/Location/Locations.API/Locations.API.csproj b/src/Services/Location/Locations.API/Locations.API.csproj index 90588c8f7..b1b322708 100644 --- a/src/Services/Location/Locations.API/Locations.API.csproj +++ b/src/Services/Location/Locations.API/Locations.API.csproj @@ -11,22 +11,7 @@ - - - - - - - - - - - - - - - - + @@ -35,10 +20,7 @@ - - - - + diff --git a/src/Services/Location/Locations.API/Model/Locations.cs b/src/Services/Location/Locations.API/Model/Locations.cs index df521b9b7..7c0580fc6 100644 --- a/src/Services/Location/Locations.API/Model/Locations.cs +++ b/src/Services/Location/Locations.API/Model/Locations.cs @@ -1,9 +1,9 @@ namespace Microsoft.eShopOnContainers.Services.Locations.API.Model { using MongoDB.Bson; + using MongoDB.Bson.Serialization.Attributes; using MongoDB.Driver.GeoJsonObjectModel; using System.Collections.Generic; - using MongoDB.Bson.Serialization.Attributes; public class Locations { diff --git a/src/Services/Location/Locations.API/Program.cs b/src/Services/Location/Locations.API/Program.cs index 8a4a486cb..7ebc0ab8a 100644 --- a/src/Services/Location/Locations.API/Program.cs +++ b/src/Services/Location/Locations.API/Program.cs @@ -1,6 +1,8 @@ -using System.IO; +using Microsoft.AspNetCore; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Logging; +using System.IO; namespace Microsoft.eShopOnContainers.Services.Locations.API { @@ -8,14 +10,19 @@ namespace Microsoft.eShopOnContainers.Services.Locations.API { public static void Main(string[] args) { - var host = new WebHostBuilder() - .UseKestrel() + BuildWebHost(args).Run(); + } + + public static IWebHost BuildWebHost(string[] args) => + WebHost.CreateDefaultBuilder(args) .UseHealthChecks("/hc") .UseContentRoot(Directory.GetCurrentDirectory()) .UseStartup() - .Build(); - - host.Run(); - } + .ConfigureLogging((hostingContext, builder) => + { + builder.AddConfiguration(hostingContext.Configuration.GetSection("Logging")); + builder.AddConsole(); + builder.AddDebug(); + }).Build(); } } diff --git a/src/Services/Location/Locations.API/Startup.cs b/src/Services/Location/Locations.API/Startup.cs index 34fd8cb75..7d235ed0f 100644 --- a/src/Services/Location/Locations.API/Startup.cs +++ b/src/Services/Location/Locations.API/Startup.cs @@ -3,58 +3,51 @@ using Autofac.Extensions.DependencyInjection; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; +using Microsoft.Azure.ServiceBus; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ; +using Microsoft.eShopOnContainers.BuildingBlocks.EventBusServiceBus; using Microsoft.eShopOnContainers.Services.Locations.API.Infrastructure; using Microsoft.eShopOnContainers.Services.Locations.API.Infrastructure.Filters; using Microsoft.eShopOnContainers.Services.Locations.API.Infrastructure.Repositories; using Microsoft.eShopOnContainers.Services.Locations.API.Infrastructure.Services; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.HealthChecks; using Microsoft.Extensions.Logging; using RabbitMQ.Client; -using System.Reflection; -using System; -using Microsoft.eShopOnContainers.BuildingBlocks.EventBusServiceBus; -using Microsoft.Azure.ServiceBus; -using System.Collections.Generic; using Swashbuckle.AspNetCore.Swagger; -using Microsoft.Extensions.HealthChecks; +using System; +using System.Collections.Generic; using System.Threading.Tasks; namespace Microsoft.eShopOnContainers.Services.Locations.API { public class Startup { - public IConfigurationRoot Configuration { get; } - - public Startup(IHostingEnvironment env) + public Startup(IConfiguration configuration) { - var builder = new ConfigurationBuilder() - .SetBasePath(env.ContentRootPath) - .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) - .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true); - - if (env.IsDevelopment()) - { - builder.AddUserSecrets(typeof(Startup).GetTypeInfo().Assembly); - } - - builder.AddEnvironmentVariables(); - - Configuration = builder.Build(); + Configuration = configuration; } - // This method gets called by the runtime. Use this method to add services to the container. + public IConfiguration Configuration { get; } + public IServiceProvider ConfigureServices(IServiceCollection services) { - // Add framework services. services.AddMvc(options => { options.Filters.Add(typeof(HttpGlobalExceptionFilter)); }).AddControllersAsServices(); + services.AddAuthentication() + .AddJwtBearer(options => + { + options.Authority = Configuration.GetValue("IdentityUrl"); + options.Audience = "locations"; + options.RequireHttpsMetadata = false; + }); + services.Configure(Configuration); if (Configuration.GetValue("AzureServiceBusEnabled")) @@ -143,14 +136,9 @@ namespace Microsoft.eShopOnContainers.Services.Locations.API // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { - //Configure logs - - loggerFactory.AddConsole(Configuration.GetSection("Logging")); - loggerFactory.AddDebug(); - app.UseCors("CorsPolicy"); - ConfigureAuth(app); + app.UseAuthentication(); app.UseMvcWithDefaultRoute(); @@ -165,17 +153,6 @@ namespace Microsoft.eShopOnContainers.Services.Locations.API .Wait(); } - protected virtual void ConfigureAuth(IApplicationBuilder app) - { - var identityUrl = Configuration.GetValue("IdentityUrl"); - app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions - { - Authority = identityUrl.ToString(), - ApiName = "locations", - RequireHttpsMetadata = false - }); - } - private void RegisterEventBus(IServiceCollection services) { if (Configuration.GetValue("AzureServiceBusEnabled")) diff --git a/src/Services/Location/Locations.API/ViewModel/LocationRequest.cs b/src/Services/Location/Locations.API/ViewModel/LocationRequest.cs index 8619bfc0f..5301cee87 100644 --- a/src/Services/Location/Locations.API/ViewModel/LocationRequest.cs +++ b/src/Services/Location/Locations.API/ViewModel/LocationRequest.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace Microsoft.eShopOnContainers.Services.Locations.API.ViewModel +namespace Microsoft.eShopOnContainers.Services.Locations.API.ViewModel { public class LocationRequest { diff --git a/src/Services/Marketing/Marketing.API/Marketing.API.csproj b/src/Services/Marketing/Marketing.API/Marketing.API.csproj index 71adef034..b834eab2d 100644 --- a/src/Services/Marketing/Marketing.API/Marketing.API.csproj +++ b/src/Services/Marketing/Marketing.API/Marketing.API.csproj @@ -18,28 +18,7 @@ - - - - - - - - - - - - - - - - - - - - - - + @@ -47,10 +26,7 @@ - - - - + diff --git a/src/Services/Marketing/Marketing.API/Program.cs b/src/Services/Marketing/Marketing.API/Program.cs index dbde65b27..197573518 100644 --- a/src/Services/Marketing/Marketing.API/Program.cs +++ b/src/Services/Marketing/Marketing.API/Program.cs @@ -1,21 +1,28 @@ namespace Microsoft.eShopOnContainers.Services.Marketing.API { - using System.IO; using AspNetCore.Hosting; + using Microsoft.AspNetCore; + using Microsoft.Extensions.Logging; + using System.IO; public class Program { public static void Main(string[] args) { - var host = new WebHostBuilder() - .UseKestrel() + BuildWebHost(args).Run(); + } + + public static IWebHost BuildWebHost(string[] args) => + WebHost.CreateDefaultBuilder(args) .UseHealthChecks("/hc") .UseContentRoot(Directory.GetCurrentDirectory()) .UseStartup() .UseWebRoot("Pics") - .Build(); - - host.Run(); - } + .ConfigureLogging((hostingContext, builder) => + { + builder.AddConfiguration(hostingContext.Configuration.GetSection("Logging")); + builder.AddConsole(); + builder.AddDebug(); + }).Build(); } } diff --git a/src/Services/Marketing/Marketing.API/Startup.cs b/src/Services/Marketing/Marketing.API/Startup.cs index e5fa1a451..3a7eab78a 100644 --- a/src/Services/Marketing/Marketing.API/Startup.cs +++ b/src/Services/Marketing/Marketing.API/Startup.cs @@ -1,58 +1,45 @@ namespace Microsoft.eShopOnContainers.Services.Marketing.API { - using Autofac; - using Autofac.Extensions.DependencyInjection; - using IntegrationEvents.Events; using AspNetCore.Builder; using AspNetCore.Hosting; using AspNetCore.Http; + using Autofac; + using Autofac.Extensions.DependencyInjection; using Azure.ServiceBus; - using EntityFrameworkCore; - using EntityFrameworkCore.Infrastructure; using BuildingBlocks.EventBus; using BuildingBlocks.EventBus.Abstractions; using BuildingBlocks.EventBusRabbitMQ; using BuildingBlocks.EventBusServiceBus; + using EntityFrameworkCore; + using Extensions.Configuration; + using Extensions.DependencyInjection; + using Extensions.HealthChecks; + using Extensions.Logging; using Infrastructure; using Infrastructure.Filters; using Infrastructure.Repositories; using Infrastructure.Services; - using Extensions.Configuration; - using Extensions.DependencyInjection; - using Extensions.Logging; + using IntegrationEvents.Events; + using Marketing.API.IntegrationEvents.Handlers; + using Microsoft.EntityFrameworkCore.Diagnostics; using Polly; using RabbitMQ.Client; + using Swashbuckle.AspNetCore.Swagger; using System; + using System.Collections.Generic; using System.Data.SqlClient; using System.Reflection; using System.Threading.Tasks; - using Extensions.HealthChecks; - using Marketing.API.IntegrationEvents.Handlers; - using Microsoft.EntityFrameworkCore.Diagnostics; - using Swashbuckle.AspNetCore.Swagger; - using System.Collections.Generic; public class Startup { - public Startup(IHostingEnvironment env) + public Startup(IConfiguration configuration) { - var builder = new ConfigurationBuilder() - .SetBasePath(env.ContentRootPath) - .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) - .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) - .AddEnvironmentVariables(); - - if (env.IsDevelopment()) - { - builder.AddUserSecrets(typeof(Startup).GetTypeInfo().Assembly); - } - - builder.AddEnvironmentVariables(); - - Configuration = builder.Build(); + Configuration = configuration; } - public IConfigurationRoot Configuration { get; } + public IConfiguration Configuration { get; } + // This method gets called by the runtime. Use this method to add services to the container. public IServiceProvider ConfigureServices(IServiceCollection services) @@ -65,6 +52,14 @@ services.Configure(Configuration); + services.AddAuthentication() + .AddJwtBearer(options => + { + options.Authority = Configuration.GetValue("IdentityUrl"); + options.Audience = "marketing"; + options.RequireHttpsMetadata = false; + }); + services.AddHealthChecks(checks => { checks.AddValueTaskCheck("HTTP Endpoint", () => new ValueTask(HealthCheckResult.Healthy("Ok"))); @@ -179,7 +174,7 @@ app.UseCors("CorsPolicy"); - ConfigureAuth(app); + app.UseAuthentication(); app.UseMvcWithDefaultRoute(); @@ -198,17 +193,6 @@ ConfigureEventBus(app); } - protected virtual void ConfigureAuth(IApplicationBuilder app) - { - var identityUrl = Configuration.GetValue("IdentityUrl"); - app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions - { - Authority = identityUrl.ToString(), - ApiName = "marketing", - RequireHttpsMetadata = false - }); - } - private void RegisterEventBus(IServiceCollection services) { if (Configuration.GetValue("AzureServiceBusEnabled"))