asp.net webapi 自托管插件式服務


  webapi問世已久,稀里糊塗的人哪它都當mvc來使,畢竟已mvc使用級別的經驗就可以應對webapi。

  webapi和mvc在asp.net5時代合體了,這告訴我們,其實 它倆還是有區別的,要不現在也不會取兩個名字,但是由於本人歸納總結能力較差,各種不同也無法一一列出了。

 

  在webapi中 HelpPage是個突出而又實用的東西,它尼瑪會把我們code中的注釋生成xml,然后以web頁面的方式把接口文檔展示出來,這尼瑪無形就下崗一批文案了,以社會責任感角度來講,ms的這個HelpPage挺不地道的,但我表示就喜歡這樣的東西。。

  步驟也很簡單:

    1、negut 搜 helppage ,認准Id是Microsoft.AspNet.WebApi.HelpPage就Install它;

    2、新建一個xml文件,在項目屬性-生成中 xml文檔文件路徑指向它 勾選;

    3、引入生成的HelpPage中HelpPageConfig.cs中,此代碼“config.SetDocumentationProvider(new XmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/App_Data/ApiDocument.xml")));”,修改xml路徑 然后解注釋;

    4、最后一生成就會發現你寫的並沒有卵用的注釋被一坨一坨塞入了那個xml文件當中,當訪問help/index 就會看到配圖如下:

    (附詳細參考一份:http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/creating-api-help-pages)

    

 

    5、Negut認准Id:WebApiTestClient,Install它,你會發現多了一個js、一個cs、兩個cshtml文件,都是TestClient開頭的,別處用的時候不要忘了靠它。

      Api.cshtml文件中加入兩行代碼   

@Html.DisplayForModel("TestClientDialogs")
@section Scripts{
    <link href="~/Areas/HelpPage/TestClient.css" rel="stylesheet" />
    @Html.DisplayForModel("TestClientReferences")
}

      點入某一個接口以后,戳那個Test Api,你的項目就可以進行接口測試了。此刻你會更堅定自己身為碼農的存在,我們並不是coder,我們不生產代碼,我們只是代碼的搬運工。

    (附詳細參考一份:https://blogs.msdn.microsoft.com/yaohuang1/2012/12/02/adding-a-simple-test-client-to-asp-net-web-api-help-page/)

    

 

 

  微幅扯淡之后,言入主題,畢竟文章精髓的要藏在后面。

  從文章標題來看,就可以分成兩個部分,webapi可以寄宿iis、console、winform、winservice等,插件開發大家也不陌生,所以精髓的部分也言簡意賅了,這也是很無奈的事情啊。

  1、使用Self Host自托管方式,negut-Id:Microsoft.AspNet.WebApi.SelfHost

    1.1 首先准備一個contrller和model,代碼很簡單

public class Product
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Category { get; set; }
        public decimal Price { get; set; }
    }

public class ProductsController : ApiController
    {
        Product[] products = new Product[]  
        {  
            new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 },  
            new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M },  
            new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M }  
        };

        public IEnumerable<Product> GetAllProducts()
        {
            return products;
        }

        public Product GetProductById(int id)
        {
            var product = products.FirstOrDefault((p) => p.Id == id);
            if (product == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
            return product;
        }

        public IEnumerable<Product> GetProductsByCategory(string category)
        {
            return products.Where(p => string.Equals(p.Category, category,
                    StringComparison.OrdinalIgnoreCase));
        }
    }

    1.2 寄宿於console,監聽port,配置route,啟動service,一目了然

 class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("請輸入監聽端口");
            string port = Console.ReadLine();

            var config = new HttpSelfHostConfiguration(string.Format("http://localhost:{0}", port ?? "8080"));

            config.Routes.MapHttpRoute(
                "API Default", "api/{controller}/{id}",
                new { id = RouteParameter.Optional });

            using (HttpSelfHostServer server = new HttpSelfHostServer(config))
            {
                server.OpenAsync().Wait();
                Console.WriteLine("Press ESC to quit.");
                while (
               !Console.ReadKey().Key.Equals(ConsoleKey.Escape))
                { }
            }
        }
    }

    1.3 用winservice來承載webapi服務

      首先: Install-Package Microsoft.AspNet.WebApi.OwinSelfHost  (提到Asp.Net Owin一句話可形容,Asp.Net5跨平台,此逼功不可沒啊)

      新建好winserver項目后就可以新建一個Owin的啟動類別,在其當中構建webapi的配置

public class Startup
    {
        public void Configuration(IAppBuilder appBuilder)
        {
            // Configure Web API for self-host. 
            HttpConfiguration config = new HttpConfiguration();
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
            //config.Services.Replace(typeof(IAssembliesResolver), new PluginsResolver());
            appBuilder.UseWebApi(config);
        }
    }

      在service中啟動,over。

private IDisposable _apiserver = null;
        protected override void OnStart(string[] args)
        {
            //Services URI 
            string serveruri = string.Format("http://localhost:{0}/", System.Configuration.ConfigurationManager.AppSettings["port"]);

            // Start OWIN host  
            _apiserver = WebApp.Start<Startup>(url: serveruri);
            base.OnStart(args);
        }

        protected override void OnStop()
        {
            if (_apiserver != null)
            {
                _apiserver.Dispose();
            }
            base.OnStop();
        }

  2、插件式服務,看似逼格很高,其實只是標題黨,畫龍點睛的代碼到了...

    重寫DefaultAssembliesResolver的GetAssemblies,反射加載指定文件下的dll中的controller,在WebApiConifg添加其實現

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, "dynamic_services");
            string[] files = Directory.GetFiles(directoryPath);
            foreach (var fileName in files)
            {
                assemblies.Add(Assembly.LoadFrom(Path.Combine(directoryPath, fileName)));
            }
            return assemblies;
        }
    }

    需要什么接口服務直接寫好dll仍進dynamic_services文件夾下就有了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM