question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Blazor server-side] OwningComponentBase disposed inside OnParametersSetAsync during Login/Logout

See original GitHub issue

Describe the bug

I have custom authentication provider to provide refresh-less login/logout. If I login/logout on page inheriting OwningComponentBase then I get System.ObjectDisposedException

To Reproduce

Not so sure about reproducing steps but mine steps are:

  1. Going to page with child component having OwningComponentBase
  2. Login/Logout
  3. Receive System.ObjectDisposedException

Got Exceptions? Include both the message and the stack trace

blazor.server.js:15 [2020-04-08T17:04:44.371Z] Error: System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'Category'.
   at Microsoft.AspNetCore.Components.OwningComponentBase`1.get_Service()
   at Shople.Shared.Category.OnParametersSetAsync() in F:\ASP.NET Core\Shople\src\Shared\Category.razor:line 355
   at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle)

Component::OnParametersSetAsync (component implement IDisposable)

    protected override async Task OnParametersSetAsync()
    {
        Products = null;

        FilterItems = await Service?.GetCategoryFiltersAsync(Model.Category.Id); // OK, NO EXCEPTION

        StateHasChanged();

        Products = (await Service?.GetCategoryProductsAsync(Model.Category.Id, DisplayOrder, Page)).DiscountList((await AuthenticationStateTask).User); // EXCEPTION, Service is disposed... LINE 355
    }

Further technical details

  • ASP.NET Core version 3.1.2
  • VS 16.5

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
javiercncommented, Apr 9, 2020

@glararan here is an example on how to do this. https://github.com/javiercn/CancellableWorkBlazor

1reaction
glararancommented, Apr 9, 2020

@glararan here is an example on how to do this. https://github.com/javiercn/CancellableWorkBlazor

Works like charm! Thank you for example.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ASP.NET Core Blazor dependency injection
After the request completes, any scoped or transient services are disposed by the DI system. Server-side, the request scope lasts for the ...
Read more >
Why are Blazor lifecycle methods getting executed twice?
Try replacing RenderMode.ServerPrerendered with RenderMode.Server , then it behaves as expected, which is that the lifecycle methods are only ...
Read more >
An Introduction to OwningComponentBase
In this post, I'm going to explore the new OwningComponentBase class. I'll look at the quirks of service scopes in Blazor and how...
Read more >
Blazor Server: Cannot access a disposed object.
In a Blazor Server application, I am trying to query the database (via EF) on a timer and load data if there is...
Read more >
OwningComponentBase<T>
When our OwningComponentBase<T> component is disposed, its IServiceProvider ( Service property) is disposed too, which in turn will dispose of every instance it ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found