diff --git a/Lecti/Selector.cs b/Lecti/Selector.cs index abd7114..172cb57 100644 --- a/Lecti/Selector.cs +++ b/Lecti/Selector.cs @@ -38,14 +38,19 @@ public async Task ProcessAsync(RequestContext context, ServiceContext stk) var assignedTarget = await stk.DataRepositories.Context.Set().FindAsync(Guid.Parse(availableVariations[variation])); context.Target = assignedTarget ?? throw new KeyNotFoundException("Assigned target not found in the database."); + // Capture values before queuing deferred task to avoid accessing disposed HTTP context + var ipAddress = context.Request.HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString(); + var targetId = context.Target.Id.ToString(); + var pluginNamespace = stk.Identity.OriginManifest.Name; + // Store the assigned variation in the scoped store async Task Task(CancellationToken cancellationToken, Repositories dataRepos) { var data = new PluginData { - Key = context.Request.HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString(), - Value = context.Target.Id.ToString(), - Namespace = stk.Identity.OriginManifest.Name, + Key = ipAddress, + Value = targetId, + Namespace = pluginNamespace, }; await dataRepos.Context.Set().AddAsync(data, cancellationToken); await dataRepos.Context.SaveChangesAsync(cancellationToken);