|
1 | 1 | # Rest Api Client Generation |
2 | 2 |
|
3 | | -Nowadays, an application which consumes RestApi is a very common scenario. We are going to generate the Rest API client automatically during the build process. We will use [NSawg](https://docs.microsoft.com/aspnet/core/tutorials/getting-started-with-nswag?view=aspnetcore-6.0&tabs=visual-studio) |
| 3 | +Nowadays, an application which consumes RestApi is a very common scenario. We are going to generate the Rest API client automatically during the build process. We will use [NSwag](https://docs.microsoft.com/aspnet/core/tutorials/getting-started-with-nswag?view=aspnetcore-6.0&tabs=visual-studio) |
4 | 4 |
|
5 | 5 | Our app will be a console app, because it is simpler. The kind of app which use the Rest Api client is not important. |
6 | 6 | The example will consume the public [Pet Store API](https://petstore.swagger.io), which publish the [OpenAPI spec](https://petstore.swagger.io/v2/swagger.json) |
@@ -52,7 +52,7 @@ The complete code version is in this PetReaderExecTaskExample folder, you can do |
52 | 52 |
|
53 | 53 | You can notice we are using [BeforeTarget and AfterTarget](https://docs.microsoft.com/visualstudio/msbuild/target-build-order#beforetargets-and-aftertargets) as way to define build order. |
54 | 54 | The first target called "generatePetClient" will be executed before the core compilation target, so we will create the source before the compiler executes. The input and output parameter are related to [Incremental Build](https://docs.microsoft.com/visualstudio/msbuild/how-to-build-incrementally). MSBuild can compare the timestamps of the input files with the timestamps of the output files and determine whether to skip, build, or partially rebuild a target. |
55 | | - After installing the NSwag.MSBuild NuGet package in your project, you can use the variable $(NSwagExe) in your .csproj file to run the NSwag command line tool in an MSBuild target. This way the tools can easily be updated via NuGet. Here we are using the _Exec MSBUild Task_ to execute the NSwag program with the required parameters to generate the client Rest Api. [More about Nsawg command and parameters](https://github.com/RicoSuter/NSwag/wiki/NSwag.MSBuild). |
| 55 | + After installing the NSwag.MSBuild NuGet package in your project, you can use the variable $(NSwagExe) in your .csproj file to run the NSwag command line tool in an MSBuild target. This way the tools can easily be updated via NuGet. Here we are using the _Exec MSBUild Task_ to execute the NSwag program with the required parameters to generate the client Rest Api. [More about NSwag command and parameters](https://github.com/RicoSuter/NSwag/wiki/NSwag.MSBuild). |
56 | 56 | You can capture output from `<Exec>` addig ConsoleToMsBuild="true" to your `<Exec>` tag and then capture the output using the ConsoleOutput parameter in an `<Output>` tag. ConsoleOutput returns the output as an Item. Whitespace is trimmed. ConsoleOutput is enabled when ConsoleToMSBuild is true. |
57 | 57 | The second target called "forceReGenerationOnRebuild" deletes the generated class during clean up to force the regeneration on rebuild target execution. This target runs after core clean MSBuild pre defined target. |
58 | 58 |
|
@@ -232,8 +232,8 @@ The complete code version is in this PetReaderToolTaskExample folder, you can do |
232 | 232 | <PetClientClientClassName>PetRestApiClient</PetClientClientClassName> |
233 | 233 | <PetClientClientNamespaceName>PetRestApiClient</PetClientClientNamespaceName> |
234 | 234 | <PetClientFolderClientClass>PetRestApiClient</PetClientFolderClientClass> |
235 | | - <!--The directory where NSawg.exe is in--> |
236 | | - <NSwagCommandFullPath>C:\Nsawg\Win</NSwagCommandFullPath> |
| 235 | + <!--The directory where NSwag.exe is in--> |
| 236 | + <NSwagCommandFullPath>C:\NSwag\Win</NSwagCommandFullPath> |
237 | 237 | </PropertyGroup> |
238 | 238 | ``` |
239 | 239 |
|
|
0 commit comments