EF Core System.ObjectDisposedException - entity-framework-core

I am getting this error mentioned in the title. The same code works for the first API call, but not for the 2nd call. It fails on the 2nd SaveChangesAsync() call
await dbContext.SaveChangesAsync().ConfigureAwait(false);
I am using
services.AddTransient<ProjectContext>();
in my startup.cs. I tried AddScope but it doesn't work.
Error:
System.ObjectDisposedException: 'Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling Dispose() on the context, or wrapping the context in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances.
Object name: 'ProjectContext'.'
Code:
private readonly Action<InjectedControllerArgs, ProjectDto, NavigationElements> _postAfterSave = async (injectedControllerArgs, dto, nav) => {
var memberClient = new MemberClient { MemberId = member.MemberId,
InvitationEmail = member.Email,
ClientId = dto.ClientId,
LastModifierId = member.MemberId };
await dbContext.MemberClient!.AddAsync(memberClient).ConfigureAwait(false);
await dbContext.SaveChangesAsync().ConfigureAwait(false);
var role = await dbContext.Role.Where(r => "client-admin" == r.RoleCode)
.FirstOrDefaultAsync()
.ConfigureAwait(false);
await dbContext.MemberClientRole!.AddAsync(new MemberClientRole
{ LastModifierId = member.MemberId,
MemberClientId = memberClient.MemberClientId,
RoleId = role.RoleId })
.ConfigureAwait(false);
await dbContext.SaveChangesAsync().ConfigureAwait(false); }
public async Task<ActionResult<ProjectDto>> PostInsertAsync(int customerId, int clientId, ProjectDto dto)
{
if (dto == null) throw new ArgumentNullException(nameof(dto));
dto.CustomerId = customerId;
dto.ClientId = clientId;
return await _pmBaseController.PostAsync(new NavigationElements(customerId, clientId, null), Topic, dto,
_returnOne, _postBeforeSave, _postAfterSave).ConfigureAwait(false);
}
public async Task<IActionResult> PutAsync(NavigationElements nav,
TopicName topic,
int id,
TDto dto,
Func<InjectedControllerArgs, NavigationElements, int, Task<T>> returnOne,
Func<TDto, int, TDto>? putBeforeSave = null,
Action<InjectedControllerArgs, TDto, NavigationElements>? putAfterSave = null
) ...

Your _postAfterSave delegate is asynchronous but does not return a Task which effectively means it is async void so your _pmBaseController is unable to await it which means execution will proceed before this Action is done. That the first SaveChanges works is timing / coincidence.
Try changing the type from Action<InjectedControllerArgs, ProjectDto, NavigationElements> to Func<InjectedControllerArgs, ProjectDto, NavigationElements, Task> and make sure to await it everywhere.

Related

how to add a token head to a request using HttpClient from IHttpClientFactory in Blazor

I am trying to use JWT in my API, and configuration is completed, can use postman tool to access data from it. However when I use Blazor as front end to access it , the request doesn't have token, so always give a 401 code.
Below is my Blazor code.
program.cs
builder.Services.AddHttpClient<IOptionService, OptionService> ("OptionAPI", (sp, cl) => {
cl.BaseAddress = new Uri("https://localhost:7172");
});
builder.Services.AddScoped(
sp => sp.GetService<IHttpClientFactory>().CreateClient("OptionAPI"));
OptionService.cs
public class OptionService : IOptionService {
private readonly HttpClient _httpClient;
public OptionService(HttpClient httpClient) {
_httpClient = httpClient;
}
public async Task<IEnumerable<OptionOutputDto>> GetOptionsAsync(Guid quizId, Guid questionId) {
_httpClient.DefaultRequestHeaders.Authorization
= new AuthenticationHeaderValue("Bearer", "token");
return await JsonSerializer.DeserializeAsync<IEnumerable<OptionOutputDto>>(
await _httpClient.GetStreamAsync($"api/quizzes/{quizId}/{questionId}/options"),
new JsonSerializerOptions {
PropertyNameCaseInsensitive = true
});
}
I tired use " new AuthenticationHeaderValue("Bearer", "token");" to attach token in header, but its not working, still give 401 code.
And I also tried use
private readonly IHttpClientFactory _httpClient;
public OptionService(IHttpClientFactory httpClient) {
_httpClient = httpClient;
}
public async Task<IEnumerable<OptionOutputDto>> GetOptionsAsync(Guid quizId, Guid questionId) {
var newHttpClient = _httpClient.CreateClient();
newHttpClient.DefaultRequestHeaders.Authorization
= new AuthenticationHeaderValue("Bearer", "token");
return await JsonSerializer.DeserializeAsync<IEnumerable<OptionOutputDto>>(
await newHttpClient.GetStreamAsync($"api/quizzes/{quizId}/{questionId}/options"),
new JsonSerializerOptions {
PropertyNameCaseInsensitive = true
});
}
it's also not working, give me an error,
Unhandled exception rendering component: A suitable constructor for type 'Services.OptionService' could not be located. Ensure the type is concrete and all parameters of a public constructor are either registered as services or passed as arguments. Also ensure no extraneous arguments are provided.
System.InvalidOperationException: A suitable constructor for type .....
Can anyone has a simple way to attach token in request header?
Thanks in advance.
I think the good option is :
builder.Services.AddHttpClient<IOptionService, OptionService> ("OptionAPI", (sp, cl) => {
cl.BaseAddress = new Uri("https://localhost:7172");
});
Could you check if the token is present in header or not?
Your error is most likely related to how the OptionService is being registered in dependency injection. It either needs an empty constructor adding - and/or - you need to ensure that the constructor has all of its dependencies registered correctly in the ServicesCollection too.
The exception is quite explicit:
Ensure the type is concrete and all parameters of a public constructor
are either registered as services or passed as arguments. Also ensure
no extraneous arguments are provided
I gave a similar answer here. Basically you need to include the BaseAddressAuthorizationMessageHandler when defining your httpclients. If you're using a typed httpclient, you can inject the IAccessTokenProvider and get the token from there. Kinda like this:
public class MyHttpClient(IAccessTokenProvider tokenProvider, HttpClient httpClient)
{
_tokenProvider = tokenProvider;
_httpClient = httpClient;
}
private async Task RequestAuthToken()
{
var requestToken = await _tokenProvider.RequestAccessToken();
requestToken.TryGetToken(out var token);
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Value);
}
public async Task<IEnumerable<ReplyDto>> SendHttpRequest()
{
await RequestAuthToken();
return await JsonSerializer.DeserializeAsync<IEnumerable<ReplyDto>>(
await _httpClient.GetStreamAsync("api/getendpoint"),
new JsonSerializerOptions {
PropertyNameCaseInsensitive = true
});
}

Does EF Core Have a Query Que?

I am new to EF Core and am trying to figure out why I keep having to increase my SQl timeout setting to make a specific query work. In the process I developed a theory. What if EF has a que of pending requests and when there are too many it just stops working instead of removing older requests from the que. That might explain why increasing timeout time is the only way to keep my browser from showing "err connection closed" when I load pages using a specific query.
I tried posting the query on the Microsoft Q and A website with my problem a couple days ago (https://learn.microsoft.com/en-us/answers/questions/601858/aspnet-core-app-only-works-for-few-minutes-after-r.html), but as usual Microsoft has not responded with an explanation that specifically explains why their product does not work and how to fix it. The best responses I've gotten are vague suggestions that don't really explain what to do.
For instance, one guy suggest not using ToArray() but doesn't say how the same functionality can be obtained without it. Another guy suggests researching connection resiliency but doesn't say how doing that might solve my problem.
So far I have managed to keep the app running longer without this problem occurring by using LazyCache to cache results of the problem query, but after a couple days the app is having the same problem again. Why is it that Microsoft is quick to tell people not to do something, but never tells people what to do instead?
UPDATE: Since Microsoft requires people to sign into their account to access the above link and nobody here want to sign in here is part of what is posted there. It is an error message from running my app locally
An unhandled exception occurred while processing the request.
Win32Exception: The wait operation timed out.
Unknown location
SqlException: Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
Microsoft.Data.SqlClient.SqlCommand+<>c.<ExecuteDbDataReaderAsync>b__169_0(Task<SqlDataReader> result)
Stack Query Cookies Headers Routing
Win32Exception: The wait operation timed out.
Show raw exception details
SqlException: Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
Microsoft.Data.SqlClient.SqlCommand+<>c.<ExecuteDbDataReaderAsync>b_169_0(Task<SqlDataReader> result)
System.Threading.Tasks.ContinuationResultTaskFromResultTask<TAntecedentResult, TResult>.InnerInvoke()
System.Threading.Tasks.Task+<>c.<.cctor>b277_0(object obj)
System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, object state)
System.Threading.Tasks.Task.ExecuteWithThreadLocal(ref Task currentTaskSlot, Thread threadPoolThread)
Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable<T>+AsyncEnumerator.InitializeReaderAsync(DbContext , bool result, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementationAsync<TState, TResult>(Func<DbContext, TState, CancellationToken, Task<TResult>> operation, Func<DbContext, TState, CancellationToken, Task<ExecutionResult<TResult>>> verifySucceeded, TState state, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementationAsync<TState, TResult>(Func<DbContext, TState, CancellationToken, Task<TResult>> operation, Func<DbContext, TState, CancellationToken, Task<ExecutionResult<TResult>>> verifySucceeded, TState state, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable<T>+AsyncEnumerator.MoveNextAsync()
Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync<TSource>(IQueryable<TSource> source, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync<TSource>(IQueryable<TSource> source, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToArrayAsync<TSource>(IQueryable<TSource> source, CancellationToken cancellationToken)
PostAlmostAnything.SiteServices.PostsService.GetPosts() in PostsService.cs
}
public async Task<Posts[]> GetPosts()
{
IAppCache cache = new CachingService(CachingService.DefaultCacheProvider) { DefaultCachePolicy = new CacheDefaults { DefaultCacheDurationSeconds = GlobalStatic.SITEPOSTSCACHEDURATION() } };
string cacheKey = $"GetPosts";
var cacheobject = await (from post in _context.Posts
where post.Active == (bool?)true && post.Adminban == (bool?)false && post.Site == (int?)GlobalStatic.SITENUMBER()
select post into pts
select new Posts
{
Postid = pts.Postid,
Title = pts.Title,
PostAlmostAnything.SiteServices.PostsService.GetPaginatedResult(int currentPage, int pageSize) in PostsService.cs
Sc2url = linkgenerator.subcategory2link($"{pts.CategoryNavigation.Categoryurl}", $"{pts.SubcategoryNavigation.Subcategoryurl}", $"{pts.Subcategory2Navigation.Subcategory2url}")
}
}).ToArrayAsync();
}
public async Task<List<Posts>> GetPaginatedResult(int currentPage, int pageSize)
{
return (await GetPosts()).OrderByDescending((Posts d) => d.Postid).Skip((currentPage - 1) * pageSize).Take(pageSize)
.ToList();
}
public async Task<List<Posts>> GetPaginatedResultByCategoryCity(string catslug, string waslug, string regionslug, string cityslug, int currentPage, int pageSize)
{
return (from d in await GetPosts()
PostAlmostAnything.Pages.IndexModel.OnGetAsync() in Index.cshtml.cs
{
PostService = postService;
}
public async Task<PageResult> OnGetAsync()
{
Posts = await PostService.GetPaginatedResult(CurrentPage, PageSize);
return Page();
}
}
}
Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.ExecutorFactory+GenericTaskHandlerMethod.Convert<T>(object taskAsObject)
Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.ExecutorFactory+GenericTaskHandlerMethod.Execute(object receiver, object[] arguments)
Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeHandlerMethodAsync()
Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeNextPageFilterAsync()
Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.Rethrow(PageHandlerExecutedContext context)
Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeInnerFilterAsync()
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g_Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>gAwaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>gLogged|17_1(ResourceInvoker invoker)
Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g_AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.StatusCodePagesMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.MigrationsEndPointMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
UPDATE: Here is where I am now as far as trying to paginate the query. I cannot find a way to use an IQueryable and access it with an async task without getting errors for lacking GetAwaiter
public IQueryable<Posts> GetPosts(bool active)
{
var query = _context.Posts.Where(post => post.Active == active && post.Adminban == (bool?)false && post.Site == GlobalStatic.SITENUMBER());
return query;
}
public IQueryable<Posts> GetPaginatedResult(bool active, int currentPage, int pageSize)
{
//IAppCache cache = new CachingService(CachingService.DefaultCacheProvider) { DefaultCachePolicy = new CacheDefaults { DefaultCacheDurationSeconds = GlobalStatic.SITEPOSTSCACHEDURATION() } };
//string cacheKey = $"GetPosts";
var cacheobject = GetPosts(active)
.Select(pts => new Posts
{
Postid = pts.Postid,
Title = pts.Title,
Description = pts.Description,
Dateposted = pts.Dateposted,
Datemodified = pts.Datemodified,
Video = pts.Video,
Videostream = pts.Videostream,
Location = pts.Location,
Tags = pts.Tags,
Cap = pts.Cap,
Titletag = pts.Titletag,
Metatag = pts.Metatag,
Link = pts.Link,
Linkurl = pts.Linkurl,
Category = pts.Category,
Subcategory = pts.Subcategory,
Subcategory2 = pts.Subcategory2,
Worldarea = pts.Worldarea,
Region = pts.Region,
City = pts.City,
Sendemail = pts.Sendemail,
Userid = pts.Userid,
Active = pts.Active,
Adminban = pts.Adminban,
Posturl = linkgenerator.postlink($"{pts.Postid}", $"{pts.Title}"),
Comments = (from cm in pts.Comments
where cm.Active == (bool?)true && cm.Adminblock == (bool?)false
select cm into cmts
select new Comments
{
Commentid = cmts.Commentid,
Comment = cmts.Comment,
Date = cmts.Date,
Active = cmts.Active,
Adminblock = cmts.Adminblock,
Userid = cmts.Userid,
Post = cmts.Post,
Ratings = cmts.Ratings.Select((Ratings rts) => new Ratings
{
Commentnumber = rts.Commentnumber,
Rating = rts.Rating
}).ToArray(),
Images = cmts.Images.Select((Images imgs) => new Images
{
Image = imgs.Image,
Imagename = imgs.Imagename
}).ToArray(),
Likes = cmts.Likes.Select((Likes lks) => new Likes
{
Likeid = lks.Likeid,
Commentid = lks.Commentid
}).ToArray()
}).ToArray(),
Commentcount = pts.Comments.Where((Comments cm) => cm.Active == (bool?)true && cm.Adminblock == (bool?)false).Count(),
Ratings = (from r in pts.Ratings
where r.CommentnumberNavigation.Active == (bool?)true && r.CommentnumberNavigation.Adminblock == (bool?)false
select r into rtgs
select new Ratings
{
Ratingid = rtgs.Ratingid,
Rating = rtgs.Rating,
Daterated = rtgs.Daterated,
CommentnumberNavigation = new Comments
{
Commentid = rtgs.CommentnumberNavigation.Commentid
}
}).ToArray(),
Ratingcount = pts.Ratings.Where((Ratings rc) => rc.CommentnumberNavigation.Active == (bool?)true && rc.CommentnumberNavigation.Adminblock == (bool?)false).Count(),
Ratingavg = ratingavg((from rac in pts.Ratings
where rac.CommentnumberNavigation.Active == (bool?)true && rac.CommentnumberNavigation.Adminblock == (bool?)false
select rac into ra
select ra.Rating).Average()),
Images = pts.Images.Select((Images imgs) => new Images
{
Imageid = imgs.Imageid,
Imagename = imgs.Imagename,
Image = imgs.Image,
Imagetype = imgs.Imagetype,
Postid = imgs.Postid,
Comment = imgs.Comment,
Userid = imgs.Userid
}).ToArray(),
Primaryimage = computations.primaryimage((from pic in pts.Images
where pic.Imagetype == (int?)1
select pic into pi
select new Images
{
Imageid = pi.Imageid,
Image = pi.Image,
Imagename = pi.Imagename
}).DefaultIfEmpty().First()),
Likes = pts.Likes.Select((Likes lks) => new Likes
{
Likeid = lks.Likeid,
Post = lks.Post,
Commentid = lks.Commentid
}).ToArray(),
Likecount = pts.Likes.Where((Likes lc) => lc.Commentid == null).Count(),
WorldareaNavigation = new Worldarea
{
Worldarea1 = pts.WorldareaNavigation.Worldarea1,
Worldareaacronym = pts.WorldareaNavigation.Worldareaacronym,
Wurl = linkgenerator.worldarealink($"{pts.WorldareaNavigation.Worldareaacronym}")
},
RegionNavigation = new Regions
{
Regionname = pts.RegionNavigation.Regionname,
Regionacronym = pts.RegionNavigation.Regionacronym,
Rurl = linkgenerator.regionlink($"{pts.WorldareaNavigation.Worldareaacronym}", $"{pts.RegionNavigation.Regionacronym}")
},
CityNavigation = new Cities
{
City = pts.CityNavigation.City,
Cityacronym = pts.CityNavigation.Cityacronym,
Cityurl = linkgenerator.citylink($"{pts.WorldareaNavigation.Worldareaacronym}", $"{pts.RegionNavigation.Regionacronym}", $"{pts.CityNavigation.Cityacronym}")
},
CategoryNavigation = new Categories
{
Categoryname = pts.CategoryNavigation.Categoryname,
Categorylongname = pts.CategoryNavigation.Categorylongname,
Categorytitle = pts.CategoryNavigation.Categorytitle,
Categorydescription = pts.CategoryNavigation.Categorydescription,
Categorykeywords = pts.CategoryNavigation.Categorykeywords,
Categorymeta = pts.CategoryNavigation.Categorymeta,
Categoryurl = pts.CategoryNavigation.Categoryurl,
Curl = linkgenerator.categorylink($"{pts.CategoryNavigation.Categoryurl}")
},
SubcategoryNavigation = new Subcategories
{
Subcategoryname = pts.SubcategoryNavigation.Subcategoryname,
Subcategorylongname = pts.SubcategoryNavigation.Subcategorylongname,
Subcategorytitle = pts.SubcategoryNavigation.Subcategorytitle,
Subcategorydescription = pts.SubcategoryNavigation.Subcategorydescription,
Subcategorymeta = pts.SubcategoryNavigation.Subcategorymeta,
Subcategorykeywords = pts.SubcategoryNavigation.Subcategorykeywords,
Subcategoryurl = pts.SubcategoryNavigation.Subcategoryurl,
Scurl = linkgenerator.subcategorylink($"{pts.CategoryNavigation.Categoryurl}", $"{pts.SubcategoryNavigation.Subcategoryurl}")
},
Subcategory2Navigation = new Subcategory2
{
Subcategory2name = pts.Subcategory2Navigation.Subcategory2name,
Subcategory2longname = pts.Subcategory2Navigation.Subcategory2longname,
Subcategory2title = pts.Subcategory2Navigation.Subcategory2title,
Subcategory2description = pts.Subcategory2Navigation.Subcategory2description,
Subcategory2meta = pts.Subcategory2Navigation.Subcategory2meta,
Subcategory2keywords = pts.Subcategory2Navigation.Subcategory2keywords,
Subcategory2url = pts.Subcategory2Navigation.Subcategory2url,
Sc2url = linkgenerator.subcategory2link($"{pts.CategoryNavigation.Categoryurl}", $"{pts.SubcategoryNavigation.Subcategoryurl}", $"{pts.Subcategory2Navigation.Subcategory2url}")
}
}).OrderByDescending((Posts d) => d.Postid).Skip((currentPage - 1) * pageSize).Take(pageSize);
//Func<List<Posts>> actionThatWeWantToCache = () => cacheobject;
//var CachedAdapter = cache.GetOrAdd(cacheKey, actionThatWeWantToCache);
return cacheobject;
}```
Ok, here's your problem with code like this:
public async Task<Posts[]> GetPosts()
{
IAppCache cache = new CachingService(CachingService.DefaultCacheProvider) { DefaultCachePolicy = new CacheDefaults { DefaultCacheDurationSeconds = GlobalStatic.SITEPOSTSCACHEDURATION() } };
string cacheKey = $"GetPosts";
var cacheobject = await (from post in _context.Posts
where post.Active == (bool?)true && post.Adminban == (bool?)false && post.Site == (int?)GlobalStatic.SITENUMBER()
select post into pts
select new Posts
{
Postid = pts.Postid,
Title = pts.Title,
Sc2url = linkgenerator.subcategory2link($"{pts.CategoryNavigation.Categoryurl}", $"{pts.SubcategoryNavigation.Subcategoryurl}", $"{pts.Subcategory2Navigation.Subcategory2url}")
}
}).ToArrayAsync();
}
public async Task<List<Posts>> GetPaginatedResult(int currentPage, int pageSize)
{
return (await GetPosts()).OrderByDescending((Posts d) => d.Postid).Skip((currentPage - 1) * pageSize).Take(pageSize)
.ToList();
}
GetPosts is doing a ToArray which will look to materialize your ENTIRE table into entities BEFORE doing pagination.... This is also calling C# code like this linkgenerator(...) method which will trigger client-side evaluation which is a huge performance no-no since that will also materialize the related entities for all rows. You are then selecting the Post Entities into another class called Posts which is either a Post entity instance or a separate DTO/ViewModel in order to capture this link.
This is a common sin when I see developers pursuing DNRY (Do Not Repeat Yourself) to keep common logic in one place, and then employ patterns that treat everything consistently for consistency's sake.
GetPosts() is a dangerous method if it returns an array of posts as it would always return everything where the code you are calling wants to optimize for paginated results. This is very common in Generic implementations which force a one-size-fits-all on operations. Some tables will be small enough that a "GetAll" type operation is reasonable safe, however that common method against another table can halt a system for no good reason.
To satisfy DNRY, centralize the common query into a private method that can be called as needed. A public GetAll() method for Posts is probably never needed for this entity so I would remove it:
private IQueryable<Post> getPosts()
{
var query = _context.Posts
.Where(post => post.Active
&& !post.Adminban
&& post.Site == GlobalStatic.SITENUMBER());
return query;
}
This method can be called by every method that wants to query across Posts and their related details. This centralizes your common rules around active state, and tenancy. (ownership)
Now to your GetPaginatedResult method. Here you're going to want to ensure you are selecting your desired data. I would recommend declaring a ViewModel for the post data given you want to have some computed values.
[Serializable]
public class PostViewModel
{
public int PostId { get; set; }
public string Title { get; set; }
public string CategoryUrl { get; set; }
public string SubcategoryUrl { get; set; }
public string Subcategory2Url { get; set; }
public string Sc2Url
{
get { return linkgenerator.subcategory2link(CategoryUrl, SubcategoryUrl, Subcategory2url); }
}
}
Within Linq expressions we want to select all of the raw data from the entity and associated other entities. Where we want to compose/calculate values, we can expose getters in the view model that do this calculation as needed using the raw data.
And for the GetPaginatedResult method:
public async Task<List<Posts>> GetPaginatedResult(int currentPage, int pageSize)
{
var posts = await getPosts()
.Select(x => new PostViewModel
{
PostId = x.PostId,
Title = x.Title,
CategoryUrl = x.CategoryNavigation.Categoryurl,
SubcategoryUrl = x.SubcategoryNavigation.Subcategoryurl,
SubcategoryUrl = x.Subcategory2Navigation.Subcategory2url
}).Skip((currentPage -1) * pageSize)
.Take(pageSize)
.ToListAsync();
}
Note that the Select only selects the raw values from the Post and associated entities. It doesn't attempt to calculate the full link. The view model will take care of that when the property is accessed, such as when it is referenced to compose a view or serialized. You can perform simple computed fields in the Linq expression, but only basic stuff like appending strings together with "+" and some simple functions that EF can translate. Even String.Format (including $"{value}") is off the cards inside a Linq expression fed to EF. It cannot translate that to SQL so it would fall on client-side evaluation and you want to avoid that at all costs. By default with EF Core 2, client-side evaluation was on by default, with EF Core 3.1 onward it is off by default but some teams turn it on. I highly recommend leaving it off as it's a bigger landmine than lazy loading.
That should eliminate your performance issues. This generates a compact SQL statement taking into account the desired pagination and serializes just those results into a view model to return to the view. The core business rules around ownership can be cetralized, and by exposing IQueryable it is left to the consuming code to project or further modify the results (paginate, etc.) to build an efficient query. Don't expose any method that you don't intend to get called. (Like a GetAll())
Edit: It sounds like you are trying to replace:
public async Task<Posts[]> GetPosts()
with
public async IQueryable<Post> GetPosts()
This is *not the objective. The objective is to centralize the core logic into a protected method which GetPosts as well as GetPaginatedResult can both call rather than having GetPaginatedResult calling GetPosts which is loading all data. You also need to address that you cannot call linkgenerator from within the Select expression without triggering client-side evaluation so the suggestion there is to either define view models for the results, or if you insist on returning the entity models as a view model, adding a non-mapped property for the generated URL.
public async Task<Posts[]> GetPosts()
{
return await _getPosts()
.Select(pts => new Posts
{
Postid = pts.Postid,
Title = pts.Title,
CategoryUrl = pts.CategoryNavigation.CategoryUrl,
SubcategoryUrl = pts.SubcategoryNavigation.SubcategoryUrl,
Subcategory2Url = pts.SubcategoryNavigation.Subcategory2Url
}).ToArrayAsync();
}
public async Task<List<Posts>> GetPaginatedResult(int currentPage, int pageSize)
{
return await _getPosts()
.Select(pts => new Posts
{
Postid = pts.Postid,
Title = pts.Title,
CategoryUrl = pts.CategoryNavigation.CategoryUrl,
SubcategoryUrl = pts.SubcategoryNavigation.SubcategoryUrl,
Subcategory2Url = pts.SubcategoryNavigation.Subcategory2Url
}).Skip((currentPage-1) * pageSize)
.Take(pageSize)
.ToListAsync();
}
Where _getPosts() is a new protected IQueryable method that centralizes the core rules around your isActive and Site. I put an Underscore before the method name to differentiate it from your public GetPosts() method which is probably called in a number of different places.
You initially want to avoid changing the signature of your public methods because this would break anywhere and everywhere else that called this method, where it is better to correct this issue a bit at a time where it is most needed.
Looking at your updated example you don't want to change your public methods to IQueryable, leave those as your original return types and just ensure that these methods that call the new IQueryable method do their respective await with ToArrayAsync or ToListAsync respectively instead of calling the public GetPosts() method. The change you will still need to make is to move ALL properties where you are calling "linkgenerator" or any similar C# objects/functions into unmapped properties and instead use Select to retrieve the raw values these calls will need.

AspNet Boilerplate Parallel DB Access through Entity Framework from an AppService

We are using ASP.NET Zero and are running into issues with parallel processing from an AppService. We know requests must be transactional, but unfortunately we need to break out to slow running APIs for numerous calls, so we have to do parallel processing.
As expected, we are running into a DbContext contingency issue on the second database call we make:
System.InvalidOperationException: A second operation started on this context
before a previous operation completed. This is usually caused by different
threads using the same instance of DbContext, however instance members are
not guaranteed to be thread safe. This could also be caused by a nested query
being evaluated on the client, if this is the case rewrite the query avoiding
nested invocations.
We read that a new UOW is required, so we tried using both the method attribute and the explicit UowManager, but neither of the two worked.
We also tried creating instances of the referenced AppServices using the IocResolver, but we are still not able to get a unique DbContext per thread (please see below).
public List<InvoiceDto> CreateInvoices(List<InvoiceTemplateLineItemDto> templateLineItems)
{
List<InvoiceDto> invoices = new InvoiceDto[templateLineItems.Count].ToList();
ConcurrentQueue<Exception> exceptions = new ConcurrentQueue<Exception>();
Parallel.ForEach(templateLineItems, async (templateLineItem) =>
{
try
{
XAppService xAppService = _iocResolver.Resolve<XAppService>();
InvoiceDto invoice = await xAppService
.CreateInvoiceInvoiceItem();
invoices.Insert(templateLineItems.IndexOf(templateLineItem), invoice);
}
catch (Exception e)
{
exceptions.Enqueue(e);
}
});
if (exceptions.Count > 0) throw new AggregateException(exceptions);
return invoices;
}
How can we ensure that a new DbContext is availble per thread?
I was able to replicate and resolve the problem with a generic version of ABP. I'm still experiencing the problem in my original solution, which is far more complex. I'll have to do some more digging to determine why it is failing there.
For others that come across this problem, which is exactly the same issue as reference here, you can simply disable the UnitOfWork through an attribute as illustrated in the code below.
public class InvoiceAppService : ApplicationService
{
private readonly InvoiceItemAppService _invoiceItemAppService;
public InvoiceAppService(InvoiceItemAppService invoiceItemAppService)
{
_invoiceItemAppService = invoiceItemAppService;
}
// Just add this attribute
[UnitOfWork(IsDisabled = true)]
public InvoiceDto GetInvoice(List<int> invoiceItemIds)
{
_invoiceItemAppService.Initialize();
ConcurrentQueue<InvoiceItemDto> invoiceItems =
new ConcurrentQueue<InvoiceItemDto>();
ConcurrentQueue<Exception> exceptions = new ConcurrentQueue<Exception>();
Parallel.ForEach(invoiceItemIds, (invoiceItemId) =>
{
try
{
InvoiceItemDto invoiceItemDto =
_invoiceItemAppService.CreateAsync(invoiceItemId).Result;
invoiceItems.Enqueue(invoiceItemDto);
}
catch (Exception e)
{
exceptions.Enqueue(e);
}
});
if (exceptions.Count > 0) {
AggregateException ex = new AggregateException(exceptions);
Logger.Error("Unable to get invoice", ex);
throw ex;
}
return new InvoiceDto {
Date = DateTime.Now,
InvoiceItems = invoiceItems.ToArray()
};
}
}
public class InvoiceItemAppService : ApplicationService
{
private readonly IRepository<InvoiceItem> _invoiceItemRepository;
private readonly IRepository<Token> _tokenRepository;
private readonly IRepository<Credential> _credentialRepository;
private Token _token;
private Credential _credential;
public InvoiceItemAppService(IRepository<InvoiceItem> invoiceItemRepository,
IRepository<Token> tokenRepository,
IRepository<Credential> credentialRepository)
{
_invoiceItemRepository = invoiceItemRepository;
_tokenRepository = tokenRepository;
_credentialRepository = credentialRepository;
}
public void Initialize()
{
_token = _tokenRepository.FirstOrDefault(x => x.Id == 1);
_credential = _credentialRepository.FirstOrDefault(x => x.Id == 1);
}
// Create an invoice item using info from an external API and some db records
public async Task<InvoiceItemDto> CreateAsync(int id)
{
// Get db record
InvoiceItem invoiceItem = await _invoiceItemRepository.GetAsync(id);
// Get price
decimal price = await GetPriceAsync(invoiceItem.Description);
return new InvoiceItemDto {
Id = id,
Description = invoiceItem.Description,
Amount = price
};
}
private async Task<decimal> GetPriceAsync(string description)
{
// Simulate a slow API call to get price using description
// We use the token and credentials here in the real deal
await Task.Delay(5000);
return 100.00M;
}
}

Stack overflow exception while returning an object asynchronously

While using MongoDB C# driver with WebApi I came to the following problem. When I want to read all documents (or even just one) from the database the repo's function will get the correct data but in WebApi the object returned from the repo causes a stack overflow. I suspect that I am doing something wrong with the way the objects are returned.
WebApi where the Repo's method is called:
// GET api/<controller>
public async Task<List<Event>> Get()
{
return await _repo.FindAll();
}
// GET api/<controller>/5
public async Task<Event> Get(string id)
{
Event e = await _repo.FindById(id);
return e;
}
And corresponding methods in the Repo:
public async Task<Event> FindById(string id)
{
Event e = await _collection.Find<Event>(x => x.ID == ObjectId.Parse(id)).FirstAsync();
return e;
}
public async Task<List<Event>> FindAll()
{
var filter = new BsonDocument();
List<Event> list = await _collection.Find(filter).ToListAsync();
return await Task<List<Event>>.FromResult(list);
}
Thanks for all the help in advance!
Edit: I found that when I return string from the function instead of Event the whole thing works.
What I think is making problems is the ID property in the Event.
The problem was that the Event had an ObjecId property. The JSON.Net doesn't know about that type. See the solution here: JSON.NET cast error when serializing Mongo ObjectId

Test a method that does something as well as throws an exception

I'm getting started with TDD (and MoQ). I have a method that takes in an Order and creates a unique CustomerId for that Order.
public class CustomerService
{
public CustomerService(IOrderRepository orderRepo)
{
_orderRepo = orderRepo;
}
public string Create(Order order)
{
//1. Save the Order that comes in, irrespective of being valid or not.
_orderRepo.Save(order);
//2. If the order is invalid, throw an exception.
if (!isValid(order))
throw new ArgumentException();
//3. Create a Customer, generate a unique CustomerId and return that.
return createCustomerAndGenerateCustomerId(order);
}
}
The following test doesn't seem to be working correctly:
[Test]
[ExpectedException(typeof(ArgumentException))]
public void Create_WhenPassedInvalidOrder_StillPersistsIt()
{
//Arrange
var order = new Order(); //This is invalid, as it has some mandatory fields missing
var mockOrderRepo = new Mock<IOrderRepository>();
var customerService = new CustomerService(mockOrderRepo.Object);
//Act
customerService.Create(order); //This should call Save() on the order repo, but still throw an exception.
//Assert
mockOrderRepo.Verify(o => o.Save(order), Times.Once());
}
This test is always passing, even if I don't call _orderRepo.Save(). What am I doing wrong?
You cannot use ExpectedException in this scenario because Nunit will try/catch the exception on the test level so your mockOrderRepo.Verify never gets called.
So you manually need to try catch your customerService.Create call - and if you want manually assert on the thrown exception - or you the Assert.Throws if you're using Nunit 2.5 or higher:
[Test]
public void Create_WhenPassedInvalidOrder_StillPersistsIt()
{
//Arrange
var order = new Order();
var mockOrderRepo = new Mock<IOrderRepository>();
var customerService = new CustomerService(mockOrderRepo.Object);
//Act
Assert.Throws<ArgumentException>(() => customerService.Create(order));
//Assert
mockOrderRepo.Verify(o => o.Save(order), Times.Once());
}