Jun*_*ior 1 c# asp.net-mvc mef owin asp.net-mvc-5
我正在使用带有MEF 框架的 Asp.net MVC 5 应用程序,以允许我将 MVC 应用程序设计为主应用程序中的插件。
我需要我的一个插件需要有自己的OwinStartup 类,该类在属于我的主应用程序的主 Owin 类之后运行。
换句话说,main.dll有Startup一个总是需要先运行plugin.dll的Startup类,然后有一个需要第二个运行的类。
是否可以拥有 2 个自己的创业班?
该
OwinStartup属性会覆盖命名约定。您还可以使用此属性指定友好名称,但是,使用友好名称需要您还使用appSetting配置文件中的元素。
所以我试着像这样添加一个友好的名字
[assembly: OwinStartup("pluginStartup", typeof(plugin.Startup))]
Run Code Online (Sandbox Code Playgroud)
在配置文件中添加了以下内容
<appSettings>
<add key="owin:appStartup" value="Main.Startup, Main" />
</appSettings>
Run Code Online (Sandbox Code Playgroud)
但这并没有归档我Plugin.Startup它只运行Main.Startup.
有没有办法运行两个不同的Startup类?
https://docs.microsoft.com/en-us/aspnet/aspnet/overview/owin-and-katana/owin-startup-class-detection
似乎不可能运行多个启动文件。
但是,我使用反射来完成工作。基本上,我在所有程序集中搜索实现IAppConfiguration接口的任何类,然后Configuration在该实例上调用。
这是我如何做到的。
我创建了一个界面
public interface IAppConfiguration
{
void Configuration(IAppBuilder app);
}
Run Code Online (Sandbox Code Playgroud)
然后在main.dll我的Startup类中添加了以下代码。
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
ConfigurePlugins(app);
}
private static void ConfigurePlugins(IAppBuilder app)
{
try
{
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
{
var startups = assembly.GetTypes().Where(x => x.IsClass && typeof(IAppConfiguration ).IsAssignableFrom(x)).ToList();
foreach (Type startup in startups)
{
var config = (IAppConfiguration )Activator.CreateInstance(startup);
config.Configuration(app);
}
}
}
catch
{
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1206 次 |
| 最近记录: |