In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
I don't know if the name definition of the title is accurate, but what I'm trying to say is to use the Task feature to request multiple different interfaces at the same time, and then merge the data. I think the development of this scenario is no stranger to people who have connected to the interfaces of other companies, and I am also included in the list. I mostly use the most original asynchronous delegation method to deal with this scenario. Today, I wrote a Task that uses 4.5 new features to deal with this scenario; if you have any questions or good suggestions and share, please blog notice, thank you.
a. Project structure diagram
The definition of BaseClass under B.namespace Pm.V.PM_BLL is as follows:
View Code
Mainly read the configuration files of each business module during the instance, initialize some commonly used and common attribute information; secondly, create a virtual method _ GetArticles, note that the parameter in it is object, which will be mentioned again later.
c. The following is the code that requests the third-party business implementation class, which is tested using the
The 1.CnblogsClass class grabs the blog list information on the home page of the blog park.
View Code
. Note that: base ("") directly inherits the method of the parent class mentioned above to initialize the configuration information (of course, this may not be a knowledge point).
. Next, the HttpClient encapsulated by the PublicClass._HttpGet method in the implemented _ GetArticles method acquires the data of the blog park.
. Parsed the returned data information (the regularities used here, some students may think that regularities may not be familiar with, you can refer to Baidu for analysis)
. Record the log, I am here to record the text log temporarily annotated, because I am afraid of grabbing too much information, I forgot to delete the occupied space
The above points are often encountered in the actual situation, and this processing step is worth learning for people who have never docked with a third-party interface.
The 2.HuJiangClass class grabs the data on the first page of .net in the blog garden. The steps and methods are the same as above. Please pay attention to the code section.
View Code
d. The main content to be talked about today is that the above is excessive. Let people understand some of the processing steps and simple encapsulation of interfacing with third-party interfaces. Here we will see the Post method in BlogsController in Pm.Api.Controllers space, such as:
View Code
1. First of all, var baseRequest = Newtonsoft.Json.JsonConvert.DeserializeObject (param) is used to deserialize for the first time to get the basic information such as account number, token, etc.
two。 Commonly used interface forms use the name of the parameter node to determine the method to be executed, or mark the name of the method directly in the node value, so there is such a sentence switch (baseRequest.FunName) to determine the direction of the program.
3. Get the data that the real caller post gives to the interface in this reverse sequence (the base class is used in the first reverse sequence above, and the base class contains the attributes needed for verification. Why not use the object in the first reverse sequence directly, because the values to be passed to the later method parameters are encapsulated in the subclass)
4. Begin to define the number of Task tasks, generally based on the first few of several third-party interfaces. Task [] guarantees the amount of tasks generated later.
5.Task.Factory.StartNew (dataType._GetArticles, r_GetArticles) method to create the task. What we want to say here is that dataType._GetArticles is the method defined by the above-mentioned request third-party interface. The value to be passed by r_GetArticles is object, which is also the fixed parameter type of StartNew. Then the created task is stored through Task [].
6.Task.WaitAll (tasks, 1000 * 1 * 30); this WaitAll comes with it, which means how many milliseconds to wait for the task to execute. The first parameter is the task array, which is in the form of an array, and the second is the waiting time in millisecond units.
7. Finally, foreach (var task in tasks) is used to loop the results returned by task.Result.
Then there is a screenshot of the effect DataType showing different data sources:
That's about it. I don't know if what I'm saying is clear. This is the business logic that handles docking different interfaces of third parties, and it's also the method of using task to process in parallel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.