public DbContextQueryHandler(IScopedContext scopedContext) { _scopedContext = scopedContext; }
public async Task Invoke(HttpContext context, IScopedContext <TenantRequestContext> scopedTenantRequestContext, IScopedHttpContextRequestForm scopedHttpContextRequestForm, ITenantStore tenantStore) { try { // TODO: Probably should use regex here. string[] parts = context.Request.Path.Value.Split('/'); if (parts.Count() > 1) { string tenantName = parts[1]; scopedTenantRequestContext.Context.TenantName = tenantName; if (string.IsNullOrWhiteSpace(tenantName) || !await tenantStore.IsTenantValidAsync(tenantName)) { _logger.LogWarning($"TenantId={tenantName}, does not exist!"); context.Response.Clear(); context.Response.StatusCode = (int)HttpStatusCode.NotFound; await context.Response.WriteAsync(""); return; } StringBuilder sb = new StringBuilder(); sb.Append('/'); if (parts.Count() > 2) { for (int i = 2; i < parts.Count(); i++) { sb.Append(parts[i]); if (i < parts.Count() - 1) { sb.Append('/'); } } } string newPath = sb.ToString(); context.Request.Path = newPath; context.Request.PathBase = $"/{tenantName}"; switch (context.Request.Method) { case "GET": break; case "POST": if (context.Request.ContentType == "application/x-www-form-urlencoded") { scopedTenantRequestContext.Context.FormCollection = await scopedHttpContextRequestForm.GetFormCollectionAsync(); } break; } } } catch (Exception ex) { _logger.LogCritical(ex, "Unhandled exception: {exception}", ex.Message); throw; } await _next(context); }