c# - 是否可以在运行时使用两个不同的 Owin 启动文件?

标签 c# asp.net-mvc asp.net-mvc-5 owin mef

我正在使用带有 MEF framework 的 Asp.net MVC 5 应用程序允许我将 MVC 应用程序设计为主应用程序中的插件。

我需要我的一个插件需要有自己的 OwinStart up 类,该类在属于我的主应用程序的主 Owin 类之后运行。

换句话说,main.dllStartup 类,它总是需要先运行,然后 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 the appSetting 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 类?

https://learn.microsoft.com/en-us/aspnet/aspnet/overview/owin-and-katana/owin-startup-class-detection

最佳答案

似乎无法运行多个启动文件。

但是,我使用反射来完成工作。基本上,我在所有程序集中搜索实现 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/

相关文章:

c# - 使用按键触发 Blazor 中的按钮时未设置对象引用

c# - HttpWebRequest .GetResponse 抛出 WebException 'The operation has timed out'

jquery - ASP.NET MVC 5 中的 Ajax(部分 View )不起作用 - 始终仅单独打开部分 View

c# - 将 DisplayFormatAttribute.ConvertEmptyStringToNull 的默认值设置为 false

匹配 'password' 和 'admin' 的 Jquery 验证不起作用

c# - NHibernate - 没有显式更新的意外更新

c# - 是否存在任何NON-GPL/AGPL病毒扫描库?

javascript - .Net Core 单选按钮总是返回 false

c# - 将谷歌时区转换为 .net 时区

c# - 读取Axios获取Asp.Net MVC Controller 中的参数