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.
- Why do we need to generate this C# client code?
- 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:
- C# client classes generated from a Swagger/OpenAPI specification of our web API.
- Model classes using a class style like POCOs, Classes implementing INotifyPropertyChanged, or Classes with Prism implementation.
- 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.
Step 3. Now, click the Swagger.json link in the Swagger documentation page.
Step 4. Now, Copy that URL, we need this URL to generate the Client code.
Step 5. It is time to launch the NSwag Studio now. Run it on our PC or Laptop.
Step 6. In the Input Section (Left Side) of NSwag Studio, paste that Copied URL in the Specification URL textbox.
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).
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, set up the namespace where we want to use this produced client codebase.
Second, uncheck the (the Inject HttpClient constructor) checkbox.
Optional, set a class type to POCO or IPC (INotifyPropertyChanged).
Optional, set up an output file path.
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.
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.
namespace ClientAppConsole
{
class Program
{
static async System.Threading.Tasks.Task Main(string[] args)
{
Console.WriteLine("Hello World!");
WeatherForecastClient weatherForecastClient = new WeatherForecastClient();
var res = await weatherForecastClient.GetAsync(); //consume a webApi get action
foreach (var item in res)
{
Console.WriteLine($"{ item.Date} temperature is {item.TemperatureC} ,{item.Summary}");
}
Console.WriteLine();
}
}
}
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.