Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Posted
Updated 29-Mar-16 23:00pm

Try putting an AsyncTimeout attribute on both the UploadVideos and Upload methods.
 
Share this answer
 
Comments
arslan afzal bhatti 30-Mar-16 2:31am    
already tried but same error occurred !
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900