Introduction
In this article, I will use WebApiClient.
WebApiClient
| Name | Description |
| WebApiClient.JIT | Using Emit to create the proxy class of http request interface at runtime. |
| WebApiClient.AOT | Support all platforms, including the platform that requires AOT, inserting proxy class IL instructions from http request interface to output assembly at compile time. |
Here I will use WebApiClient.JIT to demonstrate how to use. Let's begin!
Create Some APIs
Here I use ASP.NET Core WebAPI to creat some RESTful APIs.
- [Route("api/[controller]")]
- public class PersonsController : Controller
- {
- // GET: api/persons
- [HttpGet]
- public IEnumerable<Person> Get()
- {
- return new List<Person>
- {
- new Person{Id = 1 , Name = "catcher wong"},
- new Person{Id = 2 , Name = "james"}
- };
- }
- // GET api/persons/5
- [HttpGet("{id}")]
- public Person Get(int id)
- {
- return new Person { Id = id, Name = "name" };
- }
- // POST api/persons
- [HttpPost]
- public Person Post([FromBody]Person person)
- {
- if (person == null) return new Person();
- return new Person { Id = person.Id, Name = person.Name };
- }
- // PUT api/persons/
- [HttpPut]
- public string Put([FromBody]int id)
- {
- return $"put {id}";
- }
- // DELETE api/persons/5
- [HttpDelete("{id}")]
- public string Delete(int id)
- {
- return $"del {id}";
- }
- }
Create an interface named IPersonApiClient which inherit from IHttpApiClient.
- public interface IPersonApiClient : IHttpApiClient { }
Add some methods that need to call APIs.
- [HttpGet("/api/persons")]
- ITask<List<Person>> GetPersonsAsync();
A request URL can be updated dynamically using replacement blocks and parameters on the method. A replacement block is an alphanumeric string surrounded by { and }.
- [HttpGet("/api/persons/{id}")]
- ITask<Person> GetPersonAsync(int id);
When our requst parameters should in request body, we can use some attributes to specify the content, such as JsonContent, FormContent .etc.
- [HttpPost("/api/persons")]
- ITask<Person> AddPersonAsync([JsonContent]Person person);
- public interface IPersonApiClient : IHttpApiClient
- {
- [HttpGet("/api/persons")]
- ITask<List<Person>> GetPersonsAsync();
- [HttpGet("/api/persons/{id}")]
- ITask<Person> GetPersonAsync(int id);
- [HttpPost("/api/persons")]
- ITask<Person> AddPersonAsync([JsonContent]Person person);
- [HttpPut("/api/persons")]
- ITask<string> EditPersonAsync([JsonContent]int id);
- [HttpDelete("/api/persons/{id}")]
- ITask<string> DeletePersonAsync(int id);
- }
The next step is how to retrieve the response of the request.
We should create a client first. After creating , what we need to do is call the methods we declared in the interface.
- //specify the config
- var config = new HttpApiConfig
- {
- HttpHost = new Uri("http://localhost:9999"),
- };
- var client = HttpApiClient.Create<IPersonApiClient>(config);
- var persons = await client.GetPersonsAsync();
- Console.WriteLine("GetPersonsAsync result:");
- foreach (var item in persons)
- {
- Console.WriteLine($"{item.Id}-{item.Name}");
- }
- var person = await client.GetPersonAsync(1000);
- Console.WriteLine("GetPersonAsync result:");
- Console.WriteLine($"{person.Id}-{person.Name}");
- var newPerson = new Person { Id = 999, Name = "999" };
- var postResult = await client.AddPersonAsync(newPerson);
- Console.WriteLine("AddPersonAsync result:");
- Console.WriteLine($"{postResult.Id}-{postResult.Name}");
- var editResult = await client.EditPersonAsync(1);
- Console.WriteLine("EditPersonAsync result:");
- Console.WriteLine($"{editResult}");
- var delResult = await client.DeletePersonAsync(1);
- Console.WriteLine("DeletePersonAsync result:");
- Console.WriteLine($"{delResult}");
Here is the source code you can find in my github page .
In this article, I showed you the basic usage of WebApiClient.
I hope this article can help you!


Sai Kumar KoonaPosted Sep 7, 2018, 6:07 PM EDT
Thanks for sharing...