OpenAPI Development in Arch Linux
It as been a while since i’ve engaged with C#. In this article I’ll describe a step-by-step on how to setup your Arch Linux to implement a .netcore application implementation of OpenAPI.
Instalation
Let’s start by making sure we have the SDK and runtime packages available on the system.
sudo pacman -S dotnet-runtime dotnet-sdk |
We can validate the existing enviroments with the command
dotnet --list-sdks |
At the time of this writing
6.0.100 [/usr/share/dotnet/sdk] |
Microsoft recommends using Visual Studio Code i will skip that part but here is the url in case of need.
Hello World
Now let’s do a quick 5m Hello world just to make sure everything is working properly.
dotnet new console -o MyApp |
One should have a similar output
The template "Console App" was created successfully. |
Now let’s run this amazing helloworld
cd MyApp |
One should get the desirable outcome
Hello, World! |
Congratulations, you’ve built and run your .NET app. Let’s clean the space now
cd .. |
OpenAPI
Now let’s create our webAPI
dotnet new webapi -o WebApi1 |
Next lets add the Swashbuckle.AspNetCore package
dotnet add package Swashbuckle.AspNetCore |
At this stage if you try to build it works but fails on the execution with the following error:
That is because Arch linux ships ASPNet in a different package. To Solve one should install the extra package using pacman.
sudo pacman -Sy aspnet-runtime |
After the installation if we check the exiting runtimes
dotnet --list-runtimes |
One should have the following
Microsoft.AspNetCore.App 6.0.0 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App] |
Lets try to run it now
dotnet run |
You should have a similar output
Building... |
Now that looks promissing let’s test the the endpoint by using the browser on
https://localhost:7177/swagger/index.html |
One should see a WeatherForecast service which is the baseline service when created the project.
Leave this runnig for now and open a new terminal to create a client.
Client
Now that we have our service running let’c create a client by retrieving the OpenAPI description.
For that we will be using the NSwag.ApiDescription.Client
package.
Let’s start by creating a new client project and retrieving the OpenAPI description file from swagger.
cd .. |
Now lets add the required package
dotnet add package NSwag.ApiDescription.Client --version 13.15.5 |
If one checks the console.csproj
file we can see the package was added. Although i have added a specific version is not relevant.
We are also going to need the following package
dotnet add package Newtonsoft.Json |
Add the following block on the console.csproj
file
<ItemGroup> |
Now execute the build command to generate the WeatherService classes
dotnet build |
You should have a similar output
Microsoft (R) Build Engine version 17.0.0+c9eb9dd64 for .NET |
And a brand new WeatherClient Class is now available to use on your main Program so let’s use the following on Program.cs
// Configure HttpClientHandler to ignore certificate validation errors. |
If one runs the client now with dotnet run
the following output is expected.
12/20/2021 10:34:23 PM +00:00 - Freezing |
Hurray our client receives the API data and both client and server seems to be working
Archnt to run the service and setup a CI/CD process for it. Also to extend the service or create something more appealing.
References
- https://wiki.archlinux.org/title/.NET
- https://docs.microsoft.com/en-us/aspnet/core/tutorials/web-api-help-pages-using-swagger
- https://wiki.archlinux.org/title/Visual_Studio_Code
- https://dotnet.microsoft.com/en-us/learn/dotnet/hello-world-tutorial/create
- https://github.com/domaindrivendev/Swashbuckle.AspNetCore
- https://github.com/RicoSuter/NSwag