I am trying to make WPF application that uploads blobs to Azure Storage throught minimal web api with the help of HttpClient and displays upload progress at the same time. I have spent quite a while searching for solution, but haven't found anything that would work in this case. So far I have managed to do this. Here is my client code(WPF desktop app):
public void UploadFile(string path)
{
var memoryStream = new MemoryStream(File.ReadAllBytes(path));
var inputData = new StreamContent(memoryStream);
var handler = new HttpClientHandler();
var progressHandler = new ProgressMessageHandler(handler);
progressHandler.HttpSendProgress += (o, args) =>
{
};
var client = new HttpClient(progressHandler) { BaseAddress = new Uri("https://localhost:7088/") };
var response = await client.PostAsync("weatherforecast", inputData);
if (response.StatusCode == HttpStatusCode.OK)
{
}
}
In desktop app progressHandler reports only one time that is has uploaded all the data and it happens instantly when await body.CopyToAsync(memoryStream); in web api end point is hit (and the actual upload to Azure storage hasn't even started)
And then I have the server side code which receives Stream and then uploads it to the Azure Storage:
app.MapPost("/weatherforecast",async (Stream body) =>
{
try
{
using (var memoryStream = new MemoryStream())
{
await body.CopyToAsync(memoryStream);
var conStr = "myConString";
var blobContainerClient = new BlobContainerClient(conStr, "containerName");
BlobClient blobClient = blobContainerClient.GetBlobClient("MySample.txt");
var progressHandler = new Progress<long>();
progressHandler.ProgressChanged += (s, o) =>
{
//This reports progress as the file is uploaded
};
var options = new BlobUploadOptions
{
ProgressHandler = progressHandler
};
memoryStream.Position = 0;
await blobClient.UploadAsync(memoryStream, options);
return true;
}
}
catch (Exception ex)
{
throw;
}
});
For testing purposes I have a progressHandler here as well - this one reports the true upload progress which is the one I would like to see in my WPF desktop app. How do I pass this progress back to the caller? Is it even possible or I have to use some sort of third party library (for example SignalR) to achieve this? Please, don't be too harsh, these are my first steps in this area.
Here is the way i'm able to display progress bar and make WPF application that uploads blobs to Azure Storage through minimal web api with the help of HttpClient and displays upload progress
1. Create a Minimal Web API: Created a Web API to handle blob uploads.
2. Configure Azure Storage Connection String: In the
appsettings.json
file of your Web API project, add your Azure Storage connection string:3. Create the WPF Application: Created a WPF application that allows users to select a file and display the upload progress. You can use the
HttpClient
to upload the file to your Web API.Now, in your code-behind (MainWindow.xaml.cs), you can enable and show the progress bar only when the upload process is initiated:
Result