This code is for ASP.NET Core 2.0, but it may come in handy. It relies on Swashbuckle.AspNetCore and Microsoft.AspNetCore.TestHost
IWebHostBuilder builder = new WebHostBuilder()
.UseStartup<Startup>()
.ConfigureServices(services =>
{
services.AddSwaggerGen(opts =>
{
opts.SwaggerDoc("doc-name", new Info { Title = "Title", Version = "v1" });
});
});
JsonSerializerSettings mvcSerializerSettings;
SwaggerDocument document;
using (var testServer = new TestServer(builder))
{
IOptions<MvcJsonOptions> mvcOptions = testServer.Host.Services.GetService<IOptions<MvcJsonOptions>>();
mvcSerializerSettings = mvcOptions.Value.SerializerSettings;
ISwaggerProvider swaggerProvider = testServer.Host.Services.GetService<ISwaggerProvider>();
document = swaggerProvider.GetSwagger("doc-name");
}
var settings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
Formatting = mvcSerializerSettings.Formatting,
ContractResolver = new SwaggerContractResolver(mvcSerializerSettings),
};
string json = JsonConvert.SerializeObject(document, settings);
Where Startupis your launch class. The project is directly referenced here, but you should be able to load the assembly and consume it accordingly.
source
share