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

3 篇文章 0 订阅

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

实现功能:

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

以下是逐步实现的过程

 

一、搭建WebApi站点

1.1创建 WebApi项目

      使用VS创建一个空的控制台应用程序,使用Nuget引入以下包

            1.Microsoft.AspNet.WebApi.Core

            2.Microsoft.AspNet.WebApi.OwinSelfHost

     以上两个Package均有依赖项,同步引入

     创建Startup类 

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

[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);
        }
    }
}

      然后在入口(Main函数)启动服务

using Microsoft.Owin.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WebApiService
{
    class Program
    {
        static void Main(string[] args)
        {
            WebApp.Start<Startup>("http://127.0.0.1:8088");
            Console.WriteLine("Service Started!");
            Console.Read();
        }
    }
}

       此时我们的WebApi站点已经搭建好了

       我们写个接口试一下

       根据刚刚我们启动类中的规则,我们的接口要写在项目根目录 Controller文件夹中

using System.Web.Http;

namespace WebApiService.Controller
{
    public class GetDataController : ApiController
    {
        public string GetData()
        {
            return "Hello Word!";
        }
    }
}

      启动程式,控制台显示

Service Started!

      浏览器访问 http://127.0.0.1:8088/Api/GetData

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">Hello Word!</string>

      得到接口内容

      此时我们的WebApi服务已经搭建好了

1.2 将 WebApi服务安装到 Windows服务中

      项目->右键->添加->新建项 选择Windows服务 

      将Windows服务加入到项目后,双击打开设计界面,在设计界面右键选择添加安装程序

     根据需要修改安装程序的配置

     serviceProcessInstaller1 :中可以配置 安装时的属性,默认安装时选择安装账户

     WebApiServiceInstaller:中可以配置服务名称,启动类型等信息,默认手动启动

    修改 Windows服务 的启动,关闭事件

using Microsoft.Owin.Hosting;
using System.ServiceProcess;

namespace WebApiService
{
    partial class WebApiService : ServiceBase
    {
        public WebApiService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            // TODO: 在此处添加代码以启动服务。
            WebApp.Start<Startup>("http://127.0.0.1:8088");
        }

        protected override void OnStop()
        {
            // TODO: 在此处添加代码以执行停止服务所需的关闭操作。
        }
    }
}

    修改 入口(Main函数)

using System.ServiceProcess;

namespace WebApiService
{
    class Program
    {
        static void Main(string[] args)
        {
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[]
            {
                new WebApiService () //此处是我们的windows服务类名称
            };
            ServiceBase.Run(ServicesToRun);
        }
    }
}

   生成项目

   在VS安装目录或网上找一个 InstallUtil.exe 文件丢到生成目录中

   Cmd 进入生成目录 执行  InstallUtil.exe WebApiService.exe 进行安装

   

D:\Younger\WebApiService\WebApiService\bin\Debug>InstallUtil.exe WebApiService.e
xe
Microsoft (R) .NET Framework Installation Utility Version 4.0.30319.33440
Copyright (C) Microsoft Corporation.  著作權所有,並保留一切權利。


正在執行交易性的安裝。

正在開始安裝程式的安裝階段。
請參閱 D:\Younger\WebApiService\WebApiService\bin\Debug\WebApiService.exe 組件進
度的記錄檔內容。
檔案是位於 D:\Younger\WebApiService\WebApiService\bin\Debug\WebApiService.Instal
lLog。
正在安裝組件 'D:\Younger\WebApiService\WebApiService\bin\Debug\WebApiService.exe
'。
受影響的參數為:
   logtoconsole =
   logfile = D:\Younger\WebApiService\WebApiService\bin\Debug\WebApiService.Inst
allLog
   assemblypath = D:\Younger\WebApiService\WebApiService\bin\Debug\WebApiService
.exe
正在安裝服務 WebApiService...
已經成功安裝服務 WebApiService。
正在記錄檔 Application 中建立 EventLog 來源 WebApiService...

安裝階段已經成功完成,正在開始認可階段。
請參閱 D:\Younger\WebApiService\WebApiService\bin\Debug\WebApiService.exe 組件進
度的記錄檔內容。
檔案是位於 D:\Younger\WebApiService\WebApiService\bin\Debug\WebApiService.Instal
lLog。
正在認可組件 'D:\Younger\WebApiService\WebApiService\bin\Debug\WebApiService.exe
'。
受影響的參數為:
   logtoconsole =
   logfile = D:\Younger\WebApiService\WebApiService\bin\Debug\WebApiService.Inst
allLog
   assemblypath = D:\Younger\WebApiService\WebApiService\bin\Debug\WebApiService
.exe

已經成功完成認可階段。

已經完成交易性的安裝。

D:\Younger\WebApiService\WebApiService\bin\Debug>

   安装成功

浏览器调用接口试一下  可以访问

好的,先到这里吧,下一篇接着写!

 

附录

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

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

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

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

[GitHub][C#]WebApi多版本管控和插件式开发

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值