

第 4 版 (V4) 適用於 .NET 的 AWS SDK 已發行！

如需有關中斷變更和遷移應用程式的資訊，請參閱[遷移主題](https://docs.aws.amazon.com/sdk-for-net/v4/developer-guide/net-dg-v4.html)。

 [https://docs.aws.amazon.com/sdk-for-net/v4/developer-guide/net-dg-v4.html](https://docs.aws.amazon.com/sdk-for-net/v4/developer-guide/net-dg-v4.html)

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 將 FIFO 與適用於 .NET AWS 的訊息處理架構搭配使用
<a name="msg-proc-fw-fifo"></a>

對於訊息排序和訊息重複資料刪除至關重要的使用案例，.NET AWS 的訊息處理架構支援first-in-first-out(FIFO) [Amazon SQS 佇列](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-fifo-queues.html)和 [Amazon SNS 主題](https://docs.aws.amazon.com/sns/latest/dg/sns-fifo-topics.html)。

## 發布
<a name="mpf-fifo-publish"></a>

將訊息發佈至 FIFO 佇列或主題時，您必須設定訊息群組 ID，以指定訊息所屬的群組。群組內的訊息會依序處理。您可以在 SQS 特定和 SNS 特定訊息發佈者上設定此項目。

```
await _sqsPublisher.PublishAsync(message, new SQSOptions
{
    MessageDeduplicationId = <message-deduplication-id>,
    MessageGroupId = <message-group-id>
});
```

## 訂閱
<a name="mpf-fifo-subscribe"></a>

處理來自 FIFO 佇列的訊息時，框架會依照每次`ReceiveMessages`呼叫收到訊息的順序，來處理指定訊息群組內的訊息。框架在設定佇列以 結尾時，會自動進入此操作模式`.fifo`。

```
await Host.CreateDefaultBuilder(args)
    .ConfigureServices(services =>
    {
        // Register the AWS Message Processing Framework for .NET.
        services.AddAWSMessageBus(builder =>
        {
            // Because this is a FIFO queue, the framework automatically handles these messages in order.
            builder.AddSQSPoller("https://sqs.us-west-2.amazonaws.com/012345678910/MPF.fifo");
            builder.AddMessageHandler<OrderMessageHandler, OrderMessage>();
        });
    })
    .Build()
    .RunAsync();
```