我创建了一个简单的.net5 web。然后我得到伟大的Rico的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反射“,Config:
用于引用程序集文件的.\bin\Debug\net5.0\WebApplication7.dll
接口是
[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规范选项相同的接口?
发布于 2021-02-17 16:57:30
不要在WebAPI核心中使用“ASP.NET反射”,因为它只适用于遗留的ASP.NET (非核心!)。使用"ASP.NET核心通过assemblies“,这也或多或少是基于反射的(加载程序集或.csproj)。我建议使用.csproj作为输入而不是DLL.如果DI中没有注册的NSwag文档,也可以通过UI指定配置。
https://stackoverflow.com/questions/66184806
复制相似问题