使用 Autorest.exe 与 ADD->Rest API 客户端时不同的客户端输出

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

我正在尝试使用 Swagger 为 WebAPI 生成客户端代码。我通常通过下载元数据 json 文件(在部署 WebAPI 之后)然后为客户端 class 库使用选项 "Add->Rest API Client.." 来做到这一点。

Add -> Rest API Client。

这将为所需的项目生成客户端库并且工作正常。但我正在尝试使用 Autorest.exe 自动执行此过程。我写了一些小代码来执行 Autorest.exeparameters 生成客户端代码如下

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();

但是新生成的客户端代码不起作用(无法构建)并且与我在 visual studio 中使用 GUI 选项时得到的输出不相似。

当我尝试构建新生成的代码时,我收到每个 .cs 文件的以下错误消息。

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?)   
.
.

我需要帮助自动生成可以完美执行并与 GUI 生成的内容匹配的客户端代码。提前致谢。

我终于想通了。我使用不同版本的 Autorest.exe 生成我的客户端,而 Visual Studio "Add -> Rest API Client" 使用另一个版本。在我下载了所需的版本并使用上面相同的命令后,它就像魅力一样工作。

确实,VS 自带的 AutoRest 版本太旧了,你真的不想用那个。

获取最新版本的 AutoRest(安装到 c:\autorest ... 更改为您喜欢的位置。)

确保您有 NuGet 3.4 或更高版本。

最新的每晚构建

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

最新稳定版本

NuGet.exe install AutoRest -outputdirectory c:\autorest