使用.Net6中的WebApplication打造最小API
.net6在preview4時給我們帶來了一個新的API:WebApplication,通過這個API我們可以打造更小的輕量級API服務。今天我們來嘗試一下如何使用WebApplication設計一個小型API服務系統(tǒng)。
環(huán)境準備
- .NETSDK v6.0.0-preview.5.21355.2
- Visual Studio 2022 Preview
首先看看原始版本的WebApplication,官方已經(jīng)提供了樣例模板,打開我們的vs2022,選擇新建項目選擇asp.net core empty,framework選擇.net6.0(preview)點擊創(chuàng)建,即可生成一個簡單的最小代碼示例:
如果我們在.csproj里在配置節(jié)PropertyGroup增加使用C#10新語法讓自動進行類型推斷來隱式的轉換成委托,則可以更加精簡:
<PropertyGroup> <TargetFramework>net6.0</TargetFramework> <LangVersion>preview</LangVersion> </PropertyGroup>
當然僅僅是這樣,是無法用于生產的,畢竟不可能所有的業(yè)務單元我們塞進這么一個小小的表達式里。不過借助WebApplication我們可以打造一個輕量級的系統(tǒng),可以滿足基本的依賴注入的小型服務。比如通過自定義特性類型,在啟動階段告知系統(tǒng)為哪些服務注入哪些訪問路徑,形成路由鍵和終結點。具體代碼如下:
首先我們創(chuàng)建一個簡易的特性類,只包含httpmethod和path:
[AttributeUsage(AttributeTargets.Method)] public class WebRouter : Attribute { public string path; public HttpMethod httpMethod; public WebRouter(string path) { this.path = path; this.httpMethod = HttpMethod.Post; } public WebRouter(string path, HttpMethod httpMethod) { this.path = path; this.httpMethod = httpMethod; } }
接著我們按照一般的分層設計一套DEMO應用層/倉儲服務:
public interface IMyService { Task<MyOutput> Hello(MyInput input); } public interface IMyRepository { Task<bool> SaveData(MyOutput data); } public class MyService : IMyService { private readonly IMyRepository myRepository; public MyService(IMyRepository myRepository) { this.myRepository = myRepository; } [WebRouter("/", HttpMethod.Post)] public async Task<MyOutput> Hello(MyInput input) { var result = new MyOutput() { Words = $"hello {input.Name ?? "nobody"}" }; await myRepository.SaveData(result); return await Task.FromResult(result); } } public class MyRepository : IMyRepository { public async Task<bool> SaveData(MyOutput data) { Console.WriteLine($"保存成功:{data.Words}"); return await Task.FromResult(true); } }
最后我們需要將我們的服務接入到WebApplication的map里,怎么做呢?首先我們需要定義一套代理類型用來反射并獲取到具體的服務類型。這里為了簡單的演示,我只設計包含一個入?yún)⒑蜎]有入?yún)⒌那闆r下:
public abstract class DynamicPorxy { public abstract Delegate Instance { get; set; } } public class DynamicPorxyImpl<Tsvc, Timpl, Tinput, Toutput> : DynamicPorxy where Timpl : class where Tinput : class where Toutput : class { public override Delegate Instance { get; set; } public DynamicPorxyImpl(MethodInfo method) { Instance = ([FromServices] IServiceProvider sp, Tinput input) => ExpressionTool.CreateMethodDelegate<Timpl, Tinput, Toutput>(method)(sp.GetService(typeof(Tsvc)) as Timpl, input); } } public class DynamicPorxyImpl<Tsvc, Timpl, Toutput> : DynamicPorxy where Timpl : class where Toutput : class { public override Delegate Instance { get; set; } public DynamicPorxyImpl(MethodInfo method) { Instance = ([FromServices] IServiceProvider sp) => ExpressionTool.CreateMethodDelegate<Timpl, Toutput>(method)(sp.GetService(typeof(Tsvc)) as Timpl); } }
接著我們創(chuàng)建一個代理工廠用于創(chuàng)建服務的方法委托并創(chuàng)建代理類型實例返回給調用端
public class DynamicPorxyFactory { public static IEnumerable<(WebRouter, DynamicPorxy)> RegisterDynamicPorxy() { foreach (var methodinfo in DependencyContext.Default.CompileLibraries.Where(x => !x.Serviceable && x.Type != "package") .Select(x => AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(x.Name))) .SelectMany(x => x.GetTypes().Where(x => !x.IsInterface && x.GetInterfaces().Any()).SelectMany(x => x.GetMethods().Where(y => y.CustomAttributes.Any(z => z.AttributeType == typeof(WebRouter)))))) { var webRouter = methodinfo.GetCustomAttributes(typeof(WebRouter), false).FirstOrDefault() as WebRouter; DynamicPorxy dynamicPorxy; if (methodinfo.GetParameters().Any()) dynamicPorxy = Activator.CreateInstance(typeof(DynamicPorxyImpl<,,,>).MakeGenericType(methodinfo.DeclaringType.GetInterfaces()[0], methodinfo.DeclaringType, methodinfo.GetParameters()[0].ParameterType , methodinfo.ReturnType), new object[] { methodinfo }) as DynamicPorxy; else dynamicPorxy = Activator.CreateInstance(typeof(DynamicPorxyImpl<,,>).MakeGenericType(methodinfo.DeclaringType.GetInterfaces()[0], methodinfo.DeclaringType, methodinfo.ReturnType), new object[] { methodinfo }) as DynamicPorxy; yield return (webRouter, dynamicPorxy); } } }
internal class ExpressionTool { internal static Func<TObj, Tin, Tout> CreateMethodDelegate<TObj, Tin, Tout>(MethodInfo method) { var mParameter = Expression.Parameter(typeof(TObj), "m"); var pParameter = Expression.Parameter(typeof(Tin), "p"); var mcExpression = Expression.Call(mParameter, method, Expression.Convert(pParameter, typeof(Tin))); var reExpression = Expression.Convert(mcExpression, typeof(Tout)); return Expression.Lambda<Func<TObj, Tin, Tout>>(reExpression, mParameter, pParameter).Compile(); } internal static Func<TObj, Tout> CreateMethodDelegate<TObj, Tout>(MethodInfo method) { var mParameter = Expression.Parameter(typeof(TObj), "m"); var mcExpression = Expression.Call(mParameter, method); var reExpression = Expression.Convert(mcExpression, typeof(Tout)); return Expression.Lambda<Func<TObj, Tout>>(reExpression, mParameter).Compile(); } }
最后我們創(chuàng)建WebApplication的擴展方法來調用代理工廠以及注入IOC容器:
public static class WebApplicationBuilderExtension { static Func<string, Delegate, IEndpointConventionBuilder> GetWebApplicationMap(HttpMethod httpMethod, WebApplication webApplication) => (httpMethod) switch { (HttpMethod.Get) => webApplication.MapGet, (HttpMethod.Post) => webApplication.MapPost, (HttpMethod.Put) => webApplication.MapPut, (HttpMethod.Delete) => webApplication.MapDelete, _ => webApplication.MapGet }; public static WebApplication RegisterDependencyAndMapDelegate(this WebApplicationBuilder webApplicationBuilder, Action<IServiceCollection> registerDependencyAction, Func<IEnumerable<(WebRouter webRouter, DynamicPorxy dynamicPorxy)>> mapProxyBuilder) { webApplicationBuilder.Host.ConfigureServices((ctx, services) => { registerDependencyAction(services); }); var webApplication = webApplicationBuilder.Build(); mapProxyBuilder().ToList().ForEach(item => GetWebApplicationMap(item.webRouter.httpMethod, webApplication)(item.webRouter.path, item.dynamicPorxy.Instance)); return webApplication; } }
當然包括我們的自定義容器注入方法:
public class MyServiceDependency { public static void Register(IServiceCollection services) { services.AddScoped<IMyService, MyService>(); services.AddScoped<IMyRepository, MyRepository>(); } }
最后改造我們的program.cs的代碼,通過擴展來注入容器和代理委托并最終生成路由-終結點:
await WebApplication.CreateBuilder().RegisterDependencyAndMapDelegate(MyServiceDependency.Register,DynamicPorxyFactory.RegisterDynamicPorxy).RunAsync("http://*:80");
這樣這套小型API系統(tǒng)就基本完成了,可以滿足日常的依賴注入和獨立的業(yè)務單元類型編寫,最后我們啟動并調用一下,可以看到確實否符合我們的預期成功的調用到了應用服務并且倉儲也被正確的執(zhí)行了:
到此這篇關于使用.Net6中的WebApplication打造最小API的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
ASP.NET MVC4入門教程(九):查詢詳細信息和刪除記錄
本文主要是MVC實戰(zhàn),介紹如何查詢和刪除信息,進行到這一步,您已經(jīng)有一個完整的MVC案例了,創(chuàng)建、 讀取、 更新、 刪除和搜索等功能也都做了演示。2016-04-04ASP.NET MVC4入門教程(六):驗證編輯方法和編輯視圖
本文主要演示如何修改控制器和視圖以及處理POST的請求,以達到實現(xiàn)我們想要的功能。2016-04-04在ASP.NET 2.0中操作數(shù)據(jù)之七十一:保護連接字符串及其它設置信息
默認情況下,ASP.NET應用程序數(shù)據(jù)庫連接字符串、用戶名和密碼等敏感信息都是保存在根目錄的web.config文件中,我們可以使用加密算法對其加密,從而保證這些敏感信息不被泄漏。2016-05-05在ASP.NET 2.0中操作數(shù)據(jù)之六十八:為DataTable添加額外的列
本文介紹并使用TableAdapter向DataTable添加新的一列的方法和步驟,任何時候只要重新運行TableAdapter設置向導,用戶所做的所有定制都要被覆蓋,為避免出現(xiàn)這種情況,我們建議直接修改存儲過程。2016-05-05在ASP.NET 2.0中操作數(shù)據(jù)之十四:使用FormView 的模板
前面介紹了GridView和DetailsView控件可以使用TemplateField來自定義輸出,但是呈現(xiàn)的樣式還是一種四四方方的格子狀。當我們想完全自定義的時候,他們就愛莫能助了,這時我們就可以使用FormView控件來實現(xiàn)我們想要的效果了。2016-05-05在ASP.NET 2.0中操作數(shù)據(jù)之十九:給編輯和新增界面增加驗證控件
本文主要介紹如何對GridView和DetailsView的新增、編輯功能進行完善,將原來自動生成的綁定列轉換為模板列,進而增加驗證控件,有助于更多了解ASP.NET 2.0中新的特性。2016-05-05在ASP.NET 2.0中操作數(shù)據(jù)之二十:定制數(shù)據(jù)修改界面
本文主要介紹如何對GridView的編輯界面進行定制,使GridView在編輯時具有DropDownList和RadioButtonList控件,提供更人性化的界面。2016-05-05在ASP.NET 2.0中操作數(shù)據(jù)之六十:創(chuàng)建一個自定義的Database-Driven Site Map Provid
ASP.NET 2.0的site map是建立在provider模式的基礎上的,因此我們可以創(chuàng)建一個自定義的site map provider,從數(shù)據(jù)庫或某個層來獲取數(shù)據(jù)。本文就詳解介紹如何自定義的site map provider動態(tài)的獲取數(shù)據(jù),替代先前通過"硬編碼"的方式添加到Web.sitemap文件的方法。2016-05-05