Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Aspect-oriented programming of WebApi client

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

Aspect-oriented programming of .net

.net server application AOP is very common, and there are AOP everywhere in new frameworks such as Asp.net MVC and Asp.net WebApi. We can "cut" a service method into many aspects, such as log surface, verification surface, request processing, interface business implementation, and so on. Some aspects can be written using filter features (FilterAttribute), and then it is very convenient and featured, for ordinary engineers. You only need to focus on implementing the interface business implementation.

In the age of popular SOAP, there is little need for a strong Client, but in today's restful era, .net also has a HttpClient, which can be said to match Asp.net WebApi. Although HttpClient has a qualitative change compared to WebClient in the http interface request, the shadow of webApi,AOP on the server side is gone.

Let HttpClient support AOP

By allowing HttpClient to support AOP, it means to encapsulate HttpClient and create a client framework based on HttpClient to introduce the concept of AOP into the new client framework. We can briefly analyze the basic things that HttpClient requests an interface to do, and at the same time "cut" these things into multiple aspects. The Http request is roughly as follows:

{Method} {Path}? query1= {value1} & query2= {value2} {Header1} {Header2} {body}

I can cut out a few sides:

Method and path processing

Query parameter processing

Request body content processing

Send a request

Reply result processing

The framework we have made needs to encapsulate 1, 2, 3, and 5 as faces, and call HttpClient to complete the fourth action.

The Choice of AOP basic Framework

You can refer to this blog: NET AOP (1) Seven ways to implement AOP

Here, I chose Castle,Castle to dynamically create and instantiate the implementation class of the interface, intercept the interface method before calling it, and set the return result.

According to this interception function, we can collect the configuration of each side, assemble it into a http request message, and then call HttpClient to send the request, and then let the fifth side process the reply result. Based on Castle and HttpClient, we can encapsulate a new HttpClient framework that supports AOP, which is named WebApiClient here.

WebApiClient design

Now, our goal is to make calling WebApi declare interfaces, parameters, return values, request method and path (surface) properties, return result processing (surface) properties, and request content (surface) properties, just like writing WebApi. Then the framework helps us implement and get the examples, and then we can call these interfaces.

Step1: declare an interface

Namespace Demo {[JsonReturn] [HttpHost ("http://www.mywebapi.com")] public interface MyWebApi {[HttpGet (" / webapi/ {type} / about ")] / / GET webapi/typeValue/about Task GetAboutAsync (string type); [HttpGet (" / webapi/user ")] / / GET webapi/user?userName=aa&nickName=bb&&BeginTime=cc&EndTime=dd Task GetUserAsync (string userName, string nickName, TimeFilter timeFilter) [HttpPut ("/ webapi/user")] / / PUT webapi/user Task UpdateUserAsync ([JsonContent] UserInfo loginInfo); [HttpDelete ("/ webapi/user")] / / DELETE webapi/user?id=idValue Task DeleteUserAsync (string id); [HttpDelete ("/ webapi/user/ {id}")] / / DELETE webapi/user/idValue Task DeleteUser2Async (string id);}}

Step2: get the interface reality and call

MyWebApi = WebApiClient.HttpApiClient () .GetHttpApi myWebApi.GetAboutAsync (myWebApi.UpdateUserAsync (UserInfo {UserName =, Password = myWebApi.DeleteUser2Async (id: Main (

More noodles of WebApiClient

We can add more customization aspects to the framework, such as logging, unified verification of return results, and so on, which are not covered, but the idea is still the same.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report