-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
NSwag.MSBuild
Package: NSwag.MSBuild
Important for modern .NET (non-framework): Assembly loading#net-core
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. The /controller
parameter can be omitted to generate a Swagger specification for all controllers.
<Target Name="NSwag" AfterTargets="Build">
<Exec EnvironmentVariables="ASPNETCORE_ENVIRONMENT=Development" Command="$(NSwagExe) aspnetcore2openapi /assembly:$(TargetDir)MyWebAssembly.dll /output:swagger.json" />
<Exec Command="$(NSwagExe) openapi2tsclient /input:swagger.json /output:Scripts/MyController.ts" />
</Target>
For better testability and stable output (defaults may change), it is recommended to create an NSwag Configuration Document (e.g. with NSwagStudio) and use:
<Target Name="NSwag" AfterTargets="Build">
<Exec Command="$(NSwagExe) run nswag.json /variables:Configuration=$(Configuration)" />
</Target>
<PropertyGroup>
<RunPostBuildEvent>OnBuildSuccess</RunPostBuildEvent>
</PropertyGroup>
<Target Name="NSwag" AfterTargets="PostBuildEvent" Condition=" '$(Configuration)' == 'Debug' ">
<Exec WorkingDirectory="$(ProjectDir)" EnvironmentVariables="ASPNETCORE_ENVIRONMENT=Development" Command="$(NSwagExe_Net80) run nswag.json /variables:Configuration=$(Configuration)" />
</Target>
For more information about the missing DLLs in .NET Core, see Assembly loading#net-core.
More information on nswag run.
If you need to run the NSwag command line in x86 mode, use the $(NSwagExe_x86)
placeholder.
For modern .NET projects, one of the placeholders $(NSwagExe_Net##)
(e.g. $(NSwagExe_Net90)
for .NET 8) should be used instead of $(NSwagExe)
.
Available properties:
- NSwagExe
- NSwagExe_x86
- NSwagExe_Net80
- NSwagExe_Net90
- NSwagDir
- NSwagDir_Net80
- NSwagDir_Net90
See Command Line for more information.
Also see ServiceProjectReference for the new feature.