LoginSignup
1
1

Azure Functions の分離ワーカーモデルで OpenAPI/Swagger を使う

Last updated at Posted at 2024-05-10

2024/05/22 に公開された Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore バージョン 1.3.0 にすると Swagger が動かなくなります!

やりたいこと

  • Azure Functions の分離ワーカーモデルで OpenAPI/Swagger を有効化した HTTP トリガー Functions を作りたい

環境

  • Visual Studio Community 2022 17.9.6
  • .NET 8(C#12)

背景

  • .NET 6 の Azure Functions インプロセスモデルでは Function を作成する際に Http trigger with OpenAPI という選択肢があり、簡単に OpenAPI/Swagger を導入できた
  • 分離ワーカーモデルでは Http trigger with OpenAPI の選択肢がなくなってしまった
  • 分離ワーカーモデルでも Swagger を使いたい!

インプロセスモデルを確認する

Azure Functions プロジェクトの作成時に Function で Http trigger with OpenAPI を選択できます。

image.png

これを選択すると、OpenAPI の NuGet パッケージが追加され、Function に OpenAPI 関連の Attribute が追加されます。

image.png

Function1.cs
[FunctionName("Function1")]
[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: "text/plain", bodyType: typeof(string), Description = "The OK response")]
public async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req)
{
    _logger.LogInformation("C# HTTP trigger function processed a request.");

    string name = req.Query["name"];

    string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
    dynamic data = JsonConvert.DeserializeObject(requestBody);
    name = name ?? data?.name;

    string responseMessage = string.IsNullOrEmpty(name)
        ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
        : $"Hello, {name}. This HTTP triggered function executed successfully.";

    return new OkObjectResult(responseMessage);
}

デバッグ実行します。

image.png

swagger/ui にブラウザでアクセスします。

image.png

こいつを分離ワーカーモデルでも実現したい。

.NET 8 分離ワーカーモデルを実行してみる

仕方がないので Http trigger を選択します。

image.png

当然ですが、OpenAPI の NuGet パッケージは追加されていません。

image.png

デバッグ実行してもそこにあるのは Function だけ。

image.png

Function も驚くほど簡素(笑)

Function1.cs
[Function("Function1")]
public IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequest req)
{
    _logger.LogInformation("C# HTTP trigger function processed a request.");
    return new OkObjectResult("Welcome to Azure Functions!");
}

.NET 8 分離ワーカーモデルに OpenAPI/Swagger を追加する

まずは NuGet パッケージを追加しましょう。分離ワーカー(the out-of-process worker)向けと思われるパッケージがありました。

image.png

デバッグ実行をすると早速 Swagger が現れました!

image.png

image.png

Attribute を追加します。

Function1.cs
[Function("Function1")]
+[OpenApiOperation(operationId: "Run", tags: ["name"])]
+[OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "text/plain", bodyType: typeof(string), Description = "The OK response")]
public IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequest req)
{
    _logger.LogInformation("C# HTTP trigger function processed a request.");
    return new OkObjectResult("Welcome to Azure Functions!");
}

Swagger に表示されました!

image.png

まとめ

.NET 8 分離ワーカーモデルの Azure Functions でも .NET 6 インプロセスモデルと同様に Swagger を導入することができました。それにしてもなぜテンプレートで選べないんでしょうか。今後のアップデートに期待しておきます。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1