C
C#2y ago
SWEETPONY

❔ how to use swagger in console application?

I created simple console application and wrote this:
static internal class Program
{
static async Task Main(string[] args) =>
await CreateHostBuilder(args)
.Build()
.RunAsync();

static private IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
static internal class Program
{
static async Task Main(string[] args) =>
await CreateHostBuilder(args)
.Build()
.RunAsync();

static private IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
public class Startup
{
public IConfiguration Configuration { get; }

public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public void ConfigureServices(IServiceCollection services)
{
services.AddSwaggerGen(swagger =>
{
swagger.SwaggerDoc(
name: "v1",
info: new OpenApiInfo
{
Title = "Values API",
Version = "v1"
});
});
}

public void Configure(IApplicationBuilder app)
{
app.UseSwagger();
app.UseSwaggerUI(swagger =>
{
swagger.SwaggerEndpoint("/swagger/v1/swagger.json", "values api v1");
});
}
}
public class Startup
{
public IConfiguration Configuration { get; }

public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public void ConfigureServices(IServiceCollection services)
{
services.AddSwaggerGen(swagger =>
{
swagger.SwaggerDoc(
name: "v1",
info: new OpenApiInfo
{
Title = "Values API",
Version = "v1"
});
});
}

public void Configure(IApplicationBuilder app)
{
app.UseSwagger();
app.UseSwaggerUI(swagger =>
{
swagger.SwaggerEndpoint("/swagger/v1/swagger.json", "values api v1");
});
}
}
and all I see in console:
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
Content root path: ..
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
Content root path: ..
actually I don't understand how to see swagger ui
14 Replies
SWEETPONY
SWEETPONYOP2y ago
static public class HostBuilderExtensions
{
private const string ConfigureServicesMethodName = "ConfigureServices";

static public IHostBuilder UseStartup<TStartup>(
this IHostBuilder hostBuilder) where TStartup : class
{
hostBuilder.ConfigureServices((ctx, serviceCollection) =>
{
var cfgServicesMethod = typeof(TStartup).GetMethod(
ConfigureServicesMethodName, new[] { typeof(IServiceCollection) });

var hasConfigCtor = typeof(TStartup).GetConstructor(
new[] { typeof(IConfiguration) }) != null;

var startUpObj = hasConfigCtor
? (TStartup)Activator.CreateInstance(typeof(TStartup), ctx.Configuration)!
: (TStartup)Activator.CreateInstance(typeof(TStartup), null)!;

cfgServicesMethod?.Invoke(startUpObj, new object[] { serviceCollection });
});

return hostBuilder;
}
}
static public class HostBuilderExtensions
{
private const string ConfigureServicesMethodName = "ConfigureServices";

static public IHostBuilder UseStartup<TStartup>(
this IHostBuilder hostBuilder) where TStartup : class
{
hostBuilder.ConfigureServices((ctx, serviceCollection) =>
{
var cfgServicesMethod = typeof(TStartup).GetMethod(
ConfigureServicesMethodName, new[] { typeof(IServiceCollection) });

var hasConfigCtor = typeof(TStartup).GetConstructor(
new[] { typeof(IConfiguration) }) != null;

var startUpObj = hasConfigCtor
? (TStartup)Activator.CreateInstance(typeof(TStartup), ctx.Configuration)!
: (TStartup)Activator.CreateInstance(typeof(TStartup), null)!;

cfgServicesMethod?.Invoke(startUpObj, new object[] { serviceCollection });
});

return hostBuilder;
}
}
Pobiega
Pobiega2y ago
Uhm.. Swagger is for HTTP endpoints.
SWEETPONY
SWEETPONYOP2y ago
I just want to create open api documentation for my objects
Jimmacle
Jimmacle2y ago
your program needs to have an API to document it doesn't work in a vacuum, it generates it dynamically based on your endpoints
SWEETPONY
SWEETPONYOP2y ago
hm, I have this one but it looks like custom api let me show.. I have this class with custom attributes:
[ApiEndpoint( Name, SignatureBase )]
[Title]
[Description]
abstract public class ReportsAPI
{
public const string Name = "reports";

public const string SignatureBase = "/system/report";

static public readonly EndpointSignature Signature =
SignatureBase;

public class TitleAttribute
: TitleLocalizedAttribute
{
public TitleAttribute()
: base(typeof(Resources.ReportsAPITitle))
{
}
}

public class DescriptionAttribute
: DescriptionLocalizedAttribute
{
public DescriptionAttribute()
: base(typeof(Resources.ReportsAPIDescription))
{
}
}

[ApiCommands]
abstract public class Commands
{
[ApiArgument(typeof(ReportTemplatesListArguments), isOptional: true)]
[ApiReturn(typeof(ReportTemplatesListResponse))]
[Title]
[Description]
public const string ReportTemplatesList = "report_templates_list";

[ApiArgument(typeof(ReportTemplatesGetArguments))]
[ApiReturn(typeof(ReportTemplatesGetResponse))]
[Title]
[Description]
public const string ReportTemplatesGet = "report_templates_get";

[ApiArgument(typeof(ReportsGenerateArguments))]
[ApiReturn(typeof(ReportsGenerateResponse))]
[Title]
[Description]
public const string ReportsGenerate = "reports_generate";
}

[ApiEvents]
abstract public class Events
{
[ApiReturn(typeof(ReportGenerateStartedEvent))]
[Title]
[Description]
[EventCategory(EventCategoryType.Audit)]
[EventIsVerbalized]
public const string ReportGenerateStarted = "report_generate_started";
}
}
[ApiEndpoint( Name, SignatureBase )]
[Title]
[Description]
abstract public class ReportsAPI
{
public const string Name = "reports";

public const string SignatureBase = "/system/report";

static public readonly EndpointSignature Signature =
SignatureBase;

public class TitleAttribute
: TitleLocalizedAttribute
{
public TitleAttribute()
: base(typeof(Resources.ReportsAPITitle))
{
}
}

public class DescriptionAttribute
: DescriptionLocalizedAttribute
{
public DescriptionAttribute()
: base(typeof(Resources.ReportsAPIDescription))
{
}
}

[ApiCommands]
abstract public class Commands
{
[ApiArgument(typeof(ReportTemplatesListArguments), isOptional: true)]
[ApiReturn(typeof(ReportTemplatesListResponse))]
[Title]
[Description]
public const string ReportTemplatesList = "report_templates_list";

[ApiArgument(typeof(ReportTemplatesGetArguments))]
[ApiReturn(typeof(ReportTemplatesGetResponse))]
[Title]
[Description]
public const string ReportTemplatesGet = "report_templates_get";

[ApiArgument(typeof(ReportsGenerateArguments))]
[ApiReturn(typeof(ReportsGenerateResponse))]
[Title]
[Description]
public const string ReportsGenerate = "reports_generate";
}

[ApiEvents]
abstract public class Events
{
[ApiReturn(typeof(ReportGenerateStartedEvent))]
[Title]
[Description]
[EventCategory(EventCategoryType.Audit)]
[EventIsVerbalized]
public const string ReportGenerateStarted = "report_generate_started";
}
}
so this class tell us that we have commands, we can see their arguments and return values and all I want is generate openapi documentation for another people documentation of this class
Jimmacle
Jimmacle2y ago
do you have an actual ASP.NET Core web api? because that's what the swagger integration works with
SWEETPONY
SWEETPONYOP2y ago
I don't have it right now, but I can easily create. Can swagger create documentation by this class for me? I don't think that swagger can work with custom attributes
Jimmacle
Jimmacle2y ago
it can't that's why you need an actual ASP.NET Core web api that's what it generates documentation from
SWEETPONY
SWEETPONYOP2y ago
https://editor.swagger.io/ but left side looks like a simple yaml idk
Jimmacle
Jimmacle2y ago
yes, you can write it by hand if you want. it's just a spec but if you want to use the ASP.NET Core integration to do it automatically, you have to use ASP.NET Core
SWEETPONY
SWEETPONYOP2y ago
ah okay can you recommend some libraries for that? I don't want to create it by my own
Jimmacle
Jimmacle2y ago
i don't know of any if you have all custom code there probably isn't an option
SWEETPONY
SWEETPONYOP2y ago
oh no anyway thanks for helping
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server