NET MAUI V8.0 App - Issues with XAML page and await client.GetObjectAsync() for AWS S3 bucket

Riffy 276 Reputation points
2024-03-14T22:47:23.2366667+00:00

Hello,

I am working on a NET MAUI V8 App that uses AWS S3 for file access. However, I am experiencing weird behavior when executing the following line of code:

using (GetObjectResponse response = await client.GetObjectAsync(request).ConfigureAwait(false))

This happens on all of my pages where I use the S3 code. Once the xaml page is displayed, the subsequent lines are processed. I don't want the page to appear as there is more code to be processed. Here's an extract of the code I'm using:

 using var client = new AmazonS3Client("test", "test", Amazon.RegionEndpoint.USEast1);
var bucketName = "mybucket";
var keyName = "myfile.txt";

GetObjectRequest request = new GetObjectRequest
{
    BucketName = bucketName,
    Key = keyName,
};

using (GetObjectResponse response = await client.GetObjectAsync(request).ConfigureAwait(false))
    using (Stream responseStream = response.ResponseStream)
    using (StreamReader reader = new StreamReader(responseStream))
    {
        string inputString;
        string line = "";
        while ((inputString = await reader.ReadLineAsync().ConfigureAwait(false)) != null)
        {
            string[] parts = inputString.Split('#');
             AddParts();
        }
        reader.Close();
    }

Not sure what is causing this issue so any help would be welcome.

Thanks

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,135 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,561 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
781 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 28,321 Reputation points Microsoft Vendor
    2024-03-18T06:33:58.2966667+00:00

    Hello,

    Once the xaml page is displayed, the subsequent lines are processed.

    The subsequent lines (using (GetObjectResponse response = await client.GetObjectAsync(request).ConfigureAwait(false))) are put in GetCount method in your ViewModel(HomeViewModel) class, and you call this method in the constructor of HomeViewModel, which means that this method is called when you create a HomeViewModel object, and the HomeViewModel object isn't created until this method has run.

    I don't want the page to appear as there is more code to be processed.

    You could call the method in Page.OnAppearing method (or Page.Loaded Event) instead of the constructor of ViewModel.

    For example:

    Page

    protected override async void OnAppearing()
    {
         base.OnAppearing();
         HomeViewModel homeViewModel = this.BindingContext as HomeViewModel;
         await homeViewModel.GetCount();
    }
    

    ViewMode l(I've omitted some methods)

    public class HomeViewModel :...
    {
         public HomeViewModel()
         {
            // _ = GetCount();
        }
    
        public async Task GetCount()
         {
            ...
         }
    }
    

    Best Regards,

    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments