1
0

More than 3 years have passed since last update.

Azure [Functions]、スタート

Posted at

Azure Functionsのクイックノート

事前に必要なもの

  • Visual Studio
  • Azure アカウント
  • Azure Storage アカウント(任意、この投稿ではAzure Storageアカウントを使っています)
  • VisualStudio Dev Op アカウント

プロジェクトのスタート

Visual Studioで新しいプロジェクトを開始する際にAzure Functionsを選択してください。

image.png

プロジェクト名、ソリューション名を指定します。

image.png

次にトリガーをセットします。
今回はWebhookに対してのアクションを設定したいので、APIを指定します。
Httpトリガーを指定してください。

image.png

自動的に下記のコードが生成されます。


using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

namespace ShopifyWebhookHandler
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.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;

            return name != null
                ? (ActionResult)new OkObjectResult($"Hello, {name}")
                : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
        }
    }
}

実行

上記のFunctionを実行します。

image.png

準備完了とのことで、下記のURLをブラウザーに入力しロードします。

image.png

実行できました。

次回は他のトリガータイプも紹介したいと思います

1
0
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
0