My app has a number of custom (scoped) DI services. For the pages that need these, my services have an InitializeAsync() method which I call in the Load event of the page's Layout. However if the page is refreshed (F5 or reload button), the services are recreated, but the Layout's Load event does not fire, the page does not have the service's data so it fails. Is this per design? Do you have any suggestions how I can initialize services on reload?
Reloading a page with F5 should definitely invoke the Load method of your layout. Seems to work just like that in our local tests.
You are correct. One of the Layouts was missing an InitializeAsync call in the Load event which made me think the Load event was not firing. Thanks!
Actually this is wrong too. Using breakpoints in Visual Studio I can see if the layout Load event is called or not. What seems to be happening is:
- Home page has a layout called Main. This is shown on startup, and the Main layout Load event fires. (Also fires if page is refreshed).
- Home page includes a navigation sidebar. Select an item and a routing page appears. This has a different layout (called DataMain). The DataMain layout Load event fires (also fires if page is refreshed).
- The routing page consists of buttons which have a Click event with a single action: Navigate to a data page.
- All the data pages also have the layout DataMain. On navigating to these pages, the layout's Load event does NOT fire. However they still work, I suppose, because all the initialization was already done when the routing page was shown. If the browser is refreshed, the DataMain layout Load runs but does not complete.
On stepping through the Load function with the debugger, I find that (on browser refresh) it never returns from this Execute action (and my service initialization calls are after this):
Culture = await JSRuntime.InvokeAsync<string>("Radzen.getCulture");
I tried moving this to the end of all the layout Load handlers. This seems to have sorted out my main issue (the page now reloads). But I can't remember why I added this action to the Load handlers, so I don't know if having it at the end will cause any problems.