简单的注射器用asp.net mvc的4,负载控制器从另一个组件 [英] Simple Injector with asp.net mvc 4, load controller from another assembly

查看:108
本文介绍了简单的注射器用asp.net mvc的4,负载控制器从另一个组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个asp.net mvc的4个网站,使用简单注射器作为的Ioc工具。这将是一个可插拔的体系结构。有些控制器和视图在另一个组件(另一个mvc4应用,Plugin.Web.dll)。并从主应用程序,我知道Plugin.Web.dll的路径,加载插件。

I'm developing an asp.net mvc 4 site, using Simple Injector as a Ioc tool. It would be a pluggable architecture. Some controllers and views are in another assembly (another mvc4 application, Plugin.Web.dll). And from the main application, I know the path of Plugin.Web.dll, loading the plugin.

container.RegisterMvcControllers(Assembly.GetExecutingAssembly());

container.RegisterMvcAttributeFilterProvider();

container.Options.AllowOverridingRegistrations = true;

var appPath = AppDomain.CurrentDomain.BaseDirectory;

string[] files = Directory.GetFiles(appPath + "\\Plugins", "*",
    SearchOption.AllDirectories);

foreach (var fileName in files)
{
    Console.WriteLine(fileName);

    var assembly = Assembly.LoadFrom(fileName);

    container.RegisterMvcControllers(assembly);

    var controllerTypes =
        from type in assembly.GetExportedTypes()
        where type.Name.EndsWith("Controller",
            StringComparison.Ordinal)
        where typeof(IController).IsAssignableFrom(type)
        where !type.IsAbstract
        select type;

    // Instead of verify:
    foreach (var type in controllerTypes)
    {
        container.GetInstance(type);
    }
}            

container.Options.AllowOverridingRegistrations = false;       

container.Verify();

DependencyResolver.SetResolver(
    new SimpleInjectorDependencyResolver(container));

它给没有错误。

It gives no errors.

但是,当我点击这个点击,鉴于:

But when I click this click, in view:

@Html.ActionLink("plugin page","PluginPage","Plugin")

这使资源无法找到,HTTP 404

It gives 'The resource cannot be found., http 404'

在此先感谢

推荐答案

我怕你必须做一些更多的研究,因为你的问题是目前有点模糊,但这里有一些提示:

I'm afraid you'll have to do some more research, since your question is a bit vague at the moment, but here are some pointers:

检查 System.Web.Compilation.BuildManager.GetReferencedAssemblies()返回你的插件组件。在所有组件中的MVC DefaultControllerFactory 搜索控制器类型从该方法返回。最好是把/ bin目录里面的插件文件夹。如果我没有记错 GetReferencedAssemblies()看起来也位于/ bin的子目录。你可能需要编写一个定制的控制器工厂,如果插件必须更加动态加载。

Check if System.Web.Compilation.BuildManager.GetReferencedAssemblies() returns your plugin assemblies. The MVC DefaultControllerFactory searches for Controller types in all assemblies returned from that method. Best is to place your plugin folder inside the /bin directory. If I'm not mistaken GetReferencedAssemblies() also looks in sub directories of /bin. You'll probably need to write a custom controller factory if plugins must be loaded more dynamically.

另外看看<一个href=\"http://www.paraesthesia.com/archive/2013/01/21/putting-controllers-in-plugin-assemblies-for-asp-net-mvc.aspx\"相对=nofollow>这篇文章,因为它描述了如何强制BuildManager了解你的插件程序集。

Also take a look at this article, as it describes how to force the BuildManager to know about your plugin assemblies.

您配置似乎过于复杂。下面的配置应该做的伎俩还有:

Your configuration seems overly complex. The following configuration should do the trick as well:

container.RegisterMvcControllers(Assembly.GetExecutingAssembly());

container.RegisterMvcAttributeFilterProvider();

var appPath = AppDomain.CurrentDomain.BaseDirectory;

string[] files = Directory.GetFiles(appPath + "\\bin\\Plugins", "*",
    SearchOption.AllDirectories);

container.RegisterMvcControllers(
    from fileName in files
    select Assembly.LoadFrom(fileName)
);    

container.Verify();

您可以查询容器的配置看它所包含的内容。举例来说,这将查询所有注册的控制器:

You can query the container's configuration to look what it contains. For instance, this will query all registered controllers:

var registeredControllerTypes = (
    from registration in container.GetCurrentRegistrations()
    where typeof(IController).IsAssignableFrom(registration.ServiceType)
    select registration.ServiceType)
    .ToArray();

这篇关于简单的注射器用asp.net mvc的4,负载控制器从另一个组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆