Jump to: How to compile .NET Core projects
When a Swagger specification is generated with either the
then the selected assemblies are loaded in a separate AppDomain (when host is running on full .NET) or AssemblyLoadContext (running on .NET Core) to avoid assembly conflicts.
This is required to generate the OpenAPI specification outside of a running ASP.NET process, i.e. it can be generated externally (e.g. via command line). However, this process may be tricky - for example, because some required DLLs are missing or NSwag runs with newer versions of libraries than the loaded Web API or ASP.NET Core assembly. To avoid problems with this process, you should use the NSwag binary of the same runtime as the assembly to load.
NSwagStudio internally starts one of the NSwag command line binaries depending of the selected Runtime
(the default uses the same runtime as NSwagStudio - i.e. WinX64 or WinX86 - or the running exe). With the NSwag.NPM package, you can select the runtime with the /runtime:WinX64
setting. The package NSwag.MSBuild provides different variables to access a specific binary.
This page gives you some tips how to properly configure NSwag to load assemblies:
Runtime
setting/runtime:NetCore20
switches${NSwagExe}
propertiesAvailable runtimes (set via /runtime:WinX64
):
The important settings for loading assemblies are:
.NET Core does not output all referenced DLLs, some are looked up from the NuGet cache when running the app. The following sections show how to compile your project in a way so that all required DLLs are in the output directory.
Important: Choose the correct Runtime
in NSwagStudio or use the /runtime:NetCore21
setting with the NSwag.NPM package.
.NET Core 2.2 .NET Core 2.1
Build with
dotnet build /p:CopyLocalLockFileAssemblies=true
or use the following tag in the csproj:
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
so that all required DLLs are in the output folder (when used in CLI/NSwagStudio). Or use the following when used as MSBuild task:
<Target Name="NSwag" AfterTargets="Build"> <Copy SourceFiles="@(Reference)" DestinationFolder="$(OutDir)References" /> <Exec Command="$(NSwagExe_Core20) run nswag.json /variables:Configuration=$(Configuration)" /> <RemoveDir Directories="$(OutDir)References" /> </Target>
.NET Core 2.0
Because an ASP.NET Core 2.0 app is not published with all required DLLs, you have to add the following config to your .csproj:
<PublishWithAspNetCoreTargetManifest>false</PublishWithAspNetCoreTargetManifest>
This makes the compiler output all required DLLs to the output directory. If you want to fully publish only once, use:
dotnet publish --self-contained --runtime your_runtime
See the .NET Core Runtime IDentifier (RID) catalog for more information about runtimes.
.NET Core 1.1
First publish your project with dotnet publish
, then add the DLL with the controllers to the AssemblyPaths from the publish output directory.
.NET Core 1.0
To generate the Swagger/OpenAPI specification for a controller in a .NET Core assembly, follow these steps:
C:\Projects\MyApplication\bin\Debug\MyApplication.dll
C:\Program Files\dotnet\shared\Microsoft.NETCore.App\1.0.0
C:\Projects\MyApplication\bin\Release\PublishOutput
C:\Projects\MyApplication\web.config
IsAspNetCore
setting to trueSample configuration in NSwagStudio for a .NET Core Web API assembly:
Could not load type X from assembly Y
dotnet publish
to publish all required DLLs (also try --self-contained
for .NET Core 2.0)C:\Program Files\dotnet\shared\Microsoft.NETCore.App\2.0.0
to ReferencePaths (replace "2.0.0" with your .NET Core version)Deprecated: If some DLLs are missing because they were not published and thus not available, first try to add %USERPROFILE%/.nuget/packages
to the ReferencePaths
setting (NuGet cache locations on other platforms).
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4