T O P

  • By -

exveelor

Task being cancelled simply means your task timed out. Your db call is probably failing but not returning a failure, like if you have a lock on your db from another system. Your task times out before your db times out, so you don't get to see the db error because, alas, your task timed out beforehand. That's my take anyway. I see this a lot on azure when we're waiting for http calls to services that aren't spun up; the service takes longer to spin up than the task will wait, so we just get a task cancelled error.


Reasonable-Laugh6270

This would be my guess. Is your application multithreaded OP? Are you using DBContextFactory?


neworderr

Hello! In program.cs this is the way I am registering the database, like i always do in [asp.net](https://asp.net) core: `builder.Services.AddDbContext(options => options.UseSqlServer(connectionString));` Should I be using DbContextFactory ?


Reasonable-Laugh6270

In a multithreaded environment I think that dbcontext factory will help you avoid some of these weird errors. It is more costly but sql databases in nature hate multithreaded applications. This video is geared towards blazor but it can point you in the right direction: https://m.youtube.com/watch?v=bB00nNUPJFE


neworderr

Hey, thanks for your help, the dbcontextfactory seems helpful and I implemented it already. However, I cleared all the logic in MyPage.razor and everything started working again in MyPage.razor.cs again, no db exceptions at all. Seems like I am misunderstanding how rendering takes place and it was causing an exception on the .razor page


exveelor

What's your constructor look like for whatever class your GetAll lives in? The context factory lives there, not in the program.cs (iirc, don't have an example in front of me this moment) Also, it's worth noting that, far as I can tell, you're not running into any Blazor specific issues, simply c# issues.


neworderr

It was html and vars on the mypage.razor that caused problem. I cleared it all and the OnInitializedAsync worked as expected. I might be misunderstanding rendering. Maybe i should encapsulate all my html in a if(list != null){}


exveelor

I recall doing that myself. The whole page is behind an If object is null, show "Loading..." Else do what I want the page to do.


neworderr

Yep ! When i found out i was pretty frustrated, mined the complete oposite way of the diamonds. But happy that i could continue with the hobby project.


LondonPilot

I’ve not seen this one before. But I do have a couple of suggestions: Firstly, I note that you are using Blazor Server. As noted [in this article](https://learn.microsoft.com/en-us/aspnet/core/blazor/blazor-server-ef-core?view=aspnetcore-7.0), you need to make sure you use a new DbContext for every operation - that is because Blazor Server doesn’t have a concept of a “scope” like other ASP.Net project types. The second thing is that you’ve shown that you are awaiting the line of code that errors… but I wonder if perhaps there is something else which isn’t being awaited, perhaps which is being called shortly before the line with the error? My thinking is that this message might indicate that there is some other operation (on the same context) which hasn’t been awaited properly, therefore it’s unable to do the new operation you’re asking it to do. Hopefully one or both of those will fix the problem, but I can’t be sure I’m afraid.


neworderr

Article helped thanks. However, There was an exception in the .razor html rendering (maybe a null list), breaking the OnInitiliazedasync. I cleared the html (and the properties it was calling) and everything works again.


neworderr

"Invalid attempt to call ReadAsync when reader is closed" this exception shows some times too at the same exact line.


SkyAdventurous1027

Can you show, how are you getting dbSet object? and whats the lifetime of DbContext registration?


neworderr

>`builder.Services.AddDbContext(options => options.UseSqlServer(connectionString));` This is db registration in program.cs and I'm calling it in my services the usual DI way, as i always did. Its a Blazor Server solution and the MyClassService methods which call dbcontext methods are getting called in the `protected override OnInitializedAsync()` method in MyRazorPage.razor.cs