5

I used Visual Studio Add-Service-Reference to add a service that uses the OpenAPI Specification. Add service reference

I input the swagger URL and generated the code.

The .csproj indicates that the service is configured. service reference

However when I try to build I get an error.

The wizard contained this link

The service was generated using AutoRest. I am using VS2022 17.2.5

The full error is

Error MSB3073 The command ""C:\Users\kirst.nuget\packages\nswag.msbuild\13.0.5\build../tools/Win/NSwag.exe" openapi2csclient /className:myapicls /namespace:myapi /input:D:\dev\MyApi\UnitTestProject1\OpenAPIs\index.html /output:obj\indexClient.cs " exited with code -1. UnitTestProject1 C:\Users\kirst.nuget\packages\nswag.apidescription.client\13.0.5\build\NSwag.ApiDescription.Client.targets 28

Helen
  • 87,344
  • 17
  • 243
  • 314
Kirsten
  • 15,730
  • 41
  • 179
  • 318
  • 1
    You probably need to point to an OpenAPI YAML/JSON file, not to the Swagger UI web page. See [How to export OpenAPI YAML/JSON file from Swagger UI](https://stackoverflow.com/q/48525546/113116). – Helen Jun 29 '22 at 16:48

2 Answers2

2

A service reference expects an OpenAPI YAML/JSON document, not the Swagger UI web page. This answer explains how you can find the URL of your OpenAPI YAML/JSON file or export it from Swagger UI.

Helen
  • 87,344
  • 17
  • 243
  • 314
1

If anyone else ends up here but got the URL correct (usually its /api/swagger.json) there might be a problem with spaces in the path to the project. I started to have this problem after checking out a project to another machine.

The fix was in the file C:\Users\{user}\.nuget\packages\nswag.apidescription.client\13.0.5\build\NSwag.ApiDescription.Client.targets

in Line 21:

    <Command>%(Command) /input:%(FullPath) /output:%(OutputPath) %(Options)</Command>

To make swagger whitespace compatible, change that line to:

    <Command>%(Command) /input:"%(FullPath)" /output:"%(OutputPath)" %(Options)</Command>

Error is gone after that

Jens Caasen
  • 597
  • 1
  • 6
  • 19