在面向服务开发的时代,WebApi使 .Net 的接口开发脱离IIS束缚,更加灵活,轻量,下面我们逐步实现 WebApi的多版本管控,和插件式开发

实现功能:

  1. 通过搭建Windows服务构造WebApi站点
  2. 透过 http://*/api/v1/controller 的方式访问对应版本的 API
  3. 插件式开发,将写好接口的dll放置在站点指定文件夹中,就可以对外提供dll中的Api

以下是逐步实现的过程

三、插件式开发

当我们把WebApi服务器安装到服务器上后,再去修改已发布的Api需要重启服务,会造成访问中断

而且除Bug外修改已发布的Api是不合理的,我们也曾有修改用户正在使用的Api导致用户访问异常的状况

对于已有Api的逻辑变更,我们应通过发布不同版本来实现,在保留原有版本Api的情况先,发布新本版本Api,并通知用户逐步更新调用的Api版本

那如何在不重启服务的情况下发布新版的Api呢,以下就是WebApi接口平台组件式开发的过程

1.重写DefaultAssembliesResolver类

在项目中创建一个类 PluginsResolver继承DefaultAssembliesResolver进行方法重写

直接贴代码

using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Linq;
using System.Web.Http.Dispatcher;

namespace WebApiService
{
    public class PluginsResolver : DefaultAssembliesResolver
    {
        public override ICollection<Assembly> GetAssemblies()
        {
            //动态加载dll中的Controller,类似于插件服务,在WebApiConifg中添加配置
            // config.Services.Replace(typeof(IAssembliesResolver), new PluginsResolver());

            List<Assembly> assemblies = new List<Assembly>(base.GetAssemblies());
            string directoryPath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "ModuleApi");

            foreach (var fileName in Directory.GetFiles(directoryPath).Where(r => Path.GetExtension(r) == ".dll"))
            {
                try
                {
                    assemblies.Add(Assembly.LoadFrom(Path.Combine(directoryPath, fileName)));
                }
                catch
                {

                }
            }
            return assemblies;
        }
    }
}

创建一个文件夹 ModuleApi ,放在项目根目录,并保证他能够生成到运行目录中,你可以在文件夹中放一个静态文件达到这样的效果

2.修改启动类

using Microsoft.Owin;
using Owin;
using System.Web.Http;
using System.Web.Http.Dispatcher;

[assembly: OwinStartup(typeof(WebApiService.Startup))]

namespace WebApiService
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // 有关如何配置应用程序的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkID=316888
            var config = new HttpConfiguration();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
                );
            config.Services.Replace(typeof(IAssembliesResolver), new PluginsResolver());//此处是插件式开发的 配置,在此时还未实现先注释
            config.Services.Replace(typeof(IHttpControllerSelector), new VersionnControllerSelector(config));//此处是多版本管控的配置,在此时还未实现先注释
            app.UseWebApi(config);
        }
    }
}

ok,下边我们测试一下

新建一个项目 ApiV101

引入 Nuget 包 Microsoft.AspNet.WebApi.Core

创建 GetVerController->V101 文件夹

在文件夹中创建接口

接口代码

using System.Web.Http;

namespace ApiV101.Controller.V101
{
    public class GetVerController : ApiController
    {
        public string GetVer()
        {
            return "This V101 Api!";
        }
    }
}

好的,我们生成项目ApiV101得到 ApiV101.dll文件

现在运行 WebApi平台

访问 http://127.0.0.1:8088/Api/V101/GetVer  返回 

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<Error>
<Message>No HTTP resource was found that matches the request URI 'http://127.0.0.1:8088/Api/V101/GetVer'.</Message>
<MessageDetail>No controller was selected to handle this request.</MessageDetail>
</Error>

因为我们还没把组件放到组件文件夹中

现在我们吧 生成的 ApiV101.dll文件 放到 WebApi平台的 ModuleApi文件夹中

再次访问 http://127.0.0.1:8088/Api/V101/GetVer  返回 This V101 Api!

好了,我们的组件式开发就完成啦!

完结撒花!

注意,一定要保留好接口文档,放在合适位置,如果懒得写,可以使用 Microsoft.AspNet.WebApi.HelpPage生成接口文档

附录

WebApi多版本管控和插件式开发(一)——WebApi服务搭建

WebApi多版本管控和插件式开发(二)——WebApi多版本管控

WebApi多版本管控和插件式开发(三)——WebApi插件式开发

附上源码:https://download.csdn.net/download/xy596356456/12864960

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐