Swagger/OpenAPI –part 16
In order to see the WebAPI, Swagger is a default technology. Reading https://docs.microsoft.com/en-us/aspnet/core/tutorials/web-api-help-pages-using-swagger?view=aspnetcore-3.0 , seems to have 2 options: Swashbuckle and NSwag. Looking at github repositories, both have > 2k stars, active development, tutorials. The only thing that I see more at NSwag are generators for Angular.
Integrated NSwag as for the tutorial from https://docs.microsoft.com/en-us/aspnet/core/tutorials/getting-started-with-nswag?view=aspnetcore-3.0&tabs=visual-studio . There is a small problem – when I try the swagger , I should put explicit the version. However, a not experienced user can not see this value.
Trying to put documentation : when I go to Project=>Properties=>Build and click the XML documentation, shows the full path to the XML. I do not need this! So I edit the .csproj directly and change from full path to file
Also, because I want to be sure to have documentation, I change “warnings to errors”.
So now I must document Program and main(removed public from startup.cs and createwebhost – to not document)
Total: 6 changes , https://github.com/ignatandrei/InfoValutar/commit/4fbff903c4510acbcb219d0263ae89736e9f2d8d
So now I can browse to swagger
However, I should indicate to people that swagger is enabled to invite them browse to this – and this is easy to do in Kestrel, right ? Just take the address of the url …
So, trying in Program.cs to find the adresses:
var build = CreateHostBuilder(args).Build(); var s = build.Services.GetService(typeof(KestrelServerOptions)); var s1 = build.Services.GetService(typeof(IServer)); var s2 = build.Services.GetService(typeof(IServerAddressesFeature)); build.Run();
Not a chance – not yet configured. So , next chance is in Startup.cs
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { //other code var sa = app.ServerFeatures.Get<IServerAddressesFeature>(); var urls =string.Join(",", sa.Addresses.Select(it => it + "swagger")); Console.WriteLine("please use " + urls);
( for this, you must not use IIS Express, but rather the project)
Infovalutar
And one hour passes...(This is the result of 1 hour per day auto-challenge as a full cycle developer for an exchange rates application)
( You can see the sources at https://github.com/ignatandrei/InfoValutar/ )
Leave a Reply