Different client output when used Autorest.exe vs ADD->Rest API Client

1.3k views Asked by At

I am trying to generate client code for WebAPI with Swagger. I usually do that by downloading the metadata json file (after deploying WebAPI) and then use the option "Add->Rest API Client.." for the client class library.

Add -> Rest API Client.

This will generate client library to the desired project and works fine. But i am trying to automate this process using Autorest.exe. I wrote small code to execute Autorest.exe with parameters to generate the client code as below

string filename = @"C:\Users\xxxx\packages\AutoRest.0.9.7\tools\AutoRest.exe";
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.FileName = filename;
startInfo.Arguments = "-CodeGenerator CSharp -Modeler CompositeSwagger -Input http://WebAPIDomainNameGoesHere:80/swagger/docs/v1 -Namespace CESOutboundAPI.ClientLibrary";
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();

But the newly generated client code doesn't work (doesn't build) and isn't similar to what i get as output when i use GUI options in visual studio.

When i try building the newly generated code, i get the following error messages for each .cs file.

The type or namespace name 'ValidationRules' does not exist in the namespace 'Microsoft.Rest' (are you missing an assembly reference?)  
The type or namespace name 'SerializationException' does not exist in the namespace 'Microsoft.Rest' (are you missing an assembly reference?)   
The type or namespace name 'Serialization' does not exist in the namespace 'Microsoft.Rest' (are you missing an assembly reference?)    
The type or namespace name 'HttpResponseMessageWrapper' does not exist in the namespace 'Microsoft.Rest' (are you missing an assembly reference?)   
.
.

I need help with automating generation of client code that executes perfectly and matches to what GUI generates. Thanks in advance.

2

There are 2 answers

0
Venkat Pasumarthi On BEST ANSWER

I finally figured out the issue. I was using a different version of Autorest.exe to generate my client where as Visual Studio "Add -> Rest API Client" is using another version. After I downloaded the version needed and used the same command above, it worked like charm.

0
Garrett Serack On

Indeed, the version of AutoRest that ships in VS is quite old, and you really don't want to use that.

To get the latest version of AutoRest (installs into c:\autorest ... change to wherever you'd like it.)

Make sure you have NuGet 3.4 or later.

Latest Nightly Build

NuGet.exe install AutoRest -source https://www.myget.org/F/autorest/api/v3/index.json -prerelease -outputdirectory c:\autorest 

Latest Stable Build

NuGet.exe install AutoRest -outputdirectory c:\autorest