In MVC 6 RC1 we used the IAssemlbyProvider
interface to register assemblies that were discovered at runtime and inject additional controller types, in a similar fashion to this post.. Now with RC2 release the IAssemblyProvider
has been removed and has changed to (see reference).
Our framework version is currently net46
.
Since the upgrade our controllers in external assemblies (not referenced) are returning a 404
status.
We have tried manually add the controller to the registered controllers via the ApplicationPartManager
.
var mvcBuilder = services.AddMvc();
var controllerFeature = new ControllerFeature();
mvcBuilder.PartManager.PopulateFeature(controllerFeature);
var moduleControllers = ModulesManager.GetControllers();
foreach (var c in moduleControllers)
controllerFeature.Controllers.Add(c);
mvcBuilder.PartManager.PopulateFeature(controllerFeature);
and...
services.AddMvc().ConfigureApplicationPartManager(app =>
{
var controllerFeature = new ControllerFeature();
app.PopulateFeature(controllerFeature);
var moduleControllers = ModulesManager.GetControllers();
foreach (var c in moduleControllers)
controllerFeature.Controllers.Add(c);
app.PopulateFeature(controllerFeature);
});
Now the assemblies are definitely loaded into the AppDomain
as our dependency injection system is finding and populating services for other items in the external assemblies.
With our previous implementations this worked nicely using the IAssemblyProvider
.
public class ModuleAwareAssemblyProvider : IAssemblyProvider
{
private readonly DefaultAssemblyProvider _defaultProvider;
public ModuleAwareAssemblyProvider(DefaultAssemblyProvider defaultProvider)
{
_defaultProvider = defaultProvider;
}
public IEnumerable<Assembly> CandidateAssemblies
{
get
{
return _defaultProvider.CandidateAssemblies.Concat(ModulesManager.Assemblies).Distinct();
}
}
}
I understand RC2 is still relatively new but if anyone has any experience registering additional controllers at start-up would be helpful.
Cheers, Nico