这给了这个版本库。https:/github.comOrwelGenerateSwaggerFileWithDotnetFramewrok。
swagger tofile WebApplication1.exe v1
显示错误。
A JSON parsing exception occurred in [.\WebApplication1\bin\Debug\net48\WebApplication1.exe]: * Line 1, Column 2 Syntax error: Malformed token
A fatal error was encountered. The library 'hostpolicy.dll' required to execute the application was not found in '.\WebApplication1\bin\Debug\net48\'.
Failed to run as a self-contained app. If this should be a framework-dependent app, add the .\bin\Debug\net48\WebApplication1.json file specifying the appropriate framework.
这个错误一般是在.NET Core应用程序加载一个针对其他运行时的DLL时产生的,可能Swashbuckle.AspNetCore.Cli不能与.NET Framework API一起使用。
如何使用ASP.NET Core 2.2和Swashbuckle.AspNetCore 5.4.1针对.NET Framework 4.8生成Swagger文件?
我没能从API外部的程序中生成swagger文件。
我修改了生成swagger文件的api。swagger.json
文件。
public static class Program
{
public static void Main(string[] args)
{
var webHost = CreateWebHostBuilder(args).Build();
if (args.Length == 1 && args[0] == "generate")
{
var json = webHost.GenerateSwagger("v1", null);
File.WriteAllText("swagger.json", json);
}
else
{
webHost.Run();
}
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
public static string GenerateSwagger(this IWebHost webhost, string docName, string basePath)
{
var sw = webhost.Services.GetRequiredService<ISwaggerProvider>();
var doc = sw.GetSwagger(docName, null, basePath);
using (var streamWriter = new StringWriter())
{
var writer = new OpenApiJsonWriter(streamWriter);
doc.SerializeAsV3(writer);
return streamWriter.ToString();
}
}
}
我接受回答会产生 swagger.json
而不参考api项目。