A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
Hi @Giorgio Sfiligoi ,
Thank you for reaching out on Microsoft Q&A!
The Java.Lang.RuntimeException you are seeing occurs only in Release mode on a physical device, and specifically on the first attempt to download a file via Microsoft Graph API might typically be caused by several reasons:
- In Release builds, the linker or Ahead-of-Time (AOT) compilation may remove certain code or assemblies that are required at runtime.
- On a physical device, network initialization or Graph API client setup may take slightly longer, causing the first call to fail intermittently.
- The
Content.GetAsync()method may throw if the network stream or file stream is not fully initialized on the first attempt.
Recommend solution: Implement a retry mechanism for the first download attempt. This ensures that if the call fails due to transient issues (network latency, initialization), it will automatically retry until success or until a timeout. You can do this easily by follow this example:
public async Task<Stream> DownloadFileWithRetry(GraphServiceClient graphClient, string driveId, string filePath)
{
int maxRetries = 5;
int delayMilliseconds = 500;
for (int attempt = 1; attempt <= maxRetries; attempt++)
{
try
{
var stream = await graphClient.Drives[driveId]
.Items[filePath]
.Content
.GetAsync();
return stream; // Success
}
catch (Java.Lang.RuntimeException ex) when (attempt < maxRetries)
{
// Optionally log the exception
await Task.Delay(delayMilliseconds); // wait before retrying
}
}
throw new Exception("Failed to download file after multiple attempts.");
}
Additional Recommendations:
- Clean & rebuild the solution in Release mode to remove stale build artifacts (
binandobjfolders). - Check linker settings: Ensure required assemblies, like
Microsoft.Graph, are preserved in Release mode. - Add logging: Log failures and retry attempts for easier troubleshooting.
- Check network permissions: Ensure the physical device has internet access and proper storage permissions if writing locally.
I hope this helps you get things back on track quickly! If my suggestions can solve your issue, feel free to interact with the system accordingly!
Thank you!