NSwagStudio - Generate C# Client Code Of ASP.NET Core Web API 😍

In the last post, I told you about adding NSwag Documentation to ASP.NET core web API. In that post, we became aware of the NSwag package and Swagger/OpenAPI documentation. This NSwag package imports some real swag to a web API. All API endpoints can be tested easily without any third-party tools by using NSwag. This generated swagger API UI represents all endpoints as interactive API documentation, that lets you try out the API calls directly in the browser.
 
After implementing Swagger UI in our current web API, it is time to generate the C# Client code. Now a few questions should come up in your mind.
  1. Why do we need to generate this C# client code?
  2. How can we generate this C# client code?

Why do we need to generate this C# client code?

 
What if I tell you that we don’t have to write a single line of code to consume our API? Yes, this is possible by eliminating repetitive tasks like writing your HttpClient set up to call the REST API endpoint. Autogenerated C# client code streamlines the coordination between the backend development and frontend development. So I think we got our answer.
 

How can we generate this C# client code?

 
NSwag provides multiple options for client generation including a CLI, code, or a Windows application. In this post, we are going to use NSwagStudio. With this, we can generate various types of code.
  • TypeScriptClientGenerator: Generate TypeScript clients
  • CSharpClientGenerator: Generate CSharp clients
  • CSharpControllerGenerator: Generate CSharp Web API controllers (contract first/schema first development)
In this post, we will be going to generate the C# client code using NSwag Studio. That generated C# client code has features such as:
  1. C# client classes generated from a Swagger/OpenAPI specification of our web API.
  2. Model classes using a class style like POCOs, Classes implementing INotifyPropertyChanged, or Classes with Prism implementation.
  3. These generated clients can be used in any .NET Framework, .NET Core, Xamarin, and .NET Standard projects. 
Getting started,
 
Step 1
 
First, we need to download NSwagStudio and install it on our PC or Laptop.
 
Step 2
 
Now, run your ASP.Net Code Web API in the browser until we reach Swagger Documentation of our web API. If you do not find the Swagger documentation page of the ASP.Net core web API in the browser, please visit my previous post- Add NSwag Documentation to ASP.NET Core Web API.
 
NSwagStudio - Generate C# Client Code Of ASP.NET Core Web API 😍
 
 NSwag Documentation to ASP.NET Core Web API
 
Step 3
 
Now, click the Swagger.json link in the Swagger documentation page.
 
Swagger json
 
Step 4
 
Now, Copy that URL, we need this URL to generate the Client code.
 
swagger documentation page
 
Step 5
 
It is time to launch the NSwag Studio now. Run it on our PC or Laptop.
 
NSwagStudio
 
Step 6
 
In the Input Section (Left Side) of NSwag Studio, paste that Copied URL in the Specification URL textBox.
 
Web api NSwag Studio
 
Step 7
 
Now, go to Output Section(Right Side) in NSwag Studio, and then click on the CSharp Client checkbox. Here you can see a new tab (CSharpClient) is displaying below to ; below that checkbox, click that tab (CSharpClient).
 
CSharp Client checkbox nswag
 
Step 8
 
After choosing the CSharpClient tab, make some settings changes for our web API client code in the settings tab. Here, I will tell you about only a few essential settings.
 
First, setup the namespace where we want to use this produced client codebase.
 
Generate CSharp clients
 
Second, uncheck (the Inject HttpClient constructor) checkbox.
 
Add NSwag Documentation to ASP.NET Core Web API
 
Optional, set a class type to POCO or IPC (INotifyPropertyChanged).
 
NSwagStudio - Generate C# Client Code Of ASP.NET Core Web API 😍
 
Optional, set up an output file path.
 
insight of NSwagStudio
 
These are just basic settings inside of NSwagStudio. For more  customization of your client code, you can spend a few more minutes at the settings of NswagStudio.
 
Step 9
 
After completing the settings of NSwagStudio, hit the Generate outputs button to generate the actual C# client code for our ASP.Net Core web API.
 
C# client code for ASP.Net Core web API
 
Step 10
 
After generating the client code using NSwagStudio, you can find some C# code in the Output tab. Copy that whole code and paste it in any project where you want to consume this web API. By creating the object of any client class (Controllers of your Web API), you can make any type of call like HttpGet, HttpPut, HttpPost, etc using their exposed methods. We can use this generated C# client code in any C# base project type like .NET Framework, .NET Core, Xamarin, UNO Platform, and .NET Standard projects.
 
I have created a Console app for consuming this API, using the below code snippet we are able to consume our web API from our client app without manually setting up the HttpClient or WebClient. 
  1. namespace ClientAppConsole  
  2. {  
  3.     class Program  
  4.     {  
  5.         static async System.Threading.Tasks.Task Main(string[] args)  
  6.         {  
  7.             Console.WriteLine("Hello World!");  
  8.             WeatherForecastClient weatherForecastClient = new WeatherForecastClient();  
  9.             var res = await weatherForecastClient.GetAsync();     //consume a webApi get action
  10.             foreach (var item in res)  
  11.             {  
  12.                 Console.WriteLine($"{ item.Date}  temperature is {item.TemperatureC} ,{item.Summary}");  
  13.             }  
  14.             Console.WriteLine();  
  15.         }  
  16.     }  
  17.   
  18. }  
C# client code for our ASP.Net Core web API using nswag
 
It seems impressive to beable to get rid of that repetitive code by having the NSwag C# client code. For a better understanding of this project, you can visit this git repository.