Skip to content

Blazor with efcore proxies crash. #18076

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
alextech opened this issue Jan 1, 2020 · 5 comments
Closed

Blazor with efcore proxies crash. #18076

alextech opened this issue Jan 1, 2020 · 5 comments
Labels
area-blazor Includes: Blazor, Razor Components ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. Status: Resolved
Milestone

Comments

@alextech
Copy link

alextech commented Jan 1, 2020

Related to #10448

Minimal reproduceable project based on blazor template at: https://github.com/alextech/sample_use_proxy_blazor

Here, the issue is using proxies in efcore with blazor. If an object is passed to a sub-component that is accessing entity property sometime after the page has been loaded, context may have already been disposed.

Lazy Loading Proxies are needed to avoid cartesian explosion in master/detail style user interfaces, where each entity in main list has its own sublist of owned entities, that in turn, have their own joins.

Unhandled exception rendering component: Error generated for warning 'Microsoft.EntityFrameworkCore.Infrastructure.LazyLoadOnDisposedContextWarning': An attempt was made to lazy-load navigation property 'Description' on entity type 'WeatherForecastProxy' after the ass
ociated DbContext was disposed. This exception can be suppressed or logged by passing event ID 'CoreEventId.LazyLoadOnDisposedContextWarning' to the 'ConfigureWarnings' method in 'DbContext.OnConfiguring' or 'AddDbContext'.
System.InvalidOperationException: Error generated for warning 'Microsoft.EntityFrameworkCore.Infrastructure.LazyLoadOnDisposedContextWarning': An attempt was made to lazy-load navigation property 'Description' on entity type 'WeatherForecastProxy' after the associated DbCon
text was disposed. This exception can be suppressed or logged by passing event ID 'CoreEventId.LazyLoadOnDisposedContextWarning' to the 'ConfigureWarnings' method in 'DbContext.OnConfiguring' or 'AddDbContext'.
   at Microsoft.EntityFrameworkCore.Diagnostics.EventDefinition`2.Log[TLoggerCategory](IDiagnosticsLogger`1 logger, WarningBehavior warningBehavior, TParam1 arg1, TParam2 arg2)
   at Microsoft.EntityFrameworkCore.Diagnostics.CoreLoggerExtensions.LazyLoadOnDisposedContextWarning(IDiagnosticsLogger`1 diagnostics, DbContext context, Object entityType, String navigationName)
   at Microsoft.EntityFrameworkCore.Internal.LazyLoader.ShouldLoad(Object entity, String navigationName, NavigationEntry& navigationEntry)
   at Microsoft.EntityFrameworkCore.Internal.LazyLoader.Load(Object entity, String navigationName)
   at Microsoft.EntityFrameworkCore.Proxies.Internal.LazyLoadingInterceptor.Intercept(IInvocation invocation)
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at Castle.Proxies.WeatherForecastProxy.get_Description()
   at sample_use_proxy.Pages.WeatherDetails.BuildRenderTree(RenderTreeBuilder __builder)
   at Microsoft.AspNetCore.Components.ComponentBase.<.ctor>b__6_0(RenderTreeBuilder builder)
   at Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder batchBuilder, RenderFragment renderFragment)
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.RenderInExistingBatch(RenderQueueEntry renderQueueEntry)
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.ProcessRenderQueue()
fail: Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost[111]
      Unhandled exception in circuit 'iorggCZTbXKkp2XYKgVSJ7gAX9Zh3gJzhaCLmA6fiJE'.
System.InvalidOperationException: Error generated for warning 'Microsoft.EntityFrameworkCore.Infrastructure.LazyLoadOnDisposedContextWarning': An attempt was made to lazy-load navigation property 'Description' on entity type 'WeatherForecastProxy' after the associated DbCon
text was disposed. This exception can be suppressed or logged by passing event ID 'CoreEventId.LazyLoadOnDisposedContextWarning' to the 'ConfigureWarnings' method in 'DbContext.OnConfiguring' or 'AddDbContext'.
   at Microsoft.EntityFrameworkCore.Diagnostics.EventDefinition`2.Log[TLoggerCategory](IDiagnosticsLogger`1 logger, WarningBehavior warningBehavior, TParam1 arg1, TParam2 arg2)
   at Microsoft.EntityFrameworkCore.Diagnostics.CoreLoggerExtensions.LazyLoadOnDisposedContextWarning(IDiagnosticsLogger`1 diagnostics, DbContext context, Object entityType, String navigationName)
   at Microsoft.EntityFrameworkCore.Internal.LazyLoader.ShouldLoad(Object entity, String navigationName, NavigationEntry& navigationEntry)
   at Microsoft.EntityFrameworkCore.Internal.LazyLoader.Load(Object entity, String navigationName)
   at Microsoft.EntityFrameworkCore.Proxies.Internal.LazyLoadingInterceptor.Intercept(IInvocation invocation)
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at Castle.Proxies.WeatherForecastProxy.get_Description()
   at sample_use_proxy.Pages.WeatherDetails.BuildRenderTree(RenderTreeBuilder __builder)
   at Microsoft.AspNetCore.Components.ComponentBase.<.ctor>b__6_0(RenderTreeBuilder builder)
   at Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder batchBuilder, RenderFragment renderFragment)
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.RenderInExistingBatch(RenderQueueEntry renderQueueEntry)
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.ProcessRenderQueue()
@pranavkm pranavkm added the area-blazor Includes: Blazor, Razor Components label Jan 2, 2020
@pranavkm
Copy link
Contributor

pranavkm commented Jan 2, 2020

Hi @alextech, have a look at https://docs.microsoft.com/en-us/aspnet/core/blazor/dependency-injection?view=aspnetcore-3.1#utility-base-component-classes-to-manage-a-di-scope. Use this type to manage the lifetime of your EF context rather than creating and disposing it as part of your component initialization.

@pranavkm pranavkm added this to the Discussions milestone Jan 2, 2020
@pranavkm pranavkm added the ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. label Jan 2, 2020
@ghost ghost added the Status: Resolved label Jan 2, 2020
@alextech
Copy link
Author

alextech commented Jan 2, 2020

Thanks for reply!

Assuming I did it correctly at alextech/sample_use_proxy_blazor@8d86100

it puts me back to "second operation started on this context..."

@pranavkm
Copy link
Contributor

pranavkm commented Jan 2, 2020

it puts me back to "second operation started on this context..."

That error doesn't sound like it's specific to Blazor though. I'd suggest following up on StackOverflow for patterns that work well.

@alextech
Copy link
Author

alextech commented Jan 2, 2020

I can try stackoverflow or experts-exchane, though, this is in response to the linked issue that was collecting usecases of efcore usage with blazor.
Does this mean lazy-loading proxies are not intended to be used at all with blazor?

Discussing the issue at https://stackoverflow.com/questions/59569248/using-efcore-lazy-loading-proxies-with-blazor

@ghost
Copy link

ghost commented Jan 3, 2020

This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes.

See our Issue Management Policies for more information.

@ghost ghost closed this as completed Jan 3, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Feb 2, 2020
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. Status: Resolved
Projects
None yet
Development

No branches or pull requests

2 participants