From 3f696a32ab141c38aa4aa75be602a9c0134f8e0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D0=B4=D1=80=D0=B5=D0=B9=20=D0=9A=D1=83=D0=B4?= =?UTF-8?q?=D0=B0=D1=88=D0=BA=D0=B8=D0=BD?= Date: Tue, 13 Feb 2018 21:13:16 +0300 Subject: [PATCH] Added AsNoTracking for readonly queries --- .../Services/CorsPolicyService.cs | 3 ++- .../Stores/ClientStore.cs | 1 + .../Stores/PersistedGrantStore.cs | 4 ++-- .../Stores/ResourceStore.cs | 18 ++++++++++-------- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/IdentityServer4.EntityFramework/Services/CorsPolicyService.cs b/src/IdentityServer4.EntityFramework/Services/CorsPolicyService.cs index 32b5801..0c296c3 100644 --- a/src/IdentityServer4.EntityFramework/Services/CorsPolicyService.cs +++ b/src/IdentityServer4.EntityFramework/Services/CorsPolicyService.cs @@ -9,6 +9,7 @@ using IdentityServer4.EntityFramework.Interfaces; using Microsoft.Extensions.Logging; using Microsoft.AspNetCore.Http; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; namespace IdentityServer4.EntityFramework.Services @@ -44,7 +45,7 @@ public Task IsOriginAllowedAsync(string origin) // doing this here and not in the ctor because: https://github.com/aspnet/CORS/issues/105 var dbContext = _context.HttpContext.RequestServices.GetRequiredService(); - var origins = dbContext.Clients.SelectMany(x => x.AllowedCorsOrigins.Select(y => y.Origin)).ToList(); + var origins = dbContext.Clients.SelectMany(x => x.AllowedCorsOrigins.Select(y => y.Origin)).AsNoTracking().ToList(); var distinctOrigins = origins.Where(x => x != null).Distinct(); diff --git a/src/IdentityServer4.EntityFramework/Stores/ClientStore.cs b/src/IdentityServer4.EntityFramework/Stores/ClientStore.cs index 4faf191..d3ff81f 100644 --- a/src/IdentityServer4.EntityFramework/Stores/ClientStore.cs +++ b/src/IdentityServer4.EntityFramework/Stores/ClientStore.cs @@ -54,6 +54,7 @@ public Task FindClientByIdAsync(string clientId) .Include(x => x.IdentityProviderRestrictions) .Include(x => x.AllowedCorsOrigins) .Include(x => x.Properties) + .AsNoTracking() .FirstOrDefault(x => x.ClientId == clientId); var model = client?.ToModel(); diff --git a/src/IdentityServer4.EntityFramework/Stores/PersistedGrantStore.cs b/src/IdentityServer4.EntityFramework/Stores/PersistedGrantStore.cs index f26253b..089cd68 100644 --- a/src/IdentityServer4.EntityFramework/Stores/PersistedGrantStore.cs +++ b/src/IdentityServer4.EntityFramework/Stores/PersistedGrantStore.cs @@ -75,7 +75,7 @@ public Task StoreAsync(PersistedGrant token) /// public Task GetAsync(string key) { - var persistedGrant = _context.PersistedGrants.FirstOrDefault(x => x.Key == key); + var persistedGrant = _context.PersistedGrants.AsNoTracking().FirstOrDefault(x => x.Key == key); var model = persistedGrant?.ToModel(); _logger.LogDebug("{persistedGrantKey} found in database: {persistedGrantKeyFound}", key, model != null); @@ -90,7 +90,7 @@ public Task GetAsync(string key) /// public Task> GetAllAsync(string subjectId) { - var persistedGrants = _context.PersistedGrants.Where(x => x.SubjectId == subjectId).ToList(); + var persistedGrants = _context.PersistedGrants.Where(x => x.SubjectId == subjectId).AsNoTracking().ToList(); var model = persistedGrants.Select(x => x.ToModel()); _logger.LogDebug("{persistedGrantCount} persisted grants found for {subjectId}", persistedGrants.Count, subjectId); diff --git a/src/IdentityServer4.EntityFramework/Stores/ResourceStore.cs b/src/IdentityServer4.EntityFramework/Stores/ResourceStore.cs index 70a6d3c..cefb0b2 100644 --- a/src/IdentityServer4.EntityFramework/Stores/ResourceStore.cs +++ b/src/IdentityServer4.EntityFramework/Stores/ResourceStore.cs @@ -54,7 +54,7 @@ from apiResource in _context.ApiResources .ThenInclude(s => s.UserClaims) .Include(x => x.UserClaims); - var api = apis.FirstOrDefault(); + var api = apis.AsNoTracking().FirstOrDefault(); if (api != null) { @@ -79,7 +79,7 @@ public Task> FindApiResourcesByScopeAsync(IEnumerablenames.Contains(x.Name)).Any() + where api.Scopes.Where(x => names.Contains(x.Name)).Any() select api; var apis = query @@ -88,7 +88,7 @@ where api.Scopes.Where(x=>names.Contains(x.Name)).Any() .ThenInclude(s => s.UserClaims) .Include(x => x.UserClaims); - var results = apis.ToArray(); + var results = apis.AsNoTracking().ToArray(); var models = results.Select(x => x.ToModel()).ToArray(); _logger.LogDebug("Found {scopes} API scopes in database", models.SelectMany(x => x.Scopes).Select(x => x.Name)); @@ -113,7 +113,7 @@ where scopes.Contains(identityResource.Name) var resources = query .Include(x => x.UserClaims); - var results = resources.ToArray(); + var results = resources.AsNoTracking().ToArray(); _logger.LogDebug("Found {scopes} identity scopes in database", results.Select(x => x.Name)); @@ -127,19 +127,21 @@ where scopes.Contains(identityResource.Name) public Task GetAllResourcesAsync() { var identity = _context.IdentityResources - .Include(x => x.UserClaims); + .Include(x => x.UserClaims) + .AsNoTracking(); var apis = _context.ApiResources .Include(x => x.Secrets) .Include(x => x.Scopes) - .ThenInclude(s => s.UserClaims) - .Include(x => x.UserClaims); + .ThenInclude(s => s.UserClaims) + .Include(x => x.UserClaims) + .AsNoTracking(); var result = new Resources( identity.ToArray().Select(x => x.ToModel()).AsEnumerable(), apis.ToArray().Select(x => x.ToModel()).AsEnumerable()); - _logger.LogDebug("Found {scopes} as all scopes in database", result.IdentityResources.Select(x=>x.Name).Union(result.ApiResources.SelectMany(x=>x.Scopes).Select(x=>x.Name))); + _logger.LogDebug("Found {scopes} as all scopes in database", result.IdentityResources.Select(x => x.Name).Union(result.ApiResources.SelectMany(x => x.Scopes).Select(x => x.Name))); return Task.FromResult(result); }