cloudconvert / cloudconvert-dotnet

CloudConvert .NET SDK
Other
20 stars 15 forks source link

exportTask.Result is null despite successful job completion #22

Closed MateuszRosinski1 closed 9 months ago

MateuszRosinski1 commented 9 months ago

I'm currently working on integrating CloudConvert API into my application, and I'm encountering an issue with obtaining the result of a completed job.

After waiting for the job to complete using WaitJobAsync, the completedJob status is "finished," indicating that the job finished successfully. However, when I try to access exportTask.Result, it appears to be null.

Here is a snippet of my code:


    var completedJob = await _cloudConvert.WaitJobAsync(job.Data.Id);

    // Access export task
    var exportTask = completedJob.Data.Tasks.FirstOrDefault(t => t.Name == "export_task");

    // Attempt to access exportTask.Result, but it's null
    var exportFile = exportTask.Result.Files.FirstOrDefault(); // NullReferenceException here

    // Rest of the code...
}

I have confirmed that the job is completing successfully, but I'm unable to access the Result property of exportTask. Could you please provide guidance on what might be causing exportTask.Result to be null despite a successful job completion?

Thank you for your assistance.

josiasmontag commented 9 months ago

Are you sure your export task has the name export_task? Other than that, please use a debugger and check the objet attributes.

MateuszRosinski1 commented 9 months ago

here is the relevant code:

 var job = await _cloudConvert.CreateJobAsync(new JobCreateRequest
 {
     Tasks = new
     {
         import_task = new ImportUploadCreateRequest(),
         convert_task = new ConvertCreateRequest
         {
             Input = "import_task",
             Input_Format = "pdf",
             Output_Format = "docx"
         },
         export_task = new ExportUrlCreateRequest
         {
             Input = "convert_task",
             Archive_Multiple_Files = false
         }
     },
     Tag = "Test"
 });

 string path = @"D:/test/testpdf.pdf";
 byte[] file = await File.ReadAllBytesAsync(path);
 string filename = "testpdf.pdf";

 var uploadTask = job.Data.Tasks.FirstOrDefault(t => t.Name == "import_task");

 await _cloudConvert.UploadAsync(uploadTask.Result.Form.Url.ToString(), file, filename, uploadTask.Result.Form.Parameters);

 var completedJob = await _cloudConvert.WaitJobAsync(job.Data.Id);

 var exportTask = job.Data.Tasks.FirstOrDefault(t => t.Name == "export_task");

 var exportFile = exportTask.Result.Files.FirstOrDefault();

As you can she export task name is export_task, digging into the attributes i can she that the job status is finished image Regardless of that exportTask.Results = null image

MateuszRosinski1 commented 9 months ago

I found the solution and apologize for my foolishness and working in a hurry. I completely overlooked the fact that there should be two separate jobs, for example, 'job' and 'job2,' instead of one combined task. I won't delete this thread as a warning for other juniors. I apologize for the confusion and wasted time, and despite everything, I would like to thank you for the quick response. WRONG:

 var completedJob = await _cloudConvert.WaitJobAsync(job.Data.Id);

 var exportTask = job.Data.Tasks.FirstOrDefault(t => t.Name == "export_task");

 var exportFile = exportTask.Result.Files.FirstOrDefault();

GOOD:

 var completedJob = await _cloudConvert.WaitJobAsync(job.Data.Id);

 var exportTask =completedJob.Data.Tasks.FirstOrDefault(t => t.Name == "export_task"); //<-- here was a mistake.

 var exportFile = exportTask.Result.Files.FirstOrDefault();

I would like to mention that the opening comment, more precisely, the code located above this comment, is incorrect and was later corrected.

This is the part that confused me. As you can see in both screenshots, there is "job" in both cases. However, when combining these code snippets, they should be two separate jobs. In the WaitJobAsync(job.Data.Id method, it should, of course, be "the first job." creation of job with taskts image

creating completed job image

At the end, I would like to apologize again and wish you all good.