Giovanni Tardino
Giovanni Tardino

Reputation: 21

How to generate a Visual Basic .NET client from an OpenAPI definition?

I need to call a REST API from a Visual Basic .NET project (.NET Framework 3.5). They gave me an OpenAPI .yaml file. In the Swagger Editor I can't generate the client for Visual Basic, only for C#. I tried to export the JSON from the Swagger Editor and paste it as a class in Visual Studio, but the generated class seems to be incorrect and incomplete.

Is there a way to generate a Visual Basic .NET client from an OpenAPI definition?

Upvotes: 2

Views: 1460

Answers (2)

Joel Coehoorn
Joel Coehoorn

Reputation: 415600

VB.Net can use C# assemblies (dlls), and Visual Studio is happy to build mixed projects.

This makes it fairly simple to use a C# client generated by Swagger from an app that is otherwise fully VB.Net. You can add the Swagger-generated code to it's own separate project in the same VS solution.

Upvotes: -1

Mteheran
Mteheran

Reputation: 293

I found something that works for me using NSwag library

https://github.com/RicoSuter/NSwag/wiki/OwinGlobalAsax#integration

nuget install

  • Microsoft.Owin.Host.SystemWeb
  • NSwag.AspNet.Owin ​

WebApiConfig.vb

RouteTable.Routes.MapOwinPath("swagger", Function(app) As Owin.IAppBuilder
                                                         Return app.UseSwaggerUi(GetType(WebApiApplication).Assembly, Function(opts) As SwaggerUiSettings(Of NSwag.Generation.WebApi.WebApiOpenApiDocumentGeneratorSettings)
                                                                                                                          opts.MiddlewareBasePath = "/swagger"
                                                                                                                          opts.GeneratorSettings.DefaultUrlTemplate = "api/{controller}/{action}/{id}"
                                                                                                                          Return opts
                                                                                                                      End Function)
    End Function)

Web.Config

<configuration>
 <appSettings>
  <add key="owin:AutomaticAppStartup" value="false" />
  <system.webServer>
    <handlers>
    <!-- Handler swagger -->
<add name="NSwag" path="swagger" verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />

Upvotes: 0

Related Questions