'Why my program is not executing beyond await UploadAsync?

I am trying to upload a file through stream to azure file share. This is my function :

public static async Task UploadFile(string shareName, Stream content, string fileName, string dirName)
        {
            var shareClient = Common.CreateSMBClientFromConnectionString(shareName);
            ShareDirectoryClient directory = shareClient.GetDirectoryClient(dirName);
            ShareFileClient file = directory.GetFileClient(fileName);
            await file.CreateAsync(content.Length);
            await file.UploadAsync(content);
        }

I calling this function by the following command:

SMBHelper.UploadFile("filesharetester", rps, "hereischecking.txt", "checking/lp").GetAwaiter();

The program shows no error but while debugging the program I see that the pointer get lost whenever statement containing await arrives. Like in this case program automatically stopped working when statement await file.CreateAsync(content.Length); arrives.



Solution 1:[1]

The most likely explanation is that file.CreateAsync fails. That will cause the returned task to also fail. But since you do not check the result of the returned task, you will never know about the failure. This can be fixed by awaiting the task and using try/catch to handle any errors:

try{
    await SMBHelper.UploadFile(...)
}
catch(Exception e){
    // Handle error
}

Another possibility might be that the file.CreateAsync just never completes, you should be able to use the Task-view in visual studio to check for this.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JonasH