如何将新的DLL位置添加到的WebAPI的控制器发现? [英] How to add new DLL locations to WebAPI's controller discovery?

查看:854
本文介绍了如何将新的DLL位置添加到的WebAPI的控制器发现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP.NET的WebAPI有发现,即使这些DLL没有被引用外部DLL ApiController班多AP preciated能力。例如,我可能有MyWebApiProject有一组ApiControllers的。然后我可以创建一个完全独立的项目称为MyApiProjectPlugin包含ApiController类也。我已经能够在MyApiProjectPlugin.dll文件与第一MyApiProject.dll和原来的项目会发现在插件项目中的所有控制器添加到bin文件夹。我真的很喜欢这种能力。

ASP.NET WebAPI has a much appreciated ability to discover ApiController classes in external DLLs even if those DLLs are not referenced. For example, I may have MyWebApiProject that has a set of ApiControllers. I could then create a completely separate project called MyApiProjectPlugin that contains ApiController classes also. I have been able to add the MyApiProjectPlugin.dll file to the bin folder with the first MyApiProject.dll and the original project will discover all the controllers in the plugin project. I really like that ability.

不过,我想这样做的是能够给插件项目添加到子目录下的bin文件夹内。像斌/插件。当我尝试这样做,原来MyApiProject无法发现这个插件的控制器。

However, What I would like to do is be able to add the plugin project to a sub directory inside of the bin folder. Something like bin/plugins. When I tried this, the original MyApiProject was unable to discover the plugin's controllers.

有一个简单的方法来获得的WebAPI寻找ApiController类在bin的子目录?如果我能避免从头开始重写控制器工厂,我想。

Is there a simple way to get WebAPI to look for ApiController classes in the bin's subdirectories? If I can avoid rewriting a controller factory from scratch I would like to.

推荐答案

您可以编写一个程序集解析器。

You can write an assembly resolver.

public class PluginsResolver : DefaultAssembliesResolver
{
    public override ICollection<Assembly> GetAssemblies()
    {
        List<Assembly> assemblies = new List<Assembly>(base.GetAssemblies());

        assemblies.Add(Assembly.LoadFrom(@"<Path>\MyApiProjectPlugin.dll"));

        return assemblies;
    }
}

在WebApiConfig注册方法,注册解析。

In the Register method in WebApiConfig, register the resolver.

config.Services.Replace(typeof(IAssembliesResolver), new PluginsResolver());

这篇关于如何将新的DLL位置添加到的WebAPI的控制器发现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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