1

Swagger を使用して WebAPI のクライアント コードを生成しようとしています。私は通常、(WebAPI をデプロイした後に) メタデータ json ファイルをダウンロードし、クライアント クラス ライブラリのオプション "Add->Rest API Client.." を使用してこれを行います。

追加 -> Rest API Client

これにより、目的のプロジェクトにクライアント ライブラリが生成され、正常に動作します。しかし、Autorest.exe を使用してこのプロセスを自動化しようとしています。以下のように、クライアントコードを生成するためのパラメーターを使用してAutorest.exeを実行する小さなコードを作成しました。

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 が生成するものと一致するクライアント コードの自動生成について助けが必要です。前もって感謝します。

4

2 に答える 2

2

実際、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 
于 2017-01-05T18:08:55.940 に答える
0

私はついに問題を理解しました。別のバージョンの Autorest.exe を使用してクライアントを生成していましたが、Visual Studio の "Add -> Rest API Client" は別のバージョンを使用しています。必要なバージョンをダウンロードして上記と同じコマンドを使用すると、魅力的に機能しました。

于 2016-12-22T16:01:10.720 に答える