带参数的 NSwagStudio 反射行为
NSwagStudio reflection behavior with parameters
我创建了一个简单的 .net5 网站 api。然后我得到了很棒的 Rico Suter 的 Nswag.aspnetcore 并调整它让我的应用程序创建 swagger.json 文件
在脚手架控制器中,我编写了另一个 PUT 操作,它接受一个对象参数(我能做的最简单的)
[HttpPost]
public ActionResult simple(WeatherForecast w)
{
return NoContent();
}
开箱即用,一切正常。我可以在 http://localhost:63630/swagger/v1/swagger.json
文件中找到。
然后,我将使用 NSwagStudio 获取带有接口的 C# 客户端代理。
根据 OpenAPI 规范,我将获得漂亮的 C# 界面:
[System.CodeDom.Compiler.GeneratedCode("NSwag", "13.10.2.0 (NJsonSchema v10.3.4.0 (Newtonsoft.Json v12.0.0.0))")]
public partial interface IWeatherForecastClient
{
/// <exception cref="ApiException">A server side error occurred.</exception>
System.Threading.Tasks.Task<System.Collections.Generic.ICollection<WeatherForecast>> GetAsync();
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <exception cref="ApiException">A server side error occurred.</exception>
System.Threading.Tasks.Task<System.Collections.Generic.ICollection<WeatherForecast>> GetAsync(System.Threading.CancellationToken cancellationToken);
/// <exception cref="ApiException">A server side error occurred.</exception>
System.Threading.Tasks.Task<FileResponse> SimpleAsync(WeatherForecast w);
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <exception cref="ApiException">A server side error occurred.</exception>
System.Threading.Tasks.Task<FileResponse> SimpleAsync(WeatherForecast w, System.Threading.CancellationToken cancellationToken);
}
现在,我将使用 NSwagStudio 选项“WebAPI 反射”,配置:
- .Net Asembly 路径 => .\bin\Debug\net5.0\WebApplication7.dll
- 引用的汇编文件路径 => .\bin\Debug\net5.0\
- 检查程序集是否正在使用 ASP.NET 核心
界面是
[System.CodeDom.Compiler.GeneratedCode("NSwag", "13.10.2.0 (NJsonSchema v10.3.4.0 (Newtonsoft.Json v12.0.0.0))")]
public partial interface IWeatherForecastClient
{
/// <exception cref="ApiException">A server side error occurred.</exception>
System.Threading.Tasks.Task<System.Collections.Generic.ICollection<WeatherForecast>> GetAsync();
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <exception cref="ApiException">A server side error occurred.</exception>
System.Threading.Tasks.Task<System.Collections.Generic.ICollection<WeatherForecast>> GetAsync(System.Threading.CancellationToken cancellationToken);
/// <exception cref="ApiException">A server side error occurred.</exception>
System.Threading.Tasks.Task<FileResponse> SimpleAsync(System.DateTimeOffset? date, int? temperatureC, int? temperatureF, string summary);
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <exception cref="ApiException">A server side error occurred.</exception>
System.Threading.Tasks.Task<FileResponse> SimpleAsync(System.DateTimeOffset? date, int? temperatureC, int? temperatureF, string summary, System.Threading.CancellationToken cancellationToken);
}
post方法中的参数已被解构!!
问题是:如何通过反射获得与 OpenAPI 规范选项相同的接口?
不要将“WebAPI 反射”与 ASP.NET 核心一起使用,因为它仅用于遗留 ASP.NET(非核心!)。使用“ASP.NET Core via API Explorer”,它或多或少也基于反射(加载程序集或 .csproj)。我建议使用 .csproj 作为输入而不是 DLL...如果没有在 DI 中注册 NSwag 文档,您还可以通过 UI.
指定配置
我创建了一个简单的 .net5 网站 api。然后我得到了很棒的 Rico Suter 的 Nswag.aspnetcore 并调整它让我的应用程序创建 swagger.json 文件
在脚手架控制器中,我编写了另一个 PUT 操作,它接受一个对象参数(我能做的最简单的)
[HttpPost]
public ActionResult simple(WeatherForecast w)
{
return NoContent();
}
开箱即用,一切正常。我可以在 http://localhost:63630/swagger/v1/swagger.json
文件中找到。
然后,我将使用 NSwagStudio 获取带有接口的 C# 客户端代理。
根据 OpenAPI 规范,我将获得漂亮的 C# 界面:
[System.CodeDom.Compiler.GeneratedCode("NSwag", "13.10.2.0 (NJsonSchema v10.3.4.0 (Newtonsoft.Json v12.0.0.0))")]
public partial interface IWeatherForecastClient
{
/// <exception cref="ApiException">A server side error occurred.</exception>
System.Threading.Tasks.Task<System.Collections.Generic.ICollection<WeatherForecast>> GetAsync();
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <exception cref="ApiException">A server side error occurred.</exception>
System.Threading.Tasks.Task<System.Collections.Generic.ICollection<WeatherForecast>> GetAsync(System.Threading.CancellationToken cancellationToken);
/// <exception cref="ApiException">A server side error occurred.</exception>
System.Threading.Tasks.Task<FileResponse> SimpleAsync(WeatherForecast w);
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <exception cref="ApiException">A server side error occurred.</exception>
System.Threading.Tasks.Task<FileResponse> SimpleAsync(WeatherForecast w, System.Threading.CancellationToken cancellationToken);
}
现在,我将使用 NSwagStudio 选项“WebAPI 反射”,配置:
- .Net Asembly 路径 => .\bin\Debug\net5.0\WebApplication7.dll
- 引用的汇编文件路径 => .\bin\Debug\net5.0\
- 检查程序集是否正在使用 ASP.NET 核心
界面是
[System.CodeDom.Compiler.GeneratedCode("NSwag", "13.10.2.0 (NJsonSchema v10.3.4.0 (Newtonsoft.Json v12.0.0.0))")]
public partial interface IWeatherForecastClient
{
/// <exception cref="ApiException">A server side error occurred.</exception>
System.Threading.Tasks.Task<System.Collections.Generic.ICollection<WeatherForecast>> GetAsync();
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <exception cref="ApiException">A server side error occurred.</exception>
System.Threading.Tasks.Task<System.Collections.Generic.ICollection<WeatherForecast>> GetAsync(System.Threading.CancellationToken cancellationToken);
/// <exception cref="ApiException">A server side error occurred.</exception>
System.Threading.Tasks.Task<FileResponse> SimpleAsync(System.DateTimeOffset? date, int? temperatureC, int? temperatureF, string summary);
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <exception cref="ApiException">A server side error occurred.</exception>
System.Threading.Tasks.Task<FileResponse> SimpleAsync(System.DateTimeOffset? date, int? temperatureC, int? temperatureF, string summary, System.Threading.CancellationToken cancellationToken);
}
post方法中的参数已被解构!!
问题是:如何通过反射获得与 OpenAPI 规范选项相同的接口?
不要将“WebAPI 反射”与 ASP.NET 核心一起使用,因为它仅用于遗留 ASP.NET(非核心!)。使用“ASP.NET Core via API Explorer”,它或多或少也基于反射(加载程序集或 .csproj)。我建议使用 .csproj 作为输入而不是 DLL...如果没有在 DI 中注册 NSwag 文档,您还可以通过 UI.
指定配置