一種類(lèi)似Retrofit聲明接口即可實(shí)現(xiàn)調(diào)用的WebApi客戶端框架
為.Net出力
java有okhttp,還在okhttp這上搞了一個(gè)retrofit,.net有HttpClient,但目前我沒(méi)有發(fā)現(xiàn)有類(lèi)似的retrofit框架。最近在搞mqtt的webApi封裝,輸出很多web api接口,給移動(dòng)端也有給后臺(tái)二次開(kāi)發(fā)使用的,所以有了搞一個(gè)類(lèi)似retrofit的.net實(shí)現(xiàn),項(xiàng)目叫WebApiClient,托管在https://github.com/xljiulang/WebApiClient
WebApiClient使用
一個(gè)原則:聲明它,然后調(diào)用它,不要實(shí)現(xiàn)它。

namespaceDemo
{
[JsonReturn]
[HttpHost("http://www.mywebapi.com")]publicinterfaceMyWebApi
{
[HttpGet("/webapi/{type}/about")]//GET webapi/typeValue/aboutTask> GetAboutAsync(stringtype);
[HttpGet("/webapi/user")]//GET webapi/user?userName=aa&nickName=bb&&BeginTime=cc&EndTime=ddTask> GetUserAsync(stringuserName,stringnickName, TimeFilter timeFilter);
[HttpPut("/webapi/user")]//PUT webapi/userTask>UpdateUserAsync([JsonContent] UserInfo loginInfo);
[HttpDelete("/webapi/user")]//DELETE? webapi/user?id=idValueTask> DeleteUserAsync(stringid);
[HttpDelete("/webapi/user/{id}")]//DELETE? webapi/user/idValueTask> DeleteUser2Async(stringid);
}
}
namespace Demo{? ? class Program? ? {? ? ? ? static async void Test()? ? ? ? {? ? ? ? ? ? var myWebApi = new WebApiClient.HttpApiClient().GetHttpApi();
await myWebApi.GetAboutAsync("typeValue");
await myWebApi.UpdateUserAsync(new UserInfo { UserName = "abc", Password = "123456" });
await myWebApi.DeleteUser2Async(id: "id001");
}
static void Main(string[] args)
{
Test();
Console.ReadLine();
}
}
}
歡迎加入技術(shù)QQ群:364595326