admin管理员组文章数量:1287514
I am in the process of converting a .NET 6 Blazor Server app to a .NET 8 Blazor Web app. Our app relies heavily on a scoped service to make data, obtained from the HttpContext of the initial page request, available to all of our Blazor views and components. As you might expect, this is data that is specific to each user session.
When I launched the Blazor Web App, I discovered that a scoped service created during the PreRender phase gets disposed and recreated during the Interactive phase. The solution recommended by Microsoft is to use the PersistentComponentState service.
Evidently, this requires adding code (to register with the PersistentComponentState service, plus calls to TryTakeFromJson and PersistAsJson) to every routable Blazor view, and the persist-component-state tag helper has to be added to Pages/Shared/_Layout.cshtml so that all embedded (i.e. non-routable) components can access the persisted state.
This is a deal breaker for us; we want to upgrade to .NET 8 without having to make extensive code modifications.
I tried disabling prerender globally by setting the @rendermode attribute (on both the HeadOutlet and Routes elements in App.razor) to: "new InteractiveServerRenderMode(prerender: false)" to see if the scoped service created upon application startup would persist, but the behavior did not change, i.e. a new instance of the service still got created. This was a disappointment, because it is forcing us to abandon the idea of taking advantage of the hybrid model, and instead do a more conservative upgrade to .NET 8 Server app.
Is there really no way to convert to a Blazor Server app to a Blazor Web App and still retain the behavior where a scoped service experiences a single instantiation?
本文标签:
版权声明:本文标题:blazor - Looking for simple way to persist scoped service state from Prerender phase to Interactive phase - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741313517a2371784.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论