public async Task FailSubtaskInProgressAsync_Should_MarkSubtaskInProgressAsError() { var dbContextOptions = DbContextOptionsFactory.CreateOptions("Mark_subtask_as_error"); using (var dbContext = new TestDbContext(dbContextOptions)) { CreateMockData(dbContext); await dbContext.SaveChangesAsync(); } var subtaskInProgressCleanupServiceMock = new Mock <ISubtasksInProgressCleanupService>(); subtaskInProgressCleanupServiceMock.Setup(service => service.RemoveSubtasksInProgress(It.IsAny <int>())); using (var dbContext = new TestDbContext(dbContextOptions)) { var computationFailService = new ComputationFailService(dbContext, subtaskInProgressCleanupServiceMock.Object); await computationFailService.FailSubtaskInProgressAsync(1, new[] { "Some error" }); } using (var dbContext = new TestDbContext(dbContextOptions)) { var foundSubtaskInProgress = await dbContext.SubtasksInProgress .Include(subtaskInProgress => subtaskInProgress.Subtask) .FirstAsync(subtaskInProgress => subtaskInProgress.Id == 1); Assert.AreEqual(SubtaskInProgressStatus.Error, foundSubtaskInProgress.Status); Assert.AreEqual(SubtaskStatus.Executing, foundSubtaskInProgress.Subtask.Status); } }
public async Task GetNextSubtaskAsync_Should_ReturnASubtask_BasedOnDistributedTaskPriority() { var dbContextOptions = DbContextOptionsFactory.CreateOptions("Return_next_subtask_base_on_priority"); using (var dbContext = new TestDbContext(dbContextOptions)) { SeedDistributedTask(dbContext, 1); dbContext.Subtasks.Add(new Subtask() { Id = 1, DistributedTaskId = 1, Status = SubtaskStatus.WaitingForExecution }); SeedDistributedTask(dbContext, 2, priority: 5); dbContext.Subtasks.Add(new Subtask() { Id = 2, DistributedTaskId = 2, Status = SubtaskStatus.WaitingForExecution }); await dbContext.SaveChangesAsync(); } using (var dbContext = new TestDbContext(dbContextOptions)) { var getNextSubtaskToComputeService = new GetNextSubtaskToComputeService(dbContext); var subtask = await getNextSubtaskToComputeService.GetNextSubtaskAsync(); Assert.AreEqual(2, subtask.Id); } }
public async Task GetNextSubtaskAsync_ShouldNot_ReturnSubtask_When_DistributedTaskIsNotInProgress() { var dbContextOptions = DbContextOptionsFactory.CreateOptions("Return_next_subtask_when_distributed_task_not_in_progress"); using (var dbContext = new TestDbContext(dbContextOptions)) { dbContext.DistributedTasks.Add(new DistributedTask() { Id = 1, Status = DistributedTaskStatus.Error, }); dbContext.Subtasks.Add(new Subtask() { Id = 1, DistributedTaskId = 1, Status = SubtaskStatus.Done }); await dbContext.SaveChangesAsync(); } using (var dbContext = new TestDbContext(dbContextOptions)) { var getNextSubtaskToComputeService = new GetNextSubtaskToComputeService(dbContext); var subtask = await getNextSubtaskToComputeService.GetNextSubtaskAsync(); Assert.Null(subtask); } }
private void SaveToDb <T>(List <T> collectionToInsert) where T : class { WalletDbContext context = null; try { context = new WalletDbContext(DbContextOptionsFactory.DbContextOptions()); context.ChangeTracker.AutoDetectChangesEnabled = false; int count = 0; foreach (var entity in collectionToInsert) { ++count; context = AddToContext(context, entity, count, 100, true); } context.SaveChanges(); } finally { if (context != null) { context.Dispose(); } } }
public async Task GetNextSubtaskAsync_ShouldNot_ReturnSubtask_When_AllAreDone() { var dbContextOptions = DbContextOptionsFactory.CreateOptions("Return_next_subtask_when_all_are_done"); using (var dbContext = new TestDbContext(dbContextOptions)) { SeedDistributedTask(dbContext, 1); dbContext.Subtasks.Add(new Subtask() { Id = 1, DistributedTaskId = 1, Status = SubtaskStatus.Done }); dbContext.Subtasks.Add(new Subtask() { Id = 2, DistributedTaskId = 1, Status = SubtaskStatus.Done }); await dbContext.SaveChangesAsync(); } using (var dbContext = new TestDbContext(dbContextOptions)) { var getNextSubtaskToComputeService = new GetNextSubtaskToComputeService(dbContext); var subtask = await getNextSubtaskToComputeService.GetNextSubtaskAsync(); Assert.Null(subtask); } }
private DbContext GetDoWithYouContext() { var factory = new DbContextOptionsFactory <DoWithYouContext>(); string connectionString = GetConnectionString(""); // TODO: Add connection string return(new DoWithYouContext(factory.GetOptions(connectionString))); }
public async Task CancelSubtaskInProgressAsync_Should_SetSubtaskInProgressStatusToCancelled() { var dbContextOptions = DbContextOptionsFactory.CreateOptions("Cancel_subtask_in_progress"); using (var dbContext = new TestDbContext(dbContextOptions)) { await dbContext.SubtasksInProgress.AddAsync(new SubtaskInProgress() { Id = 1, Status = SubtaskInProgressStatus.Executing }); await dbContext.SaveChangesAsync(); } using (var dbContext = new TestDbContext(dbContextOptions)) { var computationCancelService = new ComputationCancelService(dbContext); await computationCancelService.CancelSubtaskInProgressAsync(1); } using (var dbContext = new TestDbContext(dbContextOptions)) { var subtaskInProgress = await dbContext.SubtasksInProgress.FindAsync(1); Assert.AreEqual(SubtaskInProgressStatus.Cancelled, subtaskInProgress.Status); } }
public async Task CleanAsync_ShouldNot_ModifySubtasks_When_NodeIsAlive() { var dbContextOptions = DbContextOptionsFactory.CreateOptions("Not_modify_subtasks_when_node_is_alive"); using (var dbContext = new TestDbContext(dbContextOptions)) { var distributedNodeId = Guid.NewGuid(); dbContext.DistributedNodes.Add(new DistributedNode() { Id = distributedNodeId, LastKeepAliveTime = DateTime.Now.Subtract(TimeSpan.FromMinutes(5)), SubtasksInProgress = new List <SubtaskInProgress>() { new SubtaskInProgress() { Id = 1, NodeId = distributedNodeId, Status = SubtaskInProgressStatus.Executing }, new SubtaskInProgress() { Id = 2, NodeId = distributedNodeId, Status = SubtaskInProgressStatus.Done }, new SubtaskInProgress() { Id = 3, NodeId = distributedNodeId, Status = SubtaskInProgressStatus.Executing } } }); await dbContext.SaveChangesAsync(); } var computationCancelServiceMock = new Mock <IComputationCancelService>(); computationCancelServiceMock.Setup(service => service.CancelSubtaskInProgressWithoutSavingAsync(It.IsAny <int>())) .Returns(Task.CompletedTask); using (var dbContext = new TestDbContext(dbContextOptions)) { var cleaner = new DistributedNodesCleaner(dbContext, computationCancelServiceMock.Object, TimeSpan.FromMinutes(60)); await cleaner.CleanAsync(); } computationCancelServiceMock.VerifyNoOtherCalls(); using (var dbContext = new TestDbContext(dbContextOptions)) { var distributedNodesCount = await dbContext.DistributedNodes.CountAsync(); Assert.AreEqual(1, distributedNodesCount); } }
protected override void Load(ContainerBuilder builder) { builder.RegisterType <BlBook>().As <IBlBook>().InstancePerLifetimeScope(); builder.RegisterType <EfBook>().As <IEfBook>(); builder.RegisterType <SampleContext>().AsSelf().WithParameter("options", DbContextOptionsFactory.Get()); }
public async Task CleanAsync_Should_RemoveNode_When_StaleAndNoDoneTasks() { var dbContextOptions = DbContextOptionsFactory.CreateOptions("Remove_node_when_stale_and_has_no_done_tasks"); using (var dbContext = new TestDbContext(dbContextOptions)) { var distributedNodeId = Guid.NewGuid(); dbContext.DistributedNodes.Add(new DistributedNode() { Id = distributedNodeId, LastKeepAliveTime = DateTime.Now.Subtract(TimeSpan.FromMinutes(5)), SubtasksInProgress = new List <SubtaskInProgress>() { new SubtaskInProgress() { Id = 1, NodeId = distributedNodeId, Status = SubtaskInProgressStatus.Executing }, new SubtaskInProgress() { Id = 2, NodeId = distributedNodeId, Status = SubtaskInProgressStatus.Error }, new SubtaskInProgress() { Id = 3, NodeId = distributedNodeId, Status = SubtaskInProgressStatus.Cancelled } } }); await dbContext.SaveChangesAsync(); } var computationCancelServiceMock = new Mock <IComputationCancelService>(); computationCancelServiceMock.Setup(service => service.CancelSubtaskInProgressWithoutSavingAsync(It.IsAny <int>())) .Returns(Task.CompletedTask); using (var dbContext = new TestDbContext(dbContextOptions)) { var cleaner = new DistributedNodesCleaner(dbContext, computationCancelServiceMock.Object, TimeSpan.FromSeconds(5)); await cleaner.CleanAsync(); } using (var dbContext = new TestDbContext(dbContextOptions)) { var distributedNodesCount = await dbContext.DistributedNodes.CountAsync(); Assert.AreEqual(0, distributedNodesCount); } }
internal static ContainerBuilder AddPostgresDataProvider(this ContainerBuilder builder) { builder.RegisterType <PaymentGatewayContext>() .WithParameter("options", DbContextOptionsFactory.Get()) .InstancePerLifetimeScope() .As <PaymentGatewayContext>(); return(builder); }
public async Task CompleteSubtaskInProgressAsync_Should_MarkDistributedTaskAsDone_When_NoOtherSubtasksLeft() { var dbContextOptions = DbContextOptionsFactory.CreateOptions("Mark_distributed_task_as_done"); using (var dbContext = new TestDbContext(dbContextOptions)) { await CreateMockData(dbContext); await dbContext.SaveChangesAsync(); } var problemPluginFacadeMock = new Mock <IProblemPluginFacade>(); problemPluginFacadeMock.Setup(facade => facade.JoinSubtaskResults(It.IsAny <IEnumerable <byte[]> >())) .Returns(new byte[] { 1, 2, 3 }); var problemPluginFacadeProviderMock = new Mock <IProblemPluginFacadeProvider>(); problemPluginFacadeProviderMock.Setup(provider => provider.Provide( It.Is <DistributedTaskDefinition>(taskDefinition => taskDefinition.Name == "Task definition"))) .Returns(() => problemPluginFacadeMock.Object); var subtaskInProgressCleanupServiceMock = new Mock <ISubtasksInProgressCleanupService>(); subtaskInProgressCleanupServiceMock.Setup(service => service.RemoveSubtasksInProgress(It.IsAny <int>())); using (var dbContext = new TestDbContext(dbContextOptions)) { var computationCompleteService = new ComputationCompleteService(dbContext, problemPluginFacadeProviderMock.Object, subtaskInProgressCleanupServiceMock.Object); using (var stream = new MemoryStream(new byte[] { 4, 5, 6 })) { await computationCompleteService.CompleteSubtaskInProgressAsync(1, stream); } } problemPluginFacadeProviderMock.Verify(provider => provider.Provide(It.IsAny <DistributedTaskDefinition>()), Times.Once); problemPluginFacadeMock.Verify(facade => facade.JoinSubtaskResults(It.IsAny <IEnumerable <byte[]> >()), Times.Once); using (var dbContext = new TestDbContext(dbContextOptions)) { var foundSubtask = await dbContext.Subtasks .Include(subtask => subtask.DistributedTask) .Include(subtask => subtask.SubtasksInProgress) .FirstAsync(subtask => subtask.Id == 1); Assert.AreEqual(SubtaskStatus.Done, foundSubtask.Status); Assert.AreEqual(DistributedTaskStatus.Done, foundSubtask.DistributedTask.Status); } }
public async Task CompleteSubtaskInProgressAsync_Should_MarkSubtaskAsDone_When_TrustLevelReached() { var dbContextOptions = DbContextOptionsFactory.CreateOptions("Mark_subtask_as_done"); using (var dbContext = new TestDbContext(dbContextOptions)) { await CreateMockData(dbContext); dbContext.Subtasks.Add(new Subtask() { DistributedTaskId = 1, Id = 2, SequenceNumber = 1, }); await dbContext.SaveChangesAsync(); } var problemPluginFacadeProviderMock = new Mock <IProblemPluginFacadeProvider>(); var subtaskInProgressCleanupServiceMock = new Mock <ISubtasksInProgressCleanupService>(); subtaskInProgressCleanupServiceMock.Setup(service => service.RemoveSubtasksInProgress(It.IsAny <int>())); using (var dbContext = new TestDbContext(dbContextOptions)) { if (subtaskInProgressCleanupServiceMock.Object != null) { var computationCompleteService = new ComputationCompleteService(dbContext, problemPluginFacadeProviderMock.Object, subtaskInProgressCleanupServiceMock.Object); using (var stream = new MemoryStream(new byte[] { 4, 5, 6 })) { await computationCompleteService.CompleteSubtaskInProgressAsync(1, stream); } } } subtaskInProgressCleanupServiceMock.Verify(service => service.RemoveSubtasksInProgress(1), Times.Once); using (var dbContext = new TestDbContext(dbContextOptions)) { var foundSubtaskInProgress = await dbContext.SubtasksInProgress .Include(subtaskInProgress => subtaskInProgress.Subtask) .ThenInclude(subtask => subtask.DistributedTask) .FirstAsync(subtaskInProgress => subtaskInProgress.Id == 1); Assert.AreEqual(SubtaskInProgressStatus.Done, foundSubtaskInProgress.Status); Assert.AreEqual(SubtaskStatus.Done, foundSubtaskInProgress.Subtask.Status); Assert.AreEqual(DistributedTaskStatus.InProgress, foundSubtaskInProgress.Subtask.DistributedTask.Status); } }
/// <summary> /// /// </summary> /// <param name="services"></param> /// <param name="builderAction"></param> /// <returns></returns> public static IServiceCollection AddScorpioDbContext <TDbContext>(this IServiceCollection services, Action <IScorpioDbContextOptionsBuilder <TDbContext> > builderAction) where TDbContext : ScorpioDbContext <TDbContext> { services.AddMemoryCache().AddLogging(); var options = new ScorpioDbContextOptionsBuilder <TDbContext>(services); builderAction?.Invoke(options); services.TryAddTransient(serviceProvider => DbContextOptionsFactory.Create(serviceProvider, options)); services.AddTransient <DbContextOptions>(p => p.GetRequiredService <DbContextOptions <TDbContext> >()); services.AddTransient <TDbContext>(); new EfCoreRepositoryRegistrar <TDbContext>(options).RegisterRepositories(); return(services); }
public async Task GetNextSubtaskAsync_ShouldNot_ReturnSubtask_When_SubtasksInProgressAreCancelled() { var dbContextOptions = DbContextOptionsFactory.CreateOptions("Return_next_subtask_when_subtasks_in_progress_are_cancelled"); using (var dbContext = new TestDbContext(dbContextOptions)) { SeedDistributedTask(dbContext, 1); dbContext.Subtasks.Add(new Subtask() { Id = 1, DistributedTaskId = 1, Status = SubtaskStatus.Executing, SubtasksInProgress = new List <SubtaskInProgress>() { new SubtaskInProgress() { Status = SubtaskInProgressStatus.Cancelled, Id = 1, Node = new DistributedNode() { TrustLevel = 5 } }, new SubtaskInProgress() { Status = SubtaskInProgressStatus.Cancelled, Id = 2, Node = new DistributedNode() { TrustLevel = 5 } } } }); await dbContext.SaveChangesAsync(); } using (var dbContext = new TestDbContext(dbContextOptions)) { var getNextSubtaskToComputeService = new GetNextSubtaskToComputeService(dbContext); var subtask = await getNextSubtaskToComputeService.GetNextSubtaskAsync(); Assert.NotNull(subtask); Assert.AreEqual(1, subtask.Id); } }
public async Task Write_Should_Save_Employee(Employee employee) { var options = DbContextOptionsFactory.Create(nameof(Write_Should_Save_Employee)); using (var context = new EmployeeManagerContext(options)) { var sut = new EmployeeWriter(context); await sut.Write(employee); } using (var context = new EmployeeManagerContext(options)) { var employeeStoraged = await context.Employees.FindAsync(employee.Id); employeeStoraged.Should().NotBeNull(); } }
private WalletDbContext AddToContext <T>(WalletDbContext context, T entity, int count, int commitCount, bool recreateContext) where T : class { context.Set <T>().Add(entity); if (count % commitCount == 0) { context.SaveChanges(); if (recreateContext) { context.Dispose(); context = new WalletDbContext(DbContextOptionsFactory.DbContextOptions()); context.ChangeTracker.AutoDetectChangesEnabled = false; } } return(context); }
public async Task CompleteSubtaskInProgressAsync_ShouldNot_MarkSubtaskAsDone_When_TrustLevelNotReached() { var dbContextOptions = DbContextOptionsFactory.CreateOptions("Not_mark_subtask_as_done"); using (var dbContext = new TestDbContext(dbContextOptions)) { await CreateMockData(dbContext); var distributedTask = await dbContext.DistributedTasks.FindAsync(1); distributedTask.TrustLevelToComplete = 5; await dbContext.SaveChangesAsync(); } var problemPluginFacadeProviderMock = new Mock <IProblemPluginFacadeProvider>(); var subtaskInProgressCleanupServiceMock = new Mock <ISubtasksInProgressCleanupService>(); using (var dbContext = new TestDbContext(dbContextOptions)) { var computationCompleteService = new ComputationCompleteService(dbContext, problemPluginFacadeProviderMock.Object, subtaskInProgressCleanupServiceMock.Object); using (var stream = new MemoryStream(new byte[] { 4, 5, 6 })) { await computationCompleteService.CompleteSubtaskInProgressAsync(1, stream); } } using (var dbContext = new TestDbContext(dbContextOptions)) { var foundSubtaskInProgress = await dbContext.SubtasksInProgress .Include(subtaskInProgress => subtaskInProgress.Subtask) .ThenInclude(subtask => subtask.DistributedTask) .FirstAsync(subtaskInProgress => subtaskInProgress.Id == 1); Assert.AreEqual(SubtaskInProgressStatus.Done, foundSubtaskInProgress.Status); Assert.AreEqual(SubtaskStatus.Executing, foundSubtaskInProgress.Subtask.Status); Assert.AreEqual(DistributedTaskStatus.InProgress, foundSubtaskInProgress.Subtask.DistributedTask.Status); } }
FailSubtaskInProgressAsync_Should_MarkDistributedTaskAsError_WhenMultipleSubtasksInProgressFailed() { var dbContextOptions = DbContextOptionsFactory.CreateOptions("Mark_distributed task_as_error"); using (var dbContext = new TestDbContext(dbContextOptions)) { CreateMockData(dbContext); dbContext.SubtasksInProgress.Add(new SubtaskInProgress() { Id = 2, SubtaskId = 1, Status = SubtaskInProgressStatus.Executing }); await dbContext.SaveChangesAsync(); } var subtaskInProgressCleanupServiceMock = new Mock <ISubtasksInProgressCleanupService>(); subtaskInProgressCleanupServiceMock.Setup(service => service.RemoveSubtasksInProgress(It.IsAny <int>())); using (var dbContext = new TestDbContext(dbContextOptions)) { var computationFailService = new ComputationFailService(dbContext, subtaskInProgressCleanupServiceMock.Object); await computationFailService.FailSubtaskInProgressAsync(2, new[] { "Some error" }); } subtaskInProgressCleanupServiceMock.Verify(service => service.RemoveSubtasksInProgress(1), Times.Once); using (var dbContext = new TestDbContext(dbContextOptions)) { var foundSubtaskInProgress = await dbContext.SubtasksInProgress .Include(subtaskInProgress => subtaskInProgress.Subtask) .ThenInclude(subtask => subtask.DistributedTask) .FirstAsync(subtaskInProgress => subtaskInProgress.Id == 1); Assert.AreEqual(SubtaskInProgressStatus.Error, foundSubtaskInProgress.Status); Assert.AreEqual(SubtaskStatus.Error, foundSubtaskInProgress.Subtask.Status); Assert.AreEqual(DistributedTaskStatus.Error, foundSubtaskInProgress.Subtask.DistributedTask.Status); } }
public async Task Remove_Should_Delete_Employee(Employee employee) { var options = DbContextOptionsFactory.Create(nameof(Remove_Should_Delete_Employee)); using (var context = new EmployeeManagerContext(options)) { context.Add(employee); await context.SaveChangesAsync(); } using (var context = new EmployeeManagerContext(options)) { var sut = new EmployeeWriter(context); await sut.Remove(employee); employee.DeletedAt.Should().NotBeNull(); } }
public async Task Read_When_EmployeeId_Exists_Should_Return_Employee(Employee employee) { var options = DbContextOptionsFactory.Create(nameof(Read_When_EmployeeId_Exists_Should_Return_Employee)); using (var context = new EmployeeManagerContext(options)) { context.Add(employee); await context.SaveChangesAsync(); } using (var context = new EmployeeManagerContext(options)) { var sut = new EmployeeReader(context); var actual = await sut.Read(employee.Id); actual.Should().NotBeNull(); actual.Value.Id.Should().Be(employee.Id); } }
public async Task Any_When_EmailAddress_Exists_Should_Return_True( Employee employee) { var options = DbContextOptionsFactory.Create(nameof(Any_When_EmailAddress_Exists_Should_Return_True)); using (var context = new EmployeeManagerContext(options)) { context.Add(employee); await context.SaveChangesAsync(); } using (var context = new EmployeeManagerContext(options)) { var sut = new EmployeeReader(context); var actual = await sut.Any(employee.Email.Address); actual.Should().BeTrue(); } }
public async Task Read_When_EmployeeId_Does_Not_Exist_Should_Return_Maybe_None( Employee employee, int someEmployeeId) { var options = DbContextOptionsFactory.Create(nameof(Read_When_EmployeeId_Does_Not_Exist_Should_Return_Maybe_None)); using (var context = new EmployeeManagerContext(options)) { context.Add(employee); await context.SaveChangesAsync(); } using (var context = new EmployeeManagerContext(options)) { var sut = new EmployeeReader(context); var actual = await sut.Read(someEmployeeId); actual.Should().Be(Maybe <Employee> .None); } }
public async Task Any_When_EmailAddress_Does_Not_Exist_Should_Return_False( Employee employee, string someEmployeeEmail) { var options = DbContextOptionsFactory.Create(nameof(Any_When_EmailAddress_Does_Not_Exist_Should_Return_False)); using (var context = new EmployeeManagerContext(options)) { context.Add(employee); await context.SaveChangesAsync(); } using (var context = new EmployeeManagerContext(options)) { var sut = new EmployeeReader(context); var actual = await sut.Any(someEmployeeEmail); actual.Should().BeFalse(); } }
public async Task Read_Should_Paginated_Employees(List <Employee> employees) { const int pageIndex = 1; const int pageSize = 1; var options = DbContextOptionsFactory.Create(nameof(Read_Should_Paginated_Employees)); using (var context = new EmployeeManagerContext(options)) { context.AddRange(employees); await context.SaveChangesAsync(); } using (var context = new EmployeeManagerContext(options)) { var sut = new EmployeeReader(context); var actual = await sut.Read(pageIndex, pageSize); actual.Count.Should().Be(pageSize); } }
public async Task GetNextSubtaskAsync_ShouldNot_ReturnSubtask_When_TrustLevelAlreadyReached() { var dbContextOptions = DbContextOptionsFactory.CreateOptions("Return_next_subtask_when_trust_level_already_reached"); using (var dbContext = new TestDbContext(dbContextOptions)) { SeedDistributedTask(dbContext, 1, trustLevelToComplete: 5); dbContext.Subtasks.Add(new Subtask() { Id = 1, DistributedTaskId = 1, Status = SubtaskStatus.Executing, SubtasksInProgress = new List <SubtaskInProgress>() { new SubtaskInProgress() { Status = SubtaskInProgressStatus.Executing, Id = 1, Node = new DistributedNode() { TrustLevel = 5 } } } }); await dbContext.SaveChangesAsync(); } using (var dbContext = new TestDbContext(dbContextOptions)) { var getNextSubtaskToComputeService = new GetNextSubtaskToComputeService(dbContext); var subtask = await getNextSubtaskToComputeService.GetNextSubtaskAsync(); Assert.Null(subtask); } }
public NonQueryDataService(DbContextOptionsFactory context) { _context = context; }
public void ConfigureContainer(ContainerBuilder builder) { builder.Register(ctx => new MapperConfiguration(cfg => cfg.AddProfile(typeof(AutoMapperProfile)))); builder.Register(ctx => ctx.Resolve <MapperConfiguration>().CreateMapper()).As <IMapper>(); builder.Register <ILogger>((c, p) => { return(new LoggerConfiguration() .ReadFrom.Configuration(Configuration) .Enrich.FromLogContext() .WriteTo.Console() .WriteTo.File($"/logs/log-.log", rollingInterval: RollingInterval.Day) .CreateLogger()); }).SingleInstance(); builder.Register(c => Configuration) .As <IConfigurationRoot>() .SingleInstance(); builder.RegisterType <AppSettings>() .SingleInstance(); builder.RegisterType <HttpContextAccessor>() .As <IHttpContextAccessor>() .SingleInstance(); builder.RegisterType <ActionContextAccessor>() .As <IActionContextAccessor>() .SingleInstance(); builder.RegisterType <UrlHelperFactory>() .AsImplementedInterfaces() .InstancePerLifetimeScope(); builder.RegisterType <ClientDbContext>() .WithParameter("options", DbContextOptionsFactory.GetClientDbContextOptions()) .WithParameter("config", Configuration) .InstancePerLifetimeScope(); builder.RegisterType <ManagementDbContext>() .WithParameter("options", DbContextOptionsFactory.GetManagementDbContextOptions()) .WithParameter("config", Configuration) .InstancePerLifetimeScope(); builder.RegisterType <UserContext>() .InstancePerLifetimeScope(); builder.RegisterType <EmailBuilder>() .PropertiesAutowired() .InstancePerLifetimeScope(); builder.RegisterType <EmailService>() .AsImplementedInterfaces() .PropertiesAutowired() .InstancePerLifetimeScope(); builder.RegisterAssemblyTypes(Assembly.GetAssembly(typeof(Clients.Repositories.BaseRepository))) .Where(t => t.IsSubclassOf(typeof(Clients.Repositories.BaseRepository))) .PropertiesAutowired() .AsImplementedInterfaces() .InstancePerLifetimeScope(); builder.RegisterAssemblyTypes(Assembly.GetAssembly(typeof(Management.Repositories.BaseRepository))) .Where(t => t.IsSubclassOf(typeof(Management.Repositories.BaseRepository))) .PropertiesAutowired() .AsImplementedInterfaces() .InstancePerLifetimeScope(); builder.RegisterAssemblyTypes(Assembly.GetAssembly(typeof(BaseService))) .Where(t => t.IsSubclassOf(typeof(BaseService))) .PropertiesAutowired() .AsImplementedInterfaces() .InstancePerLifetimeScope(); }
private void DoWork(object state) { try { if (isRunning) { return; } isRunning = true; List <ERC20Token> tokens; using (var dbContext = new WalletDbContext(DbContextOptionsFactory.DbContextOptions())) { tokens = dbContext.Erc20Tokens.ToList(); } foreach (var token in tokens) { if (token.IsSynchronized) { continue; } var lastBlockNumber = (int)(_explorer.GetLastAvailableBlockNumber().Result.Value); var logs = _explorer.GetFullEventLogs(token, lastBlockNumber).Result; var holders = EventLogsExplorer.GetInfoFromLogs(logs); for (int i = 0; i < holders.Count; i++) { try { var balance = _explorer.GetTokenHolderBalance(holders[i].Address, token.Address).Result; holders[i].Quantity = Web3.Convert.FromWei(balance, token.DecimalPlaces); holders[i].ERC20TokenId = token.Id; } catch (Exception e) { i--; } } SaveToDb(logs); SaveToDb(holders); using (var dbContext = new WalletDbContext(DbContextOptionsFactory.DbContextOptions())) { token.LastSynchronizedBlockNumber = lastBlockNumber; token.IsSynchronized = true; dbContext.Erc20Tokens.Update(token); dbContext.SaveChanges(); } } isRunning = false; } catch (Exception e) { isRunning = false; } }
private DbContextOptions <CurrencyContext> ConfigureCurrencyDbContext(IComponentContext context) => DbContextOptionsFactory.Get(context);