Introduction to the standard library of the OSS. Social WeChat project and the oss. social WeChat Project

Source: Internet
Author: User

Introduction to the standard library of the OSS. Social Project and the oss. social Project

After this week's efforts, OSS was finally completed last night. the standard library of the Social project is supported. Currently, you can.. net framework and. net core is called, and the calling method has also undergone some changes. Here I will share it briefly, mainly including the following parts:

·Changes after transplantation

·Relationship with OSS. Common and OSS. Http

·Introduction to asynchronous (Task)

 

I. Changes after transplantation

If you do not know about OSS. Social, you can refer to my previous articles for more information. The first change to this transplantation is true. net core project can provide direct support for the current use.. net standard library version 1.3, the corresponding framework supports 4.6, corresponding. the net core version is now basically supported. For the source code, see github. In the project, I provided a unit test project for Framework 4.6, and. the website project of netcoreapp1.0. Due to insufficient time, I did not perform all interface tests. If any bug is found, contact me in time.

The second is asynchronous support, which is also the biggest change this time. Almost all core methods under the Public Account Management Function (Offcial) module are converted to asynchronous mode, this is entirely due to the convenience provided by HttpClient and provides the most basic asynchronous support at the underlying IO layer.

You can call it as follows:

Of course, in many cases, the corresponding response information needs to be processed in a timely manner. For this synchronization requirement, I call the wait () method of the Task, it encapsulates a simple extension method WaitResult () and can be called directly as follows:

 

2. Relationship with OSS. Common, OSS. Http

  The two basic modules I wrote earlier on OSS. Common and OSS. Http are now integrated into the OSS series and are now supported by. Net Standard. The former is responsible for implementing global result entities, encryption algorithms, and basic helper classes. All returned objects in the interface method inherit the ResultMo object, which provides the IsSuccess attribute, Ret and Message information, unified the value passing and result judgment in the project, and unified judgment basis. There will be an article about it later, which is skipped here. If you want to use it in the project, you can directly install it through Nuget:

For OSS. http is mainly responsible for encapsulating Rest interface requests. The underlying layer has been migrated from HttpWebRequest to the HttpClient module. If you are interested, you can refer to my "complete OSS. the article "Http underlying HttpClient reconstruction and encapsulation" can also be installed through Nuget:

Iii. Introduction to asynchronous (Task)

1. Basic Introduction

Because a large number of async asynchronous keywords are used in this project, it may be slightly uncomfortable for those who use less in this case, here I will introduce the common usage of Asynchronization.

In my impression, Asynchronization started with Thread, Task, async, and wait keywords in the. net system. Its syntax is becoming more and more streamlined. In the Framework2.0 era, we may needNew Thread (Execute task). Start ()To create an asynchronous thread. Because frequent creation of system threads will cause a large amount of system resource consumption. net maintains a thread pool, which allows us to directly call the threads already maintained in the thread pool through ThreadPool. We can easilyThreadPool. QueueUserWorkItem (Execute task)Call the asynchronous thread.

Later. net provides Task. run () => {execution Task}) asynchronous call. By default, the thread in the thread pool is used and a Task object is returned, this object can be used to wait or determine the task completion status.

Although it is quite simple to use multi-threaded asynchronous operations at this time, sometimes asynchronous code must be processed independently, which may lead to a level relationship. So now we have the async and await keywords that allow us to write asynchronous code like a synchronous process. Below I will explain it with a simple example.

2. Usage:

Here I will share with you a simple example. If we have a requirement, we need to use the user's mobile phone number to request the user name in another system and push the text message to the user. Below is a simple example I wrote using asyn and await.

  

In this example, if you take await and asyn apart, the process is a complete synchronization process, but after adding it, it becomes asynchronous.

Here, we should mention that await does not immediately execute Asynchronization. It will only be processed in real asynchronous operations. In this example, that is, 1, 2, step 3 is executed synchronously, and step 4 and Step 5 is executed in the asynchronous thread. At the same time, when step 4 is executed, the UpdateUserInfo () method ends and returns

3. Exceptions:

For Asynchronous exceptions, I recommend this article about Exception Handling in async/await (I) in C #. Here is a question that needs to be emphasized, after Asynchronization, it is best to have subsequent operations on the returned Task, so that if an exception exists, it will be thrown in time. Otherwise, it will be triggered when the GC recycles resources. If no global exception is set, the application pool may crash.

 

If you are interested in my code, you can check my GitHub directly or follow the Public Account (OSSCoder ):

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.