Aleksandr
Aleksandr
CC#
Created by Aleksandr on 4/16/2024 in #help
Property meanings
For clarification, I'll mention that this is a client-server web application, i.e., with a back-end and front-end part, and the front-end also requires type checking of subscriptions. The subscriptions themselves are stored in the database, and recently we've been changing the number of days for the trial version's validity. I assume it would be easier for the front-end to look at static numbers like 1, 2, 3 instead of 7, 14, 30. The project context implies working with real estate and cars, and it can be assumed that subscriptions may be categorized thematically according to categories and have the same number of days but different rights. Another interesting question arises: would the Single Responsibility Principle be violated if we use the 'Days' field both as the type and the duration of the subscription?
16 replies
CC#
Created by Aleksandr on 3/28/2024 in #help
BackgroundService and dbcontext memory issue
Yes, i use select, so AsNoTracking useless And in someService i get full entities by this ids, and there i dont use AsNoTracking cause i need to track changes
8 replies
CC#
Created by Aleksandr on 3/28/2024 in #help
BackgroundService and dbcontext memory issue
public class UpdateEntitiesBgService : BgService
{

public UpdateEntitiesBgService(IServiceScopeFactory scope) : base(scope) { }

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
await ParseUrl(stoppingToken);
}

private async Task ParseUrl(CancellationToken stoppingToken)
{
await Task.Delay(TimeSpan.FromMinutes(3), stoppingToken);

int skip = 0;
const int take = 1;

while (!stoppingToken.IsCancellationRequested)
{
try
{
var lCount = await Logic(skip, take, stoppingToken);

skip = lCount == 0 ? 0 : skip += take;
}
catch (DbUpdateConcurrencyException ex)
{
ExceptionNotify(GetType().Name, ex.Message, ex.StackTrace);
}
catch (Exception ex)
{
ExceptionNotify(GetType().Name, ex.Message, ex.StackTrace);
}
await Task.Delay(TimeSpan.FromMinutes(1), stoppingToken);
}
}

private async Task<int> Logic(int skip, int take, CancellationToken stoppingToken)
{
using var scope = _scope.CreateScope();
using var context = scope.ServiceProvider.GetRequiredService<WebApiContext>();
var someService = scope.ServiceProvider.GetService<SomeService>();

var dateNow = DateTimeOffset.UtcNow;

var ids = await context.Entities
.Where(x => x.IsActive)
.Skip(skip)
.Take(take)
.Select(x => x.Id)
.ToListAsync(stoppingToken);

await someService.UpdateEntities(ids, true);

return ids.Count;
}
}
public class UpdateEntitiesBgService : BgService
{

public UpdateEntitiesBgService(IServiceScopeFactory scope) : base(scope) { }

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
await ParseUrl(stoppingToken);
}

private async Task ParseUrl(CancellationToken stoppingToken)
{
await Task.Delay(TimeSpan.FromMinutes(3), stoppingToken);

int skip = 0;
const int take = 1;

while (!stoppingToken.IsCancellationRequested)
{
try
{
var lCount = await Logic(skip, take, stoppingToken);

skip = lCount == 0 ? 0 : skip += take;
}
catch (DbUpdateConcurrencyException ex)
{
ExceptionNotify(GetType().Name, ex.Message, ex.StackTrace);
}
catch (Exception ex)
{
ExceptionNotify(GetType().Name, ex.Message, ex.StackTrace);
}
await Task.Delay(TimeSpan.FromMinutes(1), stoppingToken);
}
}

private async Task<int> Logic(int skip, int take, CancellationToken stoppingToken)
{
using var scope = _scope.CreateScope();
using var context = scope.ServiceProvider.GetRequiredService<WebApiContext>();
var someService = scope.ServiceProvider.GetService<SomeService>();

var dateNow = DateTimeOffset.UtcNow;

var ids = await context.Entities
.Where(x => x.IsActive)
.Skip(skip)
.Take(take)
.Select(x => x.Id)
.ToListAsync(stoppingToken);

await someService.UpdateEntities(ids, true);

return ids.Count;
}
}
8 replies
CC#
Created by Aleksandr on 3/28/2024 in #help
BackgroundService and dbcontext memory issue
Is not so interesting, but of course here you are public class UpdateEntitiesBgService : BgService { public UpdateEntitiesBgService(IServiceScopeFactory scope) : base(scope) { } protected override async Task ExecuteAsync(CancellationToken stoppingToken) { await ParseUrl(stoppingToken); } private async Task ParseUrl(CancellationToken stoppingToken) { await Task.Delay(TimeSpan.FromMinutes(3), stoppingToken); int skip = 0; const int take = 1; while (!stoppingToken.IsCancellationRequested) { try { var lCount = await Logic(skip, take, stoppingToken); skip = lCount == 0 ? 0 : skip += take; } catch (DbUpdateConcurrencyException ex) { ExceptionNotify(GetType().Name, ex.Message, ex.StackTrace); } catch (Exception ex) { ExceptionNotify(GetType().Name, ex.Message, ex.StackTrace); } await Task.Delay(TimeSpan.FromMinutes(1), stoppingToken); } } private async Task<int> Logic(int skip, int take, CancellationToken stoppingToken) { using var scope = _scope.CreateScope(); using var context = scope.ServiceProvider.GetRequiredService<WebApiContext>(); var someService = scope.ServiceProvider.GetService<SomeService>(); var dateNow = DateTimeOffset.UtcNow; var ids = await context.Entities .Where(x => x.IsActive) .Skip(skip) .Take(take) .Select(x => x.Id) .ToListAsync(stoppingToken); await someService.UpdateEntities(ids, true); return ids.Count; } }
8 replies