ASP.NET Web.Api插件架构 [英] ASP.NET Web.Api plugin architecture

查看:83
本文介绍了ASP.NET Web.Api插件架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您能否提供我一些文章或code样本有关网页API插件架构?

Can you suggest me some articles or code samples about plugin architecture in web api?

目前我正在考虑这样的场景:有1,集中API网关,在每一个客户端发送请求,并在Plugins文件夹不同的应用程序控制器。如果有人想增加新的服务,把它写到自己的控制器,并放入插件文件夹的DLL文件。

Currently I'm thinking about this scenario: to have 1, centralized api gateway, where every client sends request, and have different applications controllers in Plugins folder. If someone wants to add new service, writes it's own controllers and puts dll files in Plugin folder.

推荐答案

有关在运行时定位器类,你可以写一个程序集解析器,是这样的。

For locating controller classes at run time, you can write an assembly resolver, like this.

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

        // Add all plugin assemblies containing the controller classes
        assemblies.Add(Assembly.LoadFrom(@"C:\Plugins\MyAssembly.dll"));

        return assemblies;
    }
}

然后,添加此行到注册方法 WebApiConfig

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

使用此,则请求将仍然需要被发送到各个控制器即使控制器类可以来自在插件文件夹中的程序集。例如,如果在MyAssembly.dll程序插件文件夹中包含 CarsController ,该URI来打这个控制器将/ API /汽车。

With this, the request will still need to be sent to the individual controller even though the controller classes can come from assemblies in the plugin folder. For example, if MyAssembly.dll in the plugins folder contains CarsController, the URI to hit this controller will be /api/cars.

这篇关于ASP.NET Web.Api插件架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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