Closed ghost closed 6 years ago
Ran into the hostpolicy.dll issue this evening. Turns out I was only providing the assembly path and not the actual assembly name. To get the command to run, I just ran the following in my bin/Debug/netcoreapp3.1 path:
dotnet swagger tofile ./MyApp.dll v1
I'm having an issue running CLI. The app reads some variables from either environment or the appsettings.json file. But it seems when running commands it doesn't read the appsettings.json file hence throwing a null reference error. If I set those as env variables it works just fine.
P.S. I've also tried setting ASPNETCORE_ENVIRONMENT=Development with having all settings stored in appsettings.Development.json file. but it also not working.
I'm using .NETCoreApp Version=v3.1
Set of command I'm using.
dotnet restore src/myApp/myApp.csproj
dotnet publish src/myApp/myApp.csproj -o publish/ -c Release
dotnet new tool-manifest --force
dotnet tool install --local Swashbuckle.AspNetCore.Cli --version 6.1.4
dotnet swagger tofile --output swagger.json publish/myApp.dll v1
and Error is:
Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
I'm sure this is because my app is not able to find env variables.
Thanks.
Does this work for .NET core 3.1?
the CLI does not seem to be compatible
Okay I fixed it by using the correct versions of the packages. (I wrote a detailed article here ). Should work with the Swashbuckle.AspNetCore.Cli package
@Indigo744 or @fiksen99 can someone from you guys share the implemented solution - i have a task to generate swagger .json files from multiple projects all created on .net framework 4.8 and those jsons files to be merged in one swagger file used by .net core API which will behave as API Gateway
@nikolay-dimitrov unfortunately I don't have access to the source code any more(changed company). From what I recall, we basically introduced a separate argument which would output the swagger, then called that from an ms build task. Unfortunately I don't have much more info. Sounds like you'll need to do some post processing to get the swagger files merged also
@fiksen99 tnx for you answer - im not sure how exactly to get the instance of the swagger, in the example that is mention by @Indigo744 the following code is modified somehow
private static string GenerateSwagger(IWebHost host, string docName)
{
var sw = (ISwaggerProvider)host.Services.GetService(typeof(ISwaggerProvider));
var doc = sw.GetSwagger(docName, null, "/");
return JsonConvert.SerializeObject(
doc,
Formatting.Indented,
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
ContractResolver = new SwaggerContractResolver(new JsonSerializerSettings())
});
}
but since im on .net framework 4.8 there is no IWebHost host (Swagger it self is been plugged as middleware in .NET Core) while in the normal framework when Swagger package is been installed then its magically called runtime - without any DI or something else. Any help is really appreciated
Hello,
Just to be sure I'm understanding correctly, you are using ASPNET Core on .Net Framework, right?
What we essentially do is:
public static class Program
{
public static void Main(string[] args)
{
var webHost = CreateWebHostBuilder().Build();
// If args, then it's for generating swagger and exit
// otherwise run web server
if (args.Length > 0)
{
Console.WriteLine(GenerateSwagger(webHost, args[0]));
Environment.Exit(0);
}
webHost.Run();
}
private static IWebHostBuilder CreateWebHostBuilder()
{
IWebHostBuilder builder = WebHost
.CreateDefaultBuilder()
.UseConfiguration(...)
.UseUrls(...)
.UseStartup<Startup>()
....;
return builder;
}
private static string GenerateSwagger(IWebHost host, string docName)
{
ISwaggerProvider sw = (ISwaggerProvider)host.Services.GetService(typeof(ISwaggerProvider));
OpenApiDocument doc = sw.GetSwagger(docName);
return doc.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0);
}
}
Hope it helps.
@Indigo744 no its normal .net framework 4.8 web api
@nikolay-dimitrov you mean ASP.NET? I fear you're in the wrong repo then, as this is Swagger for ASP.NET Core... Maybe you should look into https://github.com/domaindrivendev/Swashbuckle.WebApi and open an issue there.
Anyway, we don't use ASP.Net so I can't help you further. Good luck!
@Indigo744 this looks very familiar and likely exactly what we did. However As @Indigo744 says - this issue (and this repo) is tracking Swashbuckle in ASP.NET Core (which you can use with .NET Framework). the ASP.NET Swashbuckle version is linked out, though I believe it is no longer maintained.
I am using dotnet swagger tofile --output [output] [startupassembly] [swaggerdoc]
this command to generate swagger docs, and it works fine locally.
But when it's in the CI/CD, as far as I know, it needs to start the app, but the app will not start in CI/CD due to some dependencies problems. So the app will throw exceptions inside Program.Main
when it's run on CI/CD.
my question is, what's the recommended solution there? is there a way to skip exceptions? or even do not start the app? or should I configure what dependency to load in Main
?
@awarrenlove Are you executing dotnet run swagger
as part of a post-build script within the csproj; if so, how are you doing that?
Feature Request
It would be great to generate the Swagger file in CI without running the app. I am willing to build such a feature (either in this repo or create another tool). Using reflection, a small program could read all docs and generate the swagger file.
What are your comments about it?