2016-12-21 56 views
1

我想用Swagger爲WebAPI生成客戶端代碼。我通常通過下載元數據json文件(在部署WebAPI之後)來完成此操作,然後使用客戶端類庫的選項「Add-> Rest API Client ..」。使用Autorest.exe時不同的客戶端輸出與ADD-> Rest API客戶端

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生成的內容匹配。提前致謝。

回答

0

我終於明白了這個問題。我正在使用不同版本的Autorest.exe來生成我的客戶端,因爲Visual Studio「添加 - > Rest API客戶端」正在使用另一個版本。在我下載了所需的版本並使用了上述相同的命令後,它的功能就像魅力一樣。

2

事實上,VS中發佈的AutoRest版本已經很老了,你真的不想使用它。

要獲取最新版本AutoRest的(安裝到c:\autorest ......改變,無論你想它。)

請確保您有3.4的NuGet或更高版本。

最新每夜構建

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