Add feature to allow MVC application parts & other resources
See original GitHub issueDuplicated assemblies with MVC and other resources When using PluginLoader.CreateFromAssemblyFile and mvcBuilder.AddPluginFromAssemblyFile, you end up loading the assemblies twice, and they’ll end up in their own LoadContexts.
I may be doing things in the wrong spot, but I had two calls:
- PluginLoader.CreateFromAssemblyFile inside Startup.Configure, to add RequestPath entries for static content, and add Endpoint Routes.
- mvcBuilder.AddPluginFromAssemblyFile inside Startup.ConfigureServices, to MVC parts.
I was trying to use the IServiceCollection dependency injection to easily access to plugin data/state from an MVC controller, but was going crazy when GetService and any direct references to Singletons and static classes were always null. Well, it turns out I was making two different types of plugin loads, and that mean the assemblies loaded twice. The MVC controller had its own un-instantiated set of classes with everything null or default.
Describe the solution you’d like Within the IMvcBuilder extensions, add a method or mode to reference an assembly that has MVC parts, and call ApplicationParts.Add for those. This should be done without reloading the assembly - the use case is the assembly has already been loaded by the plugin manager.
Alternatively, there may be a way to add MVC parts elsewhere, but I recall reading it had to be in the right spot.
I ended up taking some of the code from mvcBuilder AddPluginLoader and grabbing just the bits I needed inside ConfigureServices. Inside my call to PluginLoader.CreateFromAssemblyFile I added a list of plugins that tell me they want to use MVC (part of my plugin interface).
var partFactory = ApplicationPartFactory.GetApplicationPartFactory(assemblyWithMVC);
foreach (var part in partFactory.GetApplicationParts(assemblyWithMVC))
{
mvcBuilder.PartManager.ApplicationParts.Add(part);
}
Interestingly, I didn’t need the GetCustomAttributes<RelatedAssemblyAttribute>()
to find the views. I haven’t looked into why, but I’m assuming they are being found or are already loaded. If I work out why I’ll post again.
Describe alternatives you’ve considered Well, I guess we could do it ourselves, like I did, but I REALLY like this library. I found it small enough to get going with easily, but with a good feature set. A little more documentation would be nice, but hey I know it’s probably a hobby project 😃
Additional context This is my first large project in ASP.NET Core. It started with NET 5.0 earlier this year and I just moved it to NET 6.0 RC2 as we’re only a day or two before launch! I know Startup.cs & Program.cs can now be merged in NET 6.0, but haven’t looked into that as yet.
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (2 by maintainers)
Thanks for the additional context. I think what you are describing has the potential to be its own class library and could probably be enhanced to have even deeper integration with ASP.NET Core. If this is something you are interested in developing on your own, I could link to your project from this project’s README.
Closing due to inactivity. If you are looking at this issue in the future and think it should be reopened, please make a commented here and mention natemcmaster so he sees the notification.