Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 33 additions & 5 deletions EstateReportingAPI.BusinessLogic/QueryTimingInterceptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Shared.EntityFramework;
using Shared.General;
using Shared.Logger;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Diagnostics;
using System.Text;
using Shared.General;
using TransactionProcessor.Database.Contexts;

namespace EstateReportingAPI.BusinessLogic;

Expand Down Expand Up @@ -83,10 +84,37 @@ public ResolvedDbContext<TContext> Resolve(String connectionStringKey,

// Create an isolated service collection and provider
ServiceCollection services = new();
services.AddDbContext<TContext>(options => {
options.UseSqlServer(connectionString, sqlOptions => sqlOptions.EnableRetryOnFailure());
options.AddInterceptors(Interceptor); // attach here
});

SqlServerRetryOptions retryOptions;
try
{
retryOptions = ConfigurationReader.GetSection<SqlServerRetryOptions>("AppSettings:SqlServerRetry");
}
catch (KeyNotFoundException)
{
retryOptions = null;
}

if (retryOptions != null)
{
services.AddDbContext<EstateManagementContext>(options => {
options.UseSharedSqlServer<EstateManagementContext>(ConfigurationReader.GetConnectionString("TransactionProcessorReadModel"), retry => {
retry.AdditionalTransientErrorNumbers = retryOptions.AdditionalTransientErrorNumbers;
retry.MaxRetryCount = retryOptions.MaxRetryCount;
retry.MaxRetryDelay = retryOptions.MaxRetryDelay;
});
options.AddInterceptors(Interceptor);
});
}
else
{
services.AddDbContext<EstateManagementContext>(options => {
options.UseSqlServer(ConfigurationReader.GetConnectionString("TransactionProcessorReadModel"), retry => { retry.EnableRetryOnFailure(); });
options.AddInterceptors(Interceptor);
});

}


ServiceProvider provider = services.BuildServiceProvider();
scope = provider.CreateScope();
Expand Down
40 changes: 28 additions & 12 deletions EstateReportingAPI/Bootstrapper/RepositoryRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,47 @@ namespace EstateReportingAPI.Bootstrapper;
using Lamar;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.Extensions.Options;
using Shared.EntityFramework;
using Shared.General;
using System.Diagnostics.CodeAnalysis;

[ExcludeFromCodeCoverage]
public class RepositoryRegistry : ServiceRegistry{
public RepositoryRegistry(){
public RepositoryRegistry() {
String? inTestMode = Environment.GetEnvironmentVariable("InTestMode");
if (String.Compare(inTestMode, Boolean.TrueString, StringComparison.InvariantCultureIgnoreCase) != 0)
{
if (String.Compare(inTestMode, Boolean.TrueString, StringComparison.InvariantCultureIgnoreCase) != 0) {
this.AddSingleton<IReportingManager, ReportingManager>();
}

this.AddSingleton<DbCommandInterceptor, QueryTimingInterceptor>();
this.AddSingleton(typeof(IDbContextResolver<>), typeof(DbContextResolverX<>));
if (Startup.WebHostEnvironment.IsEnvironment("IntegrationTest") || Startup.Configuration.GetValue<Boolean>("ServiceOptions:UseInMemoryDatabase") == true)
{
this.AddDbContext<EstateManagementContext>(builder => builder.UseInMemoryDatabase("TransactionProcessorReadModel"));

if (Startup.WebHostEnvironment.IsEnvironment("IntegrationTest") || Startup.Configuration.GetValue<Boolean>("ServiceOptions:UseInMemoryDatabase") == true) {
this.AddDbContext<EstateManagementContext>(builder => { builder.UseInMemoryDatabase("TransactionProcessorReadModel"); });
}
else {
this.AddSingleton<QueryTimingInterceptor>();
this.AddDbContext<EstateManagementContext>((sp, options) =>
{
options.UseSqlServer(ConfigurationReader.GetConnectionString("TransactionProcessorReadModel"));
options.AddInterceptors(sp.GetRequiredService<QueryTimingInterceptor>());
});
SqlServerRetryOptions retryOptions;
try {
retryOptions = ConfigurationReader.GetSection<SqlServerRetryOptions>("AppSettings:SqlServerRetry");
}
catch (KeyNotFoundException) {
retryOptions = null;
}

if (retryOptions != null) {
this.AddDbContext<EstateManagementContext>(options => {
options.UseSharedSqlServer<EstateManagementContext>(ConfigurationReader.GetConnectionString("TransactionProcessorReadModel"), retry => {
retry.AdditionalTransientErrorNumbers = retryOptions.AdditionalTransientErrorNumbers;
retry.MaxRetryCount = retryOptions.MaxRetryCount;
retry.MaxRetryDelay = retryOptions.MaxRetryDelay;
Comment thread
StuartFerguson marked this conversation as resolved.
Resolved
});
});
}
else {
this.AddDbContext<EstateManagementContext>(options => { options.UseSqlServer(ConfigurationReader.GetConnectionString("TransactionProcessorReadModel"), retry => { retry.EnableRetryOnFailure(); }); });

}
}
}
}
Loading