Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Startup setup #1

Open
ylbillyli opened this issue Oct 24, 2019 · 1 comment
Open

Startup setup #1

ylbillyli opened this issue Oct 24, 2019 · 1 comment

Comments

@ylbillyli
Copy link

Instead of building ServiceProvider on-the-fly just to resolve FluentValidationSchemaProcessor, I resolve FluentValidationSchemaProcessor during Configure(IApplicationBuilder app) into a private static variable (it is singleton anyway) and then use it in the SchemaProcessors.Add call

@geoffreytran
Copy link
Member

geoffreytran commented Oct 24, 2019

Thanks! I was looking at the NSwag extension code and it looks like AddOpenApiDocument passes in IServiceProvider through Action<AspNetCoreOpenApiDocumentGeneratorSettings, IServiceProvider>, so we can just use it directly without having to build the service provider also.

https://github.com/RicoSuter/NSwag/blob/1a6c959bdb41b41f2f534d3e61a311f1cf0de6f7/src/NSwag.AspNetCore/Extensions/NSwagServiceCollectionExtensions.cs#L40

/// <summary>Adds services required for Swagger 2.0 generation (change document settings to generate OpenAPI 3.0).</summary>
/// <param name="serviceCollection">The <see cref="IServiceCollection"/>.</param>
/// <param name="configure">Configure the document.</param>
public static IServiceCollection AddOpenApiDocument(this IServiceCollection serviceCollection, 
    Action<AspNetCoreOpenApiDocumentGeneratorSettings, IServiceProvider> configure = null)`
services.AddOpenApiDocument((document, serviceProvider) =>
{
        var fluentValidationSchemaProcessor = serviceProvider.GetService<FluentValidationSchemaProcessor>();
        document.SchemaProcessors.Add(fluentValidationSchemaProcessor);
});

I'll update the documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants