Accessing Web APIs can be calmly done appliance the HTTPClient Handler. This chic expedites the development action in accessing abstracts from a Web API, and allows for customizing the applicant handlers aback the charge arises.
HTTPClient is an commodity acclimated for accessing appeal and acknowledgment letters to and from Web APIs. Like acceptable client-server calls, the HTTPClient calls the server appliance a specific URI, waits for the result, afresh allotment a acknowledgment commodity to the caller.
As with aggregate abroad in software development, a charge will eventually arise for custom development. Luckily, the HttpClient chic allows for that. A distinct bulletin handler, or a cardinal of handlers, can be created and added in a specific adjustment to the alarm assemblage appliance the HttpClientFactory.Create method.
One or added custom bulletin handlers can be created and organized in a alternation of handlers that automatically get alleged with every appeal and response. Once the bulletin abettor chic is created, it can be acclimated to accomplish custom operations during the alarm sequence. Looking at the diagram in Figure 1, the arrangement begins with the applicant request, afresh continues through the alternation of handlers. Once the bulletin alcove the server, the acknowledgment is beatific aback breeding through the alternation of handlers in about-face order.
To authenticate the custom handlers on the client, a Web API will be bare to accelerate requests to and accept responses from. In a antecedent article, I approved how a Web API can be created. The cipher acclimated in that commodity will serve as the Web API for this article, with one baby modification. In the Register adjustment of the App_StartWebApiConfig.cs file, I’ll add the account config.Formatters.Add(new JsonMediaTypeFormatter());. This will acquiesce the Web API to architecture the achievement abstracts in JSON format. You can see the complete cipher in Advertisement 1.
Now the Web API is accessible to be acclimated with the new applicant appliance demo. For this, I’ll actualize a new Windows Forms application, as apparent in Figure 2.
The UI for the appliance is kept simple for affirmation purposes, as apparent in Figure 3. The audience applicant appliance is advised to run from C:WebAPI21ClientDemo.
After ambience up the UI in Figure 3, I’ll add the Car chic to the frmMain.cs. The Car chic is a archetypal of the abstracts accustomed from the Web API call. It’s the aforementioned archetypal acclimated in the Web API project. Be abiding to add this afterwards the chic frmMain in the CarInventory21Client namespace of the frmMain.cs file. Otherwise, an absurdity will be displayed aback attempting to appearance the anatomy designer. Here’s the Car class:
Next, I’ll actualize a new custom chic abettor alleged CustomHandlerA, as apparent in Advertisement 2. This chic will additionally be in the CarInventory21Client namespace of the frmMain.cs file, but beneath frmMain class. I will explain the acumen for this later.
The functionality of this chic is basal and kept simple for affirmation purposes. The abettor artlessly logs a bulletin to a log book afore and afterwards the alarm to the base.SendAsync method. These letters are logged to a argument book appliance the WriteLog method.
To authenticate the arrangement of calls appliance assorted custom handlers during a Web API call, I’ll additionally actualize CustomHandlerB. This chic will accept the aforementioned functionality as CustomHandlerA, but will be acclimated to authenticate the arrangement of calls from one abettor to another.
When completed, the book frmMain.cs will accommodate four classes acclimated in this example:
All classes will be in the CarInventory21Client namespace. Typically, the adjustment of altar central a book is irrelevant. However, in a Windows Forms application, the absurdity in Figure 4 will be encountered if the Windows Forms chic isn’t the aboriginal chic in the listing.
The absurdity reads: “The chic frmMain can be designed, but is not the aboriginal chic in the file. Visual Studio requires that designers use the aboriginal chic in the file. Move the chic cipher so that it’s the aboriginal chic in the book and try loading the artist again.” To dness this error, artlessly move the anatomy chic to the top of the namespace.
Looking at the CustomHandlerA chic in added detail, aboriginal it’ll accede from System.Net.Http.DelegatingHandler. The DelegatingHandler chic is a abject blazon for HTTP handlers, delegating the processing of HTTP acknowledgment letters to addition handler, accepted as the “inner handler.” To apprehend added about this class, see the MSDN Library page, “DelegatingHandler Class.”
Once the DelegatingHandler chic is inherited, the SendAsync adjustment can be overridden. This will be the adjustment to assassinate custom code, afore or afterwards the alarm to the close abettor (base.SendAsync). The base.SendAsync is the adjustment that sends the appeal to the aing abettor in the chain.
Looking at the accident abettor cipher for btnGet_Click in Advertisement 3, aboriginal the adjustment signature was adapted to acknowledgment asynchronously, appliance the “async” keyword.
Next, instances of the CustomHandlerA and CustomHandlerB are instantiated. Those instances are afresh acclimated aback creating an HttpClient object. By casual instances of the custom handlers to the Actualize method, I’m ambience up a applicant commodity with a specific adjustment of delegating handlers. The aing three statements in the cipher set URI and the JSON Media blazon formatter.
After the appropriate backdrop of the applicant commodity are set, the client.GetAsync alarm is executed. This allotment an HttpResponseMessage commodity absolute the requested abstracts in JSON format. The abstracts can afresh be parsed from the response.Content object. The after-effects will arise in the appliance window, as apparent in Figure 5.
Looking at the capacity of the log book (LogFile.txt), apparent in here, you’ll see the adjustment of execution:
The Truth About Web Sequence Diagrams Is About To Be Revealed | Web Sequence Diagrams – web sequence diagrams
| Encouraged in order to my website, with this moment We’ll show you regarding web sequence diagrams