Viv*_*rma 3 .net c# azure azure-media-services
要了解 Azure 媒体服务,我正在关注教程:使用媒体服务 v3 上传、编码和流式传输视频。但目前卡在本文中给出的代码UploadEncodeAndStreamFilesRetry failed after 6 tries. (The operation was canceled.)在文件上传期间抛出异常await blob.UploadAsync(fileToUpload);。
private static async Task<Asset> CreateInputAssetAsync(
IAzureMediaServicesClient client,
string resourceGroupName,
string accountName,
string assetName,
string fileToUpload)
{
// In this example, we are assuming that the asset name is unique.
//
// If you already have an asset with the desired name, use the Assets.Get method
// to get the existing asset. In Media Services v3, the Get method on entities returns null
// if the entity doesn't exist (a case-insensitive check on the name).
// Call Media Services API to create an Asset.
// This method creates a container in storage for the Asset.
// The files (blobs) associated with the asset will be stored in this container.
Asset asset = await client.Assets.CreateOrUpdateAsync(resourceGroupName, accountName, assetName, new Asset());
// Use Media Services API to get back a response that contains
// SAS URL for the Asset container into which to upload blobs.
// That is where you would specify read-write permissions
// and the exparation time for the SAS URL.
var response = await client.Assets.ListContainerSasAsync(
resourceGroupName,
accountName,
assetName,
permissions: AssetContainerPermission.ReadWrite,
expiryTime: DateTime.UtcNow.AddHours(4).ToUniversalTime());
var sasUri = new Uri(response.AssetContainerSasUrls.First());
// Use Storage API to get a reference to the Asset container
// that was created by calling Asset's CreateOrUpdate method.
BlobContainerClient container = new BlobContainerClient(sasUri);
BlobClient blob = container.GetBlobClient(Path.GetFileName(fileToUpload));
// Use Strorage API to upload the file into the container in storage.
await blob.UploadAsync(fileToUpload);
return asset;
}
Run Code Online (Sandbox Code Playgroud)
以下是详细的异常和异常跟踪:
Count = 6
Data: {System.Collections.ListDictionaryInternal}
HResult: -2146233088
HelpLink: null
InnerException: {"The operation was canceled."}
InnerExceptions: Count = 6
Message: "Retry failed after 6 tries. (The operation was canceled.) (The operation was canceled.) (The operation was canceled.) (The operation was canceled.) (The operation was canceled.) (The operation was canceled.)"
Source: "Azure.Core"
StackTrace: " at Azure.Core.Pipeline.RetryPolicy.<ProcessAsync>d__11.MoveNext()\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Azure.Core.Pipeline.HttpPipelineSynchronousPolicy.<ProcessAsync>d__1.MoveNext()\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Azure.Core.Pipeline.HttpPipelineSynchronousPolicy.<ProcessAsync>d__1.MoveNext()\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Azure.Core.Pipeline.HttpPipelineSynchronousPolicy.<ProcessAsync>d__1.MoveNext()\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAnd
Run Code Online (Sandbox Code Playgroud)
DebuggerNotification(任务任务)\r\n 位于 Azure.Storage.Blobs.BlobRestClient.BlockBlob.d__0.MoveNext()\r\n 位于 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n 位于 System.Runtime。 CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\r\n 在 System.Threading.Tasks.ValueTask 1.get_Result()\r\n at System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable1.ConfiguredValueTaskAwaiter.GetResult()\r\n 在 Azure.Storage.Blobs.Specialized.BlockBlobClient.d__26.MoveNext()\r \n 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\r\n 在 System.Runtime.CompilerServices.ConfiguredTaskAwaitable 1.ConfiguredTaskAwaiter.GetResult()\r\n at Azure.Storage.Blobs.Specialized.BlockBlobClient.<>c__DisplayClass48_0.<<GetPartitionedUploaderBehaviors>b__0>d.MoveNext()\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Thro w()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult( )\r\n 在 Azure.Storage.PartitionedUploader 2.<UploadInternal>d__19.MoveNext()\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult()\r\n 在 Azure.Storage.Blobs.BlobClient.d__29.MoveNext()\r\n 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw ()\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\r\n 在 System.Runtime.CompilerServices.ConfiguredTaskAwaitable 1.ConfiguredTaskAwaiter.GetResult()\r\n at Azure.Storage.Blobs.BlobClient.<UploadAsync>d__28.MoveNext()\r\n at System.Runtime.ExceptionSe rvices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult()\r\n 在 Azure.Storage.Blobs。 BlobClient.d__20.MoveNext()\r\n 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\r\n 在 System.Runtime。 CompilerServices.ConfiguredTaskAwaitable 1.ConfiguredTaskAwaiter.GetResult()\r\n at Azure.Storage.Blobs.BlobClient.<UploadAsync>d__16.MoveNext()\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult()\r\n 在 Azure.Storage.Blobs.BlobClient.d__12.MoveNext()\r\n 在 System.Run time.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n 在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\r\n 位于 System.Runtime.CompilerServices.TaskAwaiter 1.GetResult()\r\n at UploadEncodeAndStreamFiles.Program.<CreateInputAssetAsync>d__7.MoveNext() in C:\\Study\\Test Source Code\\Video Portal\\media-services-v3-dotnet-tutorials-master\\AMSV3Tutorials\\UploadEncodeAndStreamFiles\\Program.cs:line 210\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter1.GetResult()\r\n 位于 C:\Study\ 中的 UploadEncodeAndStreamFiles.Program.d__4.MoveNext()测试源代码\Video Portal\media-services-v3-dotnet-tutorials-master\AMSV3Tutorials\UploadEncodeAndStreamFiles\Program.cs:第 82 行\r\n 位于 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n 处System.Runtime.Comp ilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\r\n 位于 System.Runtime.CompilerServices.TaskAwaiter.GetResult()\r\n 位于 C:\Study\ 中的 UploadEncodeAndStreamFiles.Program.d__3.MoveNext()测试源代码\Video Portal\media-services-v3-dotnet-tutorials-master\AMSV3Tutorials\UploadEncodeAndStreamFiles\Program.cs:line 34" TargetSite: {Void MoveNext()}
{"The operation was canceled."}
CancellationToken: IsCancellationRequested = true
Data: {System.Collections.ListDictionaryInternal}
HResult: -2146233029
HelpLink: null
InnerException: {"Error while copying content to a stream."}
Message: "The operation was canceled."
Source: "System.Net.Http"
StackTrace: " at System.Net.Http.HttpConnection.<SendAsyncCore>d__53.MoveNext()\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()\r\n at System.Net.Http.HttpConnectionPool.<SendWithNtConnectionAuthAsync>d__48.MoveNext()\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()\r\n at System.Net.Http.HttpConnectionPool.<SendWithRetryAsync>d__47.MoveNext()\r\n at System.Runtime.ExceptionServices.Exce
Run Code Online (Sandbox Code Playgroud)
ptionDispatchInfo.Throw()\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\r\n 在 System.Runtime。 CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult()\r\n at System.Net.Http.RedirectHandler.<SendAsync>d__4.MoveNext()\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult()\r\n 在 System.Net.Http.HttpClient.d__71.MoveNext()\r\n 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n 在 System.Runtime。 CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\r\n 在 System.Runtime.CompilerServices.TaskAwa iter.HandleNonSuccessAndDebuggerNotification(任务任务)\r\n 在 System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()\r \n 在 Azure.Core.Pipeline.HttpClientTransport.d__7.MoveNext()\r\n 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务) \r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\r\n 在 Azure.Core.Pipeline.RequestActivityPolicy.d__10.MoveNext()\r\n 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw ()\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\r\n 在 Azure.Core.Pipeline.RequestActivityPolicy .d__9.MoveNext()\r\n 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n 在 System.Runtime.Compiler Services.TaskAwaiter.ThrowForNonSuccess(任务任务)\r\n 在 System.Runtime。 CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\r\n 在 Azure.Core.Pipeline.ResponseBodyPolicy.d__5.MoveNext()\r\n 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n 在 System. Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\r\n 在 Azure.Core.Pipeline.ResponseBodyPolicy.d__3.MoveNext()\r\ n 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r\n 在 Azure.Core.Pipeline.LoggingPolicy.d__8.MoveNext()\r\n 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n 在 System.Runtime.CompilerServices.TaskAwaite r.ThrowForNonSuccess(Task任务)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\r\n 在 Azure.Core.Pipeline.LoggingPolicy.d__7.MoveNext()\r\n 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo .Throw()\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\r\n 在 Azure.Core.Pipeline .HttpPipelineSynchronousPolicy.d__1.MoveNext()\r\n 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\r\n 在 Azure.Core .Pipeline.RetryPolicy.d__11。MoveNext()" TargetSite: {Void MoveNext()} 任务:null
{"Error while copying content to a stream."}
Data: {System.Collections.ListDictionaryInternal}
HResult: -2146232800
HelpLink: null
InnerException: {"Unable to read data from the transport connection: The I/O operation has been aborted because of either a thread exit or an application request.."}
Message: "Error while copying content to a stream."
Source: "System.Net.Http"
StackTrace: " at System.Net.Http.HttpContent.<CopyToAsyncCore>d__49.MoveNext()\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Net.Http.HttpConnection.<SendRequestContentAsync>d__58.MoveNext()\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Net.Http.HttpConnection.<SendAsyncCore>d__53.MoveNext()"
TargetSite: {Void MoveNext()}
{"Unable to read data from the transport connection: The I/O operation has been aborted because of either a thread exit or an application request.."}
Data: {System.Collections.ListDictionaryInternal}
HResult: -2146232800
HelpLink: null
InnerException: {"The I/O operation has been aborted because of either a thread exit or an application request."}
Message: "Unable to read data from the transport connection: The I/O operation has been aborted because of either a thread exit or an application request.."
Source: "System.Net.Sockets"
StackTrace: " at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)\r\n at System.Net.Security.SslStream.<<WriteSingleChunk>g__CompleteAsync|210_1>d`1.MoveNext()\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Net.Security.SslStream.<WriteAsyncChunked>d__209`1.MoveNext()\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Net.Security.SslStream.<WriteAsyncInternal>d__216`1.MoveNext()\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.
Run Code Online (Sandbox Code Playgroud)
HandleNonSuccessAndDebuggerNotification(任务任务)\r\n 在 System.Net.Http.HttpConnection.d__66.MoveNext()\r\n 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n 在 System.Runtime.CompilerServices。 TaskAwaiter.ThrowForNonSuccess(任务任务)\r\n 在 System.IO.Stream.d__30.MoveNext()\r\n 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n 在 System.Runtime.CompilerServices。 TaskAwaiter.ThrowForNonSuccess(任务任务)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\r\n 在 Azure.Core.RequestContent.StreamContent.d__6.MoveNext()\r\n 在 System. Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\r\n 在Azure.Core.Pipeline.HttpClientTransport.PipelineRequest.PipelineContentAdapter.< SerializeToStreamAsync>d__8.MoveNext()\r\n 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (任务任务)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\r\n 在 System.Net.Http.HttpContent.d__49.MoveNext()" TargetSite: {Void ThrowException(System.Net) .Sockets.SocketError,System.Threading.CancellationToken)}
{"The I/O operation has been aborted because of either a thread exit or an application request."}
Data: {System.Collections.ListDictionaryInternal}
ErrorCode: 995
HResult: -2147467259
HelpLink: null
InnerException: null
Message: "The I/O operation has been aborted because of either a thread exit or an application request."
NativeErrorCode: 995
SocketErrorCode: OperationAborted
Source: null
StackTrace: null
TargetSite: null
Run Code Online (Sandbox Code Playgroud)
还有其他人遇到过这样的问题吗?如果是,您能帮助一下可能导致此问题的原因吗?我是否缺少导致此问题的权限?
小智 6
当您使用低带宽上传网络时,似乎会发生这种情况。你可以加
BlobUploadOptions upOptions = new BlobUploadOptions
{
TransferOptions = new StorageTransferOptions
{
MaximumTransferSize = 4 * 1024 * 1024,
InitialTransferSize = 4 * 1024 * 1024
}
};
// Use Strorage API to upload the file into the container in storage.
await blob.UploadAsync(file,upOptions);
Run Code Online (Sandbox Code Playgroud)
这将改变块大小并使其在较低带宽下工作。
小智 5
我同意杰森的回答,只是做了一些修改。
首先我会删除无限的 while 循环。其次,参考 github 问题线程,我认为最重要的一点是,在构造 或时BlobClientOptions应该传递。然后指定运输。正如线程
Tweaking Transport in BlobClientOptions中提到的BlobContainerClientBlobClient
new BlobClientOptions
{
Transport = new HttpClientTransport(
new HttpClient { Timeout = TimeSpan.FromSeconds(102) })
};
Run Code Online (Sandbox Code Playgroud)
我看到杰森的评论中的上述内容被注释掉了,但这将是解决问题的一个组成部分。
您还可以考虑延长重试期限,此请求需要超过 6 次尝试才能成功连接。
new BlobClientOptions // or whatever options class suitable for your client
{
Transport = new HttpClientTransport( new HttpClient { Timeout = Timeout.InfiniteTimeSpan } ),
Retry = { NetworkTimeout = Timeout.InfiniteTimeSpan }
}
Run Code Online (Sandbox Code Playgroud)