yuka1984 / azure-functions-extensions-swashbuckle

MIT License
33 stars 29 forks source link

Including swagger in a V3 function with "FunctionsStartup" #46

Open Cheranga opened 4 years ago

Cheranga commented 4 years ago

Hi, I have an Azure function (V3) and, for DI, I would like to use the FunctionsStartup rather than implementing IWebJobsStartup. When I implement IWebJobsStartup this works but not when I inherit from FunctionsStartup. I am pretty sure that, I am doing something fundamentally wrong here, but almost all of the search results lead to a solution which implements IWebJobsStartup directly.

Below is the error which I get. Any help is highly appreciated!

Microsoft.Azure.WebJobs.Host: Error indexing method 'Swagger'. Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'swashBuckleClient' to type ISwashBuckleClient. Make sure the parameter Type is supported by the binding. If you're using binding extensions (e.g. Azure Storage, ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. builder.AddAzureStorage(), builder.AddServiceBus(), build
er.AddTimers(), etc.).

My startup class is as below,

    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            var services = builder.Services;
            RegisterSwaggerDocumentation(services);
        }

        private void RegisterSwaggerDocumentation(IServiceCollection services)
        {
            if (services == null)
            {
                return;
            }

            services.AddSwaggerGen();

            //services.AddSwaggerGen(options =>
            //{
            //    options.SwaggerGeneratorOptions = new SwaggerGeneratorOptions
            //    {
            //        DescribeAllParametersInCamelCase = true
            //    };
            //});
        }
    }

The function I am testing is this,

    public class GetTodoFunction
    {
        private readonly ILogger<GetTodoFunction> _logger;

        public GetTodoFunction(ILogger<GetTodoFunction> logger)
        {
            _logger = logger;
        }

        [FunctionName(nameof(GetTodoFunction))]
        public async Task<IActionResult> GetTodoAsync([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "todos/{id}")]
            HttpRequest request, string id)
        {
            _logger.LogInformation($"{nameof(GetTodoFunction)} started.");

            await Task.Delay(TimeSpan.FromSeconds(1));

            return new OkObjectResult($"{id}");
        }
    }

The swagger functions are,

    public class SwaggerDocumentFunction
    {
        [SwaggerIgnore]
        [FunctionName("Swagger")]
        public Task<HttpResponseMessage> GenerateDocumentAsync(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "swagger/json")] HttpRequestMessage request,
            [SwashBuckleClient] ISwashBuckleClient swashBuckleClient)
        {
            return Task.FromResult(swashBuckleClient.CreateSwaggerDocumentResponse(request));
        }

        [SwaggerIgnore]
        [FunctionName("SwaggerUI")]
        public Task<HttpResponseMessage> GenerateUserInterfaceAsync(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "swagger/ui")] HttpRequestMessage request,
            [SwashBuckleClient] ISwashBuckleClient swashBuckleClient)
        {
            return Task.FromResult(swashBuckleClient.CreateSwaggerUIResponse(request, "swagger/json"));
        }
    }
dclash68 commented 3 years ago

Did you ever resolve this issue. I am seeing the same when I upgraded to V3.

Cheranga commented 3 years ago

Hi @dclash68 , no I haven't found a way to resolve this. @yuka1984 will you be able to help out please?