Lazy Load Dependency Injected Services POC #1076
Replies: 2 comments 6 replies
-
For some reason the link goes to: It should be: |
Beta Was this translation helpful? Give feedback.
-
@Jayson-Furr .NET 5 introduced assembly lazy loading for WebAssembly: The problem of course is that the solution only caters to static applications - not dynamic applications like Oqtane which load dependencies at runtime. That being said, there was some previous effort invested in exploring lazy loading - in fact, earlier versions of Oqtane actually used the custom router to load assemblies but we later relocated this logic to Startup because we ran into the following problem... dependency injection. In the Oqtane framework, external modules usually have a client project which usually contains service classes for interaction with the back-end APIs… and we encourage developers to use the standard .NET Core dependency injection approach. So we need for these services to be registered in the service container. Currently we are doing this registration dynamically during startup however this requires that the assemblies are already loaded in the app domain. In a lazy load scenario the loading of assemblies is deferred... and .NET currently provides no way to register any services outside of startup - so any services that exist within an assembly cannot be registered. The only solution in this case is to abandon dependency injection altogether for these services and instantiate the services classes manually ( ie. service = new Service() ) – which is far from optimal. I raised this topic to Microsoft and they acknowledged that there is no solution currently. So basically lazy loading works great for Hello World apps - but falls down when building real apps. So given what I just described above, it would be helpful if you could describe if/how your POC addresses these issues. |
Beta Was this translation helpful? Give feedback.
-
@sbwalker @ADefWebserver I have come up with a way to lazy load DI services in Blazor WASM using an interface, lazy implementation, and real implementation. As with any lazy loading, the code would need to be large to benefit from it but here is a small POC.
The *.Real.dll is lazy loaded. When visiting counter page, the assembly is loaded prior to being used dynamically in the the *.Lazy.dll implementation.
https://github.com/Jayson-Furr/LazyLoadPOC
Beta Was this translation helpful? Give feedback.
All reactions