LoginSignup
1
1

More than 1 year has passed since last update.

QueueServiceClientの利用時の注意点

Last updated at Posted at 2022-04-20

はじめに

.NET Core 3.1のEOLを前に.NET6への移行中に「Microsoft.Azure.Storage」が非推奨になっていたことが明らかになったため、「Azure.Storage.Queues」でのサンプルを実装し、稼働確認を行いました。

サンプル

エンキューのサンプル

public class EnqueueFunction
{
    [FunctionName("EnqueueFunction")]
    public void Run([TimerTrigger("*/10 * * * * *")]TimerInfo myTimer, ILogger log)
    {
        var queueItem = "テスト";
        log.LogInformation($"[{queueItem}]でQueueにエンキューを開始します。。");
        new QueueServiceClient("{{ConnectionString}}").GetQueueClient("myqueue-items").SendMessage(queueItem);
        log.LogInformation($"Queueへのエンキューが終了しました。");
    }
}

キュートリガーのサンプル

public class QueueTriggerFunction
{
    [FunctionName("QueueTriggerFunction")]
    public void Run([QueueTrigger("myqueue-items", Connection = "AzureWebJobsStorage")]string myQueueItem, ILogger log)
    {
        log.LogInformation($"[{myQueueItem}]を受信しました。");
    }
}

実行ログ

###### エンキューのサンプルログ ######
[テスト]でQueueにエンキューを開始します。。
Queueへのエンキューが終了しました。

###### キュートリガーのサンプルログ ######
System.Private.CoreLib: Exception while executing function: QueueTriggerFunction. Microsoft.Azure.WebJobs.Host: Exception binding parameter 'myQueueItem'. System.Private.CoreLib: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.

エラーの内容

System.Private.CoreLib: Exception while executing function:QueueTriggerFunction. 
Microsoft.Azure.WebJobs.Host: Exception binding parameter 'myQueueItem'. 
System.Private.CoreLib: 
The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, 
or an illegal character among the padding characters.

Base64でエンコードされていないとのこと。
以前は、「CloudQueueMessage」でBase64されていたが、その部分を実装しなければいけないのか?そんなことはなく、より開発しやすくなっており、QueueServiceClientのオプション設定でよいことがわかりました。

QueueClientOptions#MessageEncodingにBase64指定するだけの様子。

改修後
public class EnqueueFunction
{
    [FunctionName("EnqueueFunction")]
    public void Run([TimerTrigger("*/10 * * * * *")]TimerInfo myTimer, ILogger log)
    {
        var queueItem = "テスト";
        log.LogInformation($"[{queueItem}]でQueueにエンキューを開始します。。");
        new QueueServiceClient("{{ConnectionString}}", new QueueClientOptions()
        {
            MessageEncoding = QueueMessageEncoding.Base64
        }).GetQueueClient("myqueue-items").SendMessage(queueItem);
        log.LogInformation($"Queueへのエンキューが終了しました。");
    }
}

実際に稼働確認を行うと以下の通り正常に動作しました。

実行ログ

###### エンキューのサンプルログ ######
[テスト]でQueueにエンキューを開始します。。
Queueへのエンキューが終了しました。

###### キュートリガーのサンプルログ ######
[テスト]を受信しました。

まとめ

ちょっと焦りましたが、都度、CloudQueueMessageを経由してEnqueueする手間が省けるので、より一層使いやすいSDKになったなと感心しました。

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