我正在使用带有 MEF framework 的 Asp.net MVC 5 应用程序允许我将 MVC 应用程序设计为主应用程序中的插件。
我需要我的一个插件需要有自己的 OwinStart
up 类,该类在属于我的主应用程序的主 Owin 类之后运行。
换句话说,main.dll
有 Startup
类,它总是需要先运行,然后 plugin.dll
有一个 Startup
需要第二个运行的类。
是否可以有 2 个自己的启动类?
来自Docs about detecting启动类
The
OwinStartup
attribute overrides the naming convention. You can also specify a friendly name with this attribute, however, using a friendly name requires you to also use theappSetting
element in the configuration file.
所以我试着像这样添加一个友好的名字
[assembly: OwinStartup("pluginStartup", typeof(plugin.Startup))]
在配置文件中添加如下内容
<appSettings>
<add key="owin:appStartup" value="Main.Startup, Main" />
</appSettings>
但这并没有归档我的 Plugin.Startup
它只运行 Main.Startup
。
有没有办法运行两个不同的 Startup
类?
最佳答案
似乎无法运行多个启动文件。
但是,我使用反射来完成工作。基本上,我在所有程序集中搜索实现 IAppConfiguration
接口(interface)的任何类,然后在该实例上调用 Configuration
。
这是我的做法。
我创建了一个界面
public interface IAppConfiguration
{
void Configuration(IAppBuilder app);
}
然后在我的 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
{
}
}
关于c# - 是否可以在运行时使用两个不同的 Owin 启动文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48916380/