i want to upload the video by using youtube upload data api but unfortunately when i tried to upload the video it generate the errot that A Task was Cancelled
This error is generate due to this line of code
await videosInsertRequest.UploadAsync(); in my code i already have apply the AsyncTimeout attribute my action is
[ AsyncTimeout ( 3600000 )] public async Task <ActionResult> YouTube( int ? Id) // await Run(); var dbListVideos = db.Videos.Where(v => v.Id == Id).ToList(); await YouTubeHandler.UploadVideos(dbListVideos); return View(); and this action invoke the API of youtube which is
public static async Task UploadVideos(List<MyVideo> videosList) foreach ( var vid in videosList) await Upload(vid); public static async Task Upload(MyVideo newVideo) UserCredential credential; using ( var stream = new FileStream( " G:\\client_secret_783534382593-0cn4gm229raqq97kdu0ghsj9hqfsc5o1.apps.googleusercontent.com.json" , FileMode.Open, FileAccess.Read)) credential = await GoogleWebAuthorizationBroker.AuthorizeAsync( GoogleClientSecrets.Load(stream).Secrets, // This OAuth 2.0 access scope allows an application to upload files to the // authenticated user's YouTube channel, but doesn't allow other types of access. new[] { YouTubeService.Scope.YoutubeUpload }, " user" , CancellationToken.None var youtubeService = new YouTubeService( new BaseClientService.Initializer() HttpClientInitializer = credential, ApplicationName = Assembly.GetExecutingAssembly().GetName().Name Video video = new Video(); video.Snippet = new VideoSnippet(); video.Snippet.Title = newVideo.Title; video.Snippet.Description = newVideo.Description; video.Snippet.Tags = new string[] { newVideo.Tags }; video.Snippet.CategoryId = newVideo.Category; // See https://developers.google.com/youtube/v3/docs/videoCategories/list video.Status = new VideoStatus(); video.Status.PrivacyStatus = newVideo.PrivacyStatus; // or "private" or "publi var filePath = newVideo.Path; // Replace with path to actual movie file. // YouTubeHandler t = new YouTubeHandler(); using ( var fileStream = new FileStream(filePath, FileMode.Open)) var videosInsertRequest = youtubeService.Videos.Insert(video, " snippet,status" , fileStream, " video/*" ); videosInsertRequest.ProgressChanged += videosInsertRequest_ProgressChanged; videosInsertRequest.ResponseReceived += videosInsertRequest_ResponseReceived; await videosInsertRequest.UploadAsync(); static void videosInsertRequest_ProgressChanged(Google.Apis.Upload.IUploadProgress progress) switch (progress.Status) case UploadStatus.Uploading: Console.WriteLine( " {0} bytes sent." , progress.BytesSent); break ; case UploadStatus.Failed: Console.WriteLine( " An error prevented the upload from completing.\n{0}" , progress.Exception); break ; static void videosInsertRequest_ResponseReceived(Video video) Console.WriteLine( " Video id '{0}' was successfully uploaded." , video.Id); What I have tried:
Stack Trace and Task Cancelled excepton is :
[ TaskCanceledException : A task was canceled.] Google.Apis.Upload.<UploadCoreAsync>d__87.MoveNext() in C:\Users\mdril\Documents\GitHub\google-api-dotnet-client\Src\Support\GoogleApis\Apis\[Media]\Upload\ResumableUpload.cs: 472 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +26 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +11795403 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 System.Runtime.CompilerServices.ConfiguredTaskAwaiter.GetResult() +28 Google.Apis.Upload.<UploadAsync>d__83.MoveNext() in C:\Users\mdril\Documents\GitHub\google-api-dotnet-client\Src\Support\GoogleApis\Apis\[Media]\Upload\ResumableUpload.cs: 388 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +26 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +11795403 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +28 Final.handler.<Upload>d__2.MoveNext() in C:\Users\Arslan Afzal\Documents\Visual Studio 2015\Projects\Final\Final\handler.cs: 81 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +26 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +11795403 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 System.Runtime.CompilerServices.TaskAwaiter.GetResult() +26 Final.handler.<UploadVideos>d__1.MoveNext() in C:\Users\Arslan Afzal\Documents\Visual Studio 2015\Projects\Final\Final\handler.cs: 26 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +26 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +11795403 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 System.Runtime.CompilerServices.TaskAwaiter.GetResult() +26 Final.Controllers.<YouTube>d__11.MoveNext() in C:\Users\Arslan Afzal\Documents\Visual Studio 2015\Projects\Final\Final\Controllers\HomeController.cs: 212 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +26 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +11795403 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 System.Runtime.CompilerServices.TaskAwaiter.GetResult() +26 System.Threading.Tasks.TaskHelpersExtensions.ThrowIfFaulted(Task task) +42 System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult) +73 System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeAsynchronousActionMethod>b__36(IAsyncResult asyncResult) +37 System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +27 System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +58 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +68 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +34 System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +69 System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +230 System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +230 System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +27 System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +27 System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +58 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +68 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34 System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +42 System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +124 System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +27 System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +58 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +30 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +29 System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +27 System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +48 System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +58 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +30 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +21 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +32 System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +26 System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +40 System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +58 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +30 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +21 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +29 System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +24 System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +27 System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +48 System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +58 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +30 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +21 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +29 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +23 System.Web.CallHandlerExecutionStep.OnAsyncHandlerCompletion(IAsyncResult ar) +129 ok i had found the solution of this problem You need to set the timeout before you use the service.
YouTubeService youtubeService = new YouTubeService(initializer); youtubeService.HttpClient.Timeout= TimeSpan.FromMinutes( 60 ); this will solve the task cancelled problem. Thank you
  • Read the question carefully.
  • Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  • If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  • Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question. Let's work to help developers, not make them feel stupid.
  •