How to define multiple partial Owin Startup classes and have them all run their code

3.1k views Asked by At

I'm working on an MVC application that will feature a "plugin" architecture.

Basically there will be a main "host" project that will dynamically load other projects at runtime.

We want to move all ASP.NET Identity related stuff into its own separate plugin project.

The main host project already contains an Owin Startup class which has some logic that it needs to run. However, we also need to create an Owin Startup class within the Identity plugin project so we can call ConfigureAuth().

How can I accomplish this?

MvcPluginHost.Web Startup.cs

[assembly: OwinStartupAttribute(typeof(MvcPluginHost.Web.Startup))]
namespace MvcPluginHost.Web
{
    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.MapSignalR();
        }
    }
}

MvcPluginHost.Plugins.IdentityPlugin Startup.cs

[assembly: OwinStartupAttribute(typeof(MvcPluginHost.Plugins.IdentityPlugin.Startup))]
namespace MvcPluginHost.Plugins.IdentityPlugin
{
    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            ConfigureAuth(app);
        }
    }
}
1

There are 1 answers

8
bateloche On BEST ANSWER

Partial classes are not meant for this, the first thing that comes to mind is to query the desired assemblies for some interface and call a common method on all of them.

var type = typeof(IMyInterface);
var types = AppDomain.CurrentDomain.GetAssemblies()
    .SelectMany(s => s.GetTypes())
    .Where(p => type.IsAssignableFrom(p));

foreach(var implementation in types)
{
    var obj = (IMyInterface)Activator.CreateInstance(implementation);
    obj.RunPlugin() //Interface method you're trying to implement in partials
}