C# Classes for WebApi Calls
First of all let me give a quick primer on the various useful C# classes that are now available specifically for the purposes of making calls to the Web Api. These classes handle most of the finer detail for us.
The most important thing is that you will need to install a nuget package called "Microsoft.AspNet.WebApi.Client".
So you will have to open the Package Manager Console and run this command: Install-Package Microsoft.AspNet.WebApi.Client:
Install-Package Microsoft.AspNet.WebApi.Client
One class is called the HttpClient
class. This class provides various functions like GetAsync, PostAsJsonAsync, PutAsJsonAsync, DeleteAsync. These methods handle json serialization, deserialization, http headers - everything from A to Z - for us.
The class HttpResponseMessage contains the response as received from the server side.
A try-catch block is often used to handle error conditions.
Video Explanation (see it happen!)
Please watch the following youtube video:
How to Make a GetAll Api Call
After this brief introduction, let us now open our WPF application project to examine the various parts. The completed application can be found in the downloads linked to this video.
This is the solution explorer (see the linked video). Let us first have an overview.
The models folder contains the data transfer object class called DoctorDTO. This is the same class that is used in our ASPNET Core WebApi project. Double-click and open it! As we can see we have copy-pasted it from the WebApi project. Notice that a constructor has been commented out because it is not relevant from the perspective of a client application.
Next, in the solution explorer we have a dialog window for editing and deleting a record. And, lastly we have the main window that contains a datagrid for showing the records.
Double-click and open the MainWindow.xaml.cs file.
// MainWindow.xaml.cs // see the attached downloads for // the WPF project using System; using System.Collections.Generic; using System.Net.Http; using System.Windows; using System.Windows.Controls; using WpfInterface.models; // Install-Package Microsoft.AspNet.WebApi.Client namespace WpfInterface { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { // the webapi server address public static readonly String SvcIP = "https://localhost:7264/doctor"; public MainWindow() { InitializeComponent(); LoadAllDoctors(); } // makes a call to the webapi and obtains the list // from https://localhost:7264/doctor/all async void LoadAllDoctors() { // make an API request using (HttpClient client = new HttpClient()) { try { HttpResponseMessage response = await client.GetAsync($"{SvcIP}/all"); response.EnsureSuccessStatusCode(); // deserialize to a list IList<DoctorDTO> ls = await response.Content.ReadAsAsync<IList<DoctorDTO>>(); dgRecords.DataContext = ls; } catch (Exception ex) { MessageBox.Show(ex.Message + "IS SERVER RUNNING AT THE PORT?"); } } } // . . . other functions not relevant here } }
First we have the namespaces. After that I have typed the name of the nuget package. You will have to run this command in the package manager console.
We have a readonly member to hold the IP address of the web api. This address is configured in the WebApi application - and we already know it, and we have already used it everywhere in this chapter.
The main function of interest is the LoadAllDoctors function. This function uses the HttpClient class that I have explained earlier. GetAsync method is used to make the GetAll request to the Web Api. The json sent by the web api can be deserialized by using the ReadAsAsync function. After that the list is attached as DataContext of the datagrid.
Run the Application
Now let us run the applications. First run the ASPNET Core WebApi application so as to start the server. Allow the home page to open.
Next open the WPF project and run the project. The window opens and shows a list of all the records sent by the ASPNET Core Application! (see the linked video). We'll take the case of POST WebApi in the next tutorial. Thanks!
Similar Posts
This Blog Post/Article "(C# ASP.NET Core) Call a GET Web API From a C# WPF Application" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.