question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add feature to allow MVC application parts & other resources

See original GitHub issue

Duplicated 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:closed
  • Created 2 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
natemcmastercommented, Nov 17, 2021

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.

0reactions
github-actions[bot]commented, May 6, 2023

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Share controllers, views, Razor Pages and more with ...
Using Application Parts, you can share an assembly (DLL) containing controllers, views, Razor Pages, razor compilation sources, Tag Helpers, and ...
Read more >
Docs/aspnetcore/mvc/advanced/app-parts.md at master
An Application Part is an abstraction over the resources of an application, from which MVC features like controllers, view components, or tag helpers...
Read more >
When ASP.NET Core can't find your controller: debugging ...
Application Parts allow you to share the same resources (controllers, Razor Pages etc) between multiple apps. If you're familiar with Razor ...
Read more >
Area in ASP.NET MVC
Area allows us to partition the large application into smaller units where each unit contains a separate MVC folder structure, same as the...
Read more >
Top 13 ASP.NET Core Features You Need to Know
13 features to build better applications with ASP.NET Core · 1. Cross-platform & container support · 2. High performance · 3. Asynchronous via ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found