azure-functionsswagger-uiazure-functions-isolated

OpenAPI extensions for Swagger in a v4 Azure Function - Swagger UI is blank


I'm trying to enable swagger in my v4 Azure Function. Followed examples as best I could. I see the swagger endpoints show up in the log window while running locally.

eg., localhost:8190/api/swagger/ui

However, when I navigate to the swagger UI endpoint, I see an HTTP 500 generated and a blank screen.

in program.cs

using Microsoft.Extensions.Hosting;
using Microsoft.Azure.Functions.Worker.Extensions.OpenApi.Extensions;

var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults()
    .ConfigureOpenApi() // Enable OpenAPI
    .Build();

host.Run();

Function class

using System.Net;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Enums;
using Microsoft.OpenApi.Models;
using Newtonsoft.Json;

namespace MyFunctionApp
{
    public static class MyFunction
    {
        [Function("MyFunction")]
        [OpenApiOperation(operationId: "Run", tags: new[] { "name" })]
        [OpenApiParameter(name: "name", In = ParameterLocation.Query, Required = true, Type = typeof(string), Description = "The **Name** parameter")]
        [OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "application/json", bodyType: typeof(string), Description = "The OK response")]
        public static HttpResponseData Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequestData req)
        {
            var query = System.Web.HttpUtility.ParseQueryString(req.Url.Query);
            string name = query["name"];

            var response = req.CreateResponse(HttpStatusCode.OK);
            response.Headers.Add("Content-Type", "application/json");

            var responseBody = JsonConvert.SerializeObject(new { message = $"Hello, {name}!" });
            response.WriteString(responseBody);

            return response;
        }
    }
}

[update]

Holy @#$#$@#$, I thought I would just start a new function app and keep the bare minimums. Low and behold, it worked. So then I compared what I had. Found I had this line hanging around in my program.cs

.ConfigureWebHostDefaults()

Took that out and my swagger ui showed up. That was obviously stepping on something.

Im off to go kick a brick wall now


Solution

  • I do agree and faced a similar issue then changed it to ConfigureFunctionsWebApplication , then it worked for me.

    Program.cs:

     using Microsoft.Azure.Functions.Worker;
     using Microsoft.Extensions.DependencyInjection;
     using Microsoft.Extensions.Hosting;
        
     var rith = new HostBuilder()
          .ConfigureFunctionsWebApplication()
          .ConfigureServices(test =>
            {
                test.AddApplicationInsightsTelemetryWorkerService();
                test.ConfigureFunctionsApplicationInsights();
            })
          .Build();
        
     rith.Run();
    

    Output:

    enter image description here