|
|
@ -5,7 +5,6 @@ using Basket.API.Infrastructure.Middlewares; |
|
|
|
using Basket.API.IntegrationEvents.EventHandling; |
|
|
|
using Basket.API.IntegrationEvents.Events; |
|
|
|
using HealthChecks.UI.Client; |
|
|
|
|
|
|
|
using Microsoft.ApplicationInsights.Extensibility; |
|
|
|
using Microsoft.ApplicationInsights.ServiceFabric; |
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer; |
|
|
@ -58,7 +57,6 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API |
|
|
|
{ |
|
|
|
options.Filters.Add(typeof(HttpGlobalExceptionFilter)); |
|
|
|
options.Filters.Add(typeof(ValidateModelStateFilter)); |
|
|
|
|
|
|
|
}) |
|
|
|
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2) |
|
|
|
.AddControllersAsServices(); |
|
|
@ -85,69 +83,15 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API |
|
|
|
return ConnectionMultiplexer.Connect(configuration); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* if (Configuration.GetValue<bool>("AzureServiceBusEnabled")) |
|
|
|
{ |
|
|
|
services.AddSingleton<IServiceBusPersisterConnection>(sp => |
|
|
|
{ |
|
|
|
var logger = sp.GetRequiredService<ILogger<DefaultServiceBusPersisterConnection>>(); |
|
|
|
|
|
|
|
var serviceBusConnectionString = Configuration["EventBusConnection"]; |
|
|
|
var serviceBusConnection = new ServiceBusConnectionStringBuilder(serviceBusConnectionString); |
|
|
|
|
|
|
|
return new DefaultServiceBusPersisterConnection(serviceBusConnection, logger); |
|
|
|
}); |
|
|
|
}*/ |
|
|
|
/* else |
|
|
|
services.AddSingleton<IMultiRabbitMQPersistentConnections>(sp => |
|
|
|
{ |
|
|
|
services.AddSingleton<IRabbitMQPersistentConnection>(sp => |
|
|
|
{ |
|
|
|
var logger = sp.GetRequiredService<ILogger<DefaultRabbitMQPersistentConnection>>(); |
|
|
|
|
|
|
|
var factory = new ConnectionFactory() |
|
|
|
{ |
|
|
|
HostName = Configuration["EventBusConnection"], |
|
|
|
DispatchConsumersAsync = true |
|
|
|
}; |
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(Configuration["EventBusUserName"])) |
|
|
|
{ |
|
|
|
factory.UserName = Configuration["EventBusUserName"]; |
|
|
|
} |
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(Configuration["EventBusPassword"])) |
|
|
|
{ |
|
|
|
factory.Password = Configuration["EventBusPassword"]; |
|
|
|
} |
|
|
|
|
|
|
|
factory.VirtualHost = "TenantA"; |
|
|
|
|
|
|
|
var retryCount = 5; |
|
|
|
if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) |
|
|
|
{ |
|
|
|
retryCount = int.Parse(Configuration["EventBusRetryCount"]); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount); |
|
|
|
|
|
|
|
});*/ |
|
|
|
|
|
|
|
services.AddSingleton<IMultiRabbitMQPersistentConnections>(sp => |
|
|
|
{ |
|
|
|
IMultiRabbitMQPersistentConnections connections = new MultiRabbitMQPersistentConnections(); |
|
|
|
connections.AddConnection(GenerateConnection("TenantA", sp)); |
|
|
|
connections.AddConnection(GenerateConnection("TenantB", sp)); |
|
|
|
connections.AddConnection(GenerateConnection("/", sp)); |
|
|
|
|
|
|
|
return connections; |
|
|
|
}); |
|
|
|
IMultiRabbitMQPersistentConnections connections = new MultiRabbitMQPersistentConnections(); |
|
|
|
connections.AddConnection(GenerateConnection("TenantA", sp)); |
|
|
|
connections.AddConnection(GenerateConnection("TenantB", sp)); |
|
|
|
|
|
|
|
return connections; |
|
|
|
}); |
|
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
RegisterEventBus(services); |
|
|
|
|
|
|
@ -170,7 +114,7 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API |
|
|
|
TokenUrl = $"{Configuration.GetValue<string>("IdentityUrlExternal")}/connect/token", |
|
|
|
Scopes = new Dictionary<string, string>() |
|
|
|
{ |
|
|
|
{ "basket", "Basket API" } |
|
|
|
{"basket", "Basket API"} |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
@ -181,10 +125,10 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API |
|
|
|
{ |
|
|
|
options.AddPolicy("CorsPolicy", |
|
|
|
builder => builder |
|
|
|
.SetIsOriginAllowed((host) => true) |
|
|
|
.AllowAnyMethod() |
|
|
|
.AllowAnyHeader() |
|
|
|
.AllowCredentials()); |
|
|
|
.SetIsOriginAllowed((host) => true) |
|
|
|
.AllowAnyMethod() |
|
|
|
.AllowAnyHeader() |
|
|
|
.AllowCredentials()); |
|
|
|
}); |
|
|
|
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); |
|
|
|
services.AddTransient<IBasketRepository, RedisBasketRepository>(); |
|
|
@ -226,9 +170,8 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API |
|
|
|
{ |
|
|
|
retryCount = int.Parse(Configuration["EventBusRetryCount"]); |
|
|
|
} |
|
|
|
|
|
|
|
return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount); |
|
|
|
|
|
|
|
return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount); |
|
|
|
} |
|
|
|
|
|
|
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
|
|
@ -254,7 +197,7 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API |
|
|
|
Predicate = r => r.Name.Contains("self") |
|
|
|
}); |
|
|
|
|
|
|
|
app.UseStaticFiles(); |
|
|
|
app.UseStaticFiles(); |
|
|
|
app.UseCors("CorsPolicy"); |
|
|
|
|
|
|
|
ConfigureAuth(app); |
|
|
@ -262,27 +205,29 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API |
|
|
|
app.UseMvcWithDefaultRoute(); |
|
|
|
|
|
|
|
app.UseSwagger() |
|
|
|
.UseSwaggerUI(c => |
|
|
|
{ |
|
|
|
c.SwaggerEndpoint($"{ (!string.IsNullOrEmpty(pathBase) ? pathBase : string.Empty) }/swagger/v1/swagger.json", "Basket.API V1"); |
|
|
|
c.OAuthClientId ("basketswaggerui"); |
|
|
|
c.OAuthAppName("Basket Swagger UI"); |
|
|
|
}); |
|
|
|
.UseSwaggerUI(c => |
|
|
|
{ |
|
|
|
c.SwaggerEndpoint( |
|
|
|
$"{(!string.IsNullOrEmpty(pathBase) ? pathBase : string.Empty)}/swagger/v1/swagger.json", |
|
|
|
"Basket.API V1"); |
|
|
|
c.OAuthClientId("basketswaggerui"); |
|
|
|
c.OAuthAppName("Basket Swagger UI"); |
|
|
|
}); |
|
|
|
|
|
|
|
ConfigureEventBus(app); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
private void RegisterAppInsights(IServiceCollection services) |
|
|
|
{ |
|
|
|
services.AddApplicationInsightsTelemetry(Configuration); |
|
|
|
var orchestratorType = Configuration.GetValue<string>("OrchestratorType"); |
|
|
|
|
|
|
|
|
|
|
|
if (orchestratorType?.ToUpper() == "K8S") |
|
|
|
{ |
|
|
|
// Enable K8s telemetry initializer
|
|
|
|
services.AddApplicationInsightsKubernetesEnricher(); |
|
|
|
} |
|
|
|
|
|
|
|
if (orchestratorType?.ToUpper() == "SF") |
|
|
|
{ |
|
|
|
// Enable SF telemetry initializer
|
|
|
@ -296,13 +241,12 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API |
|
|
|
// prevent from mapping "sub" claim to nameidentifier.
|
|
|
|
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear(); |
|
|
|
|
|
|
|
var identityUrl = Configuration.GetValue<string>("IdentityUrl"); |
|
|
|
|
|
|
|
var identityUrl = Configuration.GetValue<string>("IdentityUrl"); |
|
|
|
|
|
|
|
services.AddAuthentication(options => |
|
|
|
{ |
|
|
|
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; |
|
|
|
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; |
|
|
|
|
|
|
|
}).AddJwtBearer(options => |
|
|
|
{ |
|
|
|
options.Authority = identityUrl; |
|
|
@ -325,76 +269,31 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API |
|
|
|
{ |
|
|
|
var subscriptionClientName = Configuration["SubscriptionClientName"]; |
|
|
|
|
|
|
|
/*if (Configuration.GetValue<bool>("AzureServiceBusEnabled")) |
|
|
|
services.AddSingleton<IMultiEventBus, MultiEventBusRabbitMQ>(sp => |
|
|
|
{ |
|
|
|
services.AddSingleton<IEventBus, EventBusServiceBus>(sp => |
|
|
|
{ |
|
|
|
var serviceBusPersisterConnection = sp.GetRequiredService<IServiceBusPersisterConnection>(); |
|
|
|
var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>(); |
|
|
|
var logger = sp.GetRequiredService<ILogger<EventBusServiceBus>>(); |
|
|
|
var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>(); |
|
|
|
|
|
|
|
return new EventBusServiceBus(serviceBusPersisterConnection, logger, |
|
|
|
eventBusSubcriptionsManager, subscriptionClientName, iLifetimeScope); |
|
|
|
}); |
|
|
|
} |
|
|
|
else |
|
|
|
{*/ |
|
|
|
|
|
|
|
services.AddSingleton<IMultiEventBus, MultiEventBusRabbitMQ>(sp => |
|
|
|
{ |
|
|
|
var multiRabbitMqPersistentConnections = sp.GetRequiredService<IMultiRabbitMQPersistentConnections>(); |
|
|
|
var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>(); |
|
|
|
var logger = sp.GetRequiredService<ILogger<EventBusRabbitMQ>>(); |
|
|
|
var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>(); |
|
|
|
var multiRabbitMqPersistentConnections = sp.GetRequiredService<IMultiRabbitMQPersistentConnections>(); |
|
|
|
var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>(); |
|
|
|
var logger = sp.GetRequiredService<ILogger<EventBusRabbitMQ>>(); |
|
|
|
var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>(); |
|
|
|
|
|
|
|
var retryCount = 5; |
|
|
|
if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) |
|
|
|
{ |
|
|
|
retryCount = int.Parse(Configuration["EventBusRetryCount"]); |
|
|
|
} |
|
|
|
List<IEventBus> eventBuses = new List<IEventBus>(); |
|
|
|
|
|
|
|
eventBuses.Add(new EventBusRabbitMQ(multiRabbitMqPersistentConnections.GetConnections()[0], logger, iLifetimeScope, eventBusSubcriptionsManager, "TenantA", subscriptionClientName, retryCount)); |
|
|
|
eventBuses.Add(new EventBusRabbitMQ(multiRabbitMqPersistentConnections.GetConnections()[1], logger, iLifetimeScope, eventBusSubcriptionsManager, "TenantB", subscriptionClientName, retryCount)); |
|
|
|
|
|
|
|
|
|
|
|
/*multiRabbitMqPersistentConnections.GetConnections().ForEach(conn => |
|
|
|
{ |
|
|
|
eventBuses.Add(new EventBusRabbitMQ(conn, logger, iLifetimeScope, eventBusSubcriptionsManager, subscriptionClientName, retryCount)); |
|
|
|
});*/ |
|
|
|
|
|
|
|
return new MultiEventBusRabbitMQ(eventBuses); |
|
|
|
}); |
|
|
|
|
|
|
|
/* services.AddSingleton<IEventBus, EventBusRabbitMQ>(sp => |
|
|
|
var retryCount = 5; |
|
|
|
if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) |
|
|
|
{ |
|
|
|
var rabbitMQPersistentConnection = sp.GetRequiredService<IRabbitMQPersistentConnection>(); |
|
|
|
var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>(); |
|
|
|
var logger = sp.GetRequiredService<ILogger<EventBusRabbitMQ>>(); |
|
|
|
var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>(); |
|
|
|
retryCount = int.Parse(Configuration["EventBusRetryCount"]); |
|
|
|
} |
|
|
|
|
|
|
|
var retryCount = 5; |
|
|
|
if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) |
|
|
|
{ |
|
|
|
retryCount = int.Parse(Configuration["EventBusRetryCount"]); |
|
|
|
} |
|
|
|
List<IEventBus> eventBuses = new List<IEventBus>(); |
|
|
|
|
|
|
|
eventBuses.Add(new EventBusRabbitMQ(multiRabbitMqPersistentConnections.GetConnections()[0], logger, |
|
|
|
iLifetimeScope, eventBusSubcriptionsManager, "TenantA", subscriptionClientName, retryCount)); |
|
|
|
eventBuses.Add(new EventBusRabbitMQ(multiRabbitMqPersistentConnections.GetConnections()[1], logger, |
|
|
|
iLifetimeScope, eventBusSubcriptionsManager, "TenantB", subscriptionClientName, retryCount)); |
|
|
|
Dictionary<int, String> tenants = new Dictionary<int, string>(); |
|
|
|
tenants.Add(1, "TenantA"); |
|
|
|
tenants.Add(2, "TenantB"); |
|
|
|
|
|
|
|
var multiRabbitMqPersistentConnections = sp.GetRequiredService<IMultiRabbitMQPersistentConnections>(); |
|
|
|
List<IEventBus> testing = new List<IEventBus>(); |
|
|
|
|
|
|
|
multiRabbitMqPersistentConnections.GetConnections().ForEach(conn => |
|
|
|
{ |
|
|
|
testing.Add(new EventBusRabbitMQ(conn, logger, iLifetimeScope, eventBusSubcriptionsManager, subscriptionClientName, retryCount)); |
|
|
|
}); |
|
|
|
|
|
|
|
Console.WriteLine(testing); |
|
|
|
|
|
|
|
return new EventBusRabbitMQ(rabbitMQPersistentConnection, logger, iLifetimeScope, eventBusSubcriptionsManager, subscriptionClientName, retryCount); |
|
|
|
});*/ |
|
|
|
|
|
|
|
//}
|
|
|
|
return new MultiEventBusRabbitMQ(eventBuses, tenants); |
|
|
|
}); |
|
|
|
|
|
|
|
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>(); |
|
|
|
|
|
|
@ -408,22 +307,23 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API |
|
|
|
|
|
|
|
eventBus.Subscribe<ProductPriceChangedIntegrationEvent, ProductPriceChangedIntegrationEventHandler>(); |
|
|
|
eventBus.Subscribe<OrderStartedIntegrationEvent, OrderStartedIntegrationEventHandler>(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public static class CustomExtensionMethods |
|
|
|
{ |
|
|
|
public static IServiceCollection AddCustomHealthCheck(this IServiceCollection services, IConfiguration configuration) |
|
|
|
public static IServiceCollection AddCustomHealthCheck(this IServiceCollection services, |
|
|
|
IConfiguration configuration) |
|
|
|
{ |
|
|
|
var hcBuilder = services.AddHealthChecks(); |
|
|
|
|
|
|
|
hcBuilder.AddCheck("self", () => HealthCheckResult.Healthy()); |
|
|
|
|
|
|
|
hcBuilder |
|
|
|
hcBuilder |
|
|
|
.AddRedis( |
|
|
|
configuration["ConnectionString"], |
|
|
|
name: "redis-check", |
|
|
|
tags: new string[] { "redis" }); |
|
|
|
tags: new string[] {"redis"}); |
|
|
|
|
|
|
|
if (configuration.GetValue<bool>("AzureServiceBusEnabled")) |
|
|
|
{ |
|
|
@ -432,7 +332,7 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API |
|
|
|
configuration["EventBusConnection"], |
|
|
|
topicName: "eshop_event_bus", |
|
|
|
name: "basket-servicebus-check", |
|
|
|
tags: new string[] { "servicebus" }); |
|
|
|
tags: new string[] {"servicebus"}); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
@ -440,10 +340,10 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API |
|
|
|
.AddRabbitMQ( |
|
|
|
$"amqp://{configuration["EventBusConnection"]}", |
|
|
|
name: "basket-rabbitmqbus-check", |
|
|
|
tags: new string[] { "rabbitmqbus" }); |
|
|
|
tags: new string[] {"rabbitmqbus"}); |
|
|
|
} |
|
|
|
|
|
|
|
return services; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |