Browse Source

partial checkin

features/migration-dotnet3
ericuss 5 years ago
parent
commit
aedd0e38e9
6 changed files with 81 additions and 40 deletions
  1. +8
    -1
      src/BuildingBlocks/WebHostCustomization/WebHost.Customization/WebHostExtensions.cs
  2. +13
    -2
      src/Services/Marketing/Marketing.API/Infrastructure/MarketingContext.cs
  3. +45
    -33
      src/Services/Marketing/Marketing.API/Startup.cs
  4. +1
    -2
      src/Services/Marketing/Marketing.FunctionalTests/Marketing.FunctionalTests.csproj
  5. +12
    -1
      src/Services/Marketing/Marketing.FunctionalTests/MarketingTestStartup.cs
  6. +2
    -1
      src/Services/Marketing/Marketing.FunctionalTests/appsettings.json

+ 8
- 1
src/BuildingBlocks/WebHostCustomization/WebHost.Customization/WebHostExtensions.cs View File

@ -39,8 +39,15 @@ namespace Microsoft.AspNetCore.Hosting
}
else
{
var retries = 10;
var retry = Policy.Handle<SqlException>()
.WaitAndRetry(10, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));
.WaitAndRetry(
retryCount: retries,
sleepDurationProvider: retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
onRetry: (exception, timeSpan, retry, ctx) =>
{
logger.LogWarning(exception, "[{prefix}] Exception {ExceptionType} with message {Message} detected on attempt {retry} of {retries}", nameof(TContext), exception.GetType().Name, exception.Message, retry, retries);
});
//if the sql server container is not created on run docker compose this
//migration can't fail for network related exception. The retry options for DbContext only


+ 13
- 2
src/Services/Marketing/Marketing.API/Infrastructure/MarketingContext.cs View File

@ -1,9 +1,12 @@
namespace Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure
{
using System;
using System.IO;
using EntityConfigurations;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.eShopOnContainers.Services.Marketing.API.Model;
using Microsoft.Extensions.Configuration;
public class MarketingContext : DbContext
{
@ -27,9 +30,17 @@
{
public MarketingContext CreateDbContext(string[] args)
{
IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
var connectionString = configuration["ConnectionString"];
Console.WriteLine(" -- Connection string");
Console.WriteLine(connectionString);
var optionsBuilder = new DbContextOptionsBuilder<MarketingContext>()
.UseSqlServer("Server=.;Initial Catalog=Microsoft.eShopOnContainers.Services.MarketingDb;Integrated Security=true");
.UseSqlServer(connectionString);
// .UseSqlServer("Server=.;Initial Catalog=Microsoft.eShopOnContainers.Services.MarketingDb;Integrated Security=true");
return new MarketingContext(optionsBuilder.Options);
}
}

+ 45
- 33
src/Services/Marketing/Marketing.API/Startup.cs View File

@ -23,7 +23,9 @@
using Marketing.API.IntegrationEvents.Handlers;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.eShopOnContainers.Services.Marketing.API.Controllers;
using Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure.Middlewares;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.OpenApi.Models;
@ -44,13 +46,18 @@
// This method gets called by the runtime. Use this method to add services to the container.
public IServiceProvider ConfigureServices(IServiceCollection services)
public virtual IServiceProvider ConfigureServices(IServiceCollection services)
{
RegisterAppInsights(services);
// Add framework services.
services.AddCustomHealthCheck(Configuration);
services.AddControllers().AddNewtonsoftJson();
services
.AddControllers()
// Added for functional tests
.AddApplicationPart(typeof(LocationsController).Assembly)
.AddNewtonsoftJson()
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
services.Configure<MarketingSettings>(Configuration);
ConfigureAuthService(services);
@ -117,36 +124,8 @@
}
// Add framework services.
services.AddSwaggerGen(options =>
{
options.DescribeAllEnumsAsStrings();
options.SwaggerDoc("v1", new OpenApiInfo
{
Title = "eShopOnContainers - Marketing HTTP API",
Version = "v1",
Description = "The Marketing Service HTTP API"
});
options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
{
Type = SecuritySchemeType.OAuth2,
Flows = new OpenApiOAuthFlows()
{
Implicit = new OpenApiOAuthFlow()
{
AuthorizationUrl = new Uri($"{Configuration.GetValue<string>("IdentityUrlExternal")}/connect/authorize"),
TokenUrl = new Uri($"{Configuration.GetValue<string>("IdentityUrlExternal")}/connect/token"),
Scopes = new Dictionary<string, string>()
{
{ "marketing", "Marketing API" }
}
}
}
});
options.OperationFilter<AuthorizeCheckOperationFilter>();
});
AddCustomSwagger(services);
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy",
@ -186,10 +165,10 @@
}
app.UseCors("CorsPolicy");
app.UseRouting();
ConfigureAuth(app);
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapDefaultControllerRoute();
@ -216,6 +195,39 @@
ConfigureEventBus(app);
}
private void AddCustomSwagger(IServiceCollection services)
{
services.AddSwaggerGen(options =>
{
options.DescribeAllEnumsAsStrings();
options.SwaggerDoc("v1", new OpenApiInfo
{
Title = "eShopOnContainers - Marketing HTTP API",
Version = "v1",
Description = "The Marketing Service HTTP API"
});
options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
{
Type = SecuritySchemeType.OAuth2,
Flows = new OpenApiOAuthFlows()
{
Implicit = new OpenApiOAuthFlow()
{
AuthorizationUrl = new Uri($"{Configuration.GetValue<string>("IdentityUrlExternal")}/connect/authorize"),
TokenUrl = new Uri($"{Configuration.GetValue<string>("IdentityUrlExternal")}/connect/token"),
Scopes = new Dictionary<string, string>()
{
{ "marketing", "Marketing API" }
}
}
}
});
options.OperationFilter<AuthorizeCheckOperationFilter>();
});
}
private void RegisterAppInsights(IServiceCollection services)
{
services.AddApplicationInsightsTelemetry(Configuration);
@ -225,7 +237,7 @@
private void ConfigureAuthService(IServiceCollection services)
{
// prevent from mapping "sub" claim to nameidentifier.
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Remove("sub");
services.AddAuthentication(options =>
{


+ 1
- 2
src/Services/Marketing/Marketing.FunctionalTests/Marketing.FunctionalTests.csproj View File

@ -17,8 +17,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="$(Microsoft_AspNetCore_Mvc_Testing)" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(Microsoft_NET_Test_Sdk)" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="$(Microsoft_AspNetCore_TestHost)" />
<PackageReference Include="xunit" Version="$(xunit)" />


+ 12
- 1
src/Services/Marketing/Marketing.FunctionalTests/MarketingTestStartup.cs View File

@ -1,6 +1,9 @@
using Microsoft.AspNetCore.Builder;
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing;
using Microsoft.eShopOnContainers.Services.Marketing.API;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace Marketing.FunctionalTests
{
@ -10,6 +13,14 @@ namespace Marketing.FunctionalTests
{
}
public override IServiceProvider ConfigureServices(IServiceCollection services)
{
// Added to avoid the Authorize data annotation in test environment.
// Property "SuppressCheckForUnhandledSecurityMetadata" in appsettings.json
services.Configure<RouteOptions>(Configuration);
return base.ConfigureServices(services);
}
protected override void ConfigureAuth(IApplicationBuilder app)
{
if (Configuration["isTest"] == bool.TrueString.ToLowerInvariant())


+ 2
- 1
src/Services/Marketing/Marketing.FunctionalTests/appsettings.json View File

@ -6,5 +6,6 @@
"isTest": "true",
"EventBusConnection": "localhost",
"PicBaseUrl": "http://localhost:5110/api/v1/campaigns/[0]/pic/",
"SubscriptionClientName": "Marketing"
"SubscriptionClientName": "Marketing",
"SuppressCheckForUnhandledSecurityMetadata":true
}

Loading…
Cancel
Save