

第 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)

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

# 在 中支援 HTTP 2 適用於 .NET 的 AWS SDK
<a name="http2-support"></a>

有些 AWS 服務和操作需要 HTTP 2。例如，Amazon Transcribe Streaming 中的雙向串流無法透過 HTTP 1.1 進行，因此需要 HTTP 2。 適用於 .NET 的 AWS SDK 新增對 HTTP 2 的 第 4 版支援，讓您可以在應用程式中使用這些操作。對於雙向 HTTP 2 操作，接收串流時 SDK 的行為類似於 HTTP 1.1 的行為。也就是說，當使用 SDK 的應用程式將事件傳送到服務時，請求會有開發人員指派的發佈者。

若要查看此行為的實際運作狀態，請考慮下列 Amazon Transcribe Streaming 的範例。它使用 [Amazon.TranscribeStreaming](https://docs.aws.amazon.com/sdkfornet/v4/apidocs/items/TranscribeStreaming/NTranscribeStreaming.html) 和 [Amazon.TranscribeStreaming.Model](https://docs.aws.amazon.com/sdkfornet/v4/apidocs/items/TranscribeStreaming/NTranscribeStreamingModel.html) 命名空間。

在此範例中，開發人員使用回呼函數定義 `StartStreamTranscriptionRequest.AudioStreamPublisher` 屬性，即 .NET `Func`。開發套件使用 `Func` 定義的 `AudioStreamPublisher` ，從使用者的程式碼提取事件以串流至使用者。軟體開發套件會呼叫 ，`Func`直到傳回 null。

此程式碼示範如何將檔案的音訊串流至 Amazon Transcribe Streaming 進行處理。

```
using Amazon;
using Amazon.TranscribeStreaming;
using Amazon.TranscribeStreaming.Model;

CancellationTokenSource cancelSource = new CancellationTokenSource();

var client = new AmazonTranscribeStreamingClient(RegionEndpoint.USEast1);

var startRequest = new StartStreamTranscriptionRequest
{
    LanguageCode = LanguageCode.EnUS,
    MediaEncoding = MediaEncoding.Flac,
    MediaSampleRateHertz = 44100,
    NumberOfChannels = 2,
    EnableChannelIdentification = true
};

Stream fileStream = File.OpenRead("hello-world.flac");
var buffer = new byte[1024 * 10];
startRequest.AudioStreamPublisher += async () =>
{
    var bytesRead = await fileStream.ReadAsync(buffer, 0, buffer.Length);

    if (bytesRead == 0)
        return null;

    var audioEvent = new AudioEvent
    {
        AudioChunk = new MemoryStream(buffer, 0, bytesRead)
    };

    return audioEvent;
};

using var response = await client.StartStreamTranscriptionAsync(startRequest);
Console.WriteLine(response.HttpStatusCode);

response.TranscriptResultStream.ExceptionReceived += TranscriptResultStream_ExceptionReceived;
response.TranscriptResultStream.TranscriptEventReceived += TranscriptResultStream_TranscriptEventReceived;

void TranscriptResultStream_ExceptionReceived(object? sender, Amazon.Runtime.EventStreams.EventStreamExceptionReceivedArgs<TranscribeStreamingEventStreamException> e)
{
    Console.WriteLine(e.EventStreamException.Message);
    cancelSource.Cancel();
}
void TranscriptResultStream_TranscriptEventReceived(object? sender, Amazon.Runtime.EventStreams.EventStreamEventReceivedArgs<TranscriptEvent> e)
{
    foreach (var result in e.EventStreamEvent.Transcript.Results)
    {
        if (!string.Equals("ch_0", result.ChannelId, StringComparison.OrdinalIgnoreCase))
            continue;

        var text = result.Alternatives[0].Transcript;
        if (!string.IsNullOrEmpty(text))
        {
            Console.WriteLine(text);
        }
    }
}

_ = response.TranscriptResultStream.StartProcessingAsync();

try
{
    await Task.Delay(10000, cancelSource.Token);
}
catch (TaskCanceledException) { }
```

**警告**  
有些雙向 HTTP 2 操作，例如來自 Amazon Bedrock 的 `InvokeModelWithBidirectionalStreamAsync`方法，即 [Amazon.BedrockRuntime](https://docs.aws.amazon.com/sdkfornet/v4/apidocs/items/BedrockRuntime/NBedrockRuntime.html) 命名空間，在發佈某些事件之前，不會傳回來自服務用戶端上初始調用的回應。此行為可能會導致您的應用程式遭到封鎖。若要避免這種情況，請區隔向發佈者提供事件的應用程式程式碼，並在不同於在服務用戶端上叫用操作的執行緒上執行。

## 其他考量
<a name="http2-support-additional"></a>
+ 適用於 .NET 的 AWS SDK HTTP 2 的支援僅適用於以 .NET 8 及更高版本為目標的版本。它不適用於以 .NET Framework 為目標的版本。
+ 如需詳細資訊，請參閱 [aws-sdk-net](https://github.com/aws/aws-sdk-net) GitHub 儲存庫中的 [PR 3730](https://github.com/aws/aws-sdk-net/pull/3730)。