var filePath = startupPath + "\\" + $"test-{house.Id}.png";
var oAuthResponse = await MakeRequestAsync();
DropboxClient dbx = new DropboxClient(oAuthResponse.access_token);
var folder = "/Apps/MyFiles";
var file = $"{Guid.NewGuid()}.png";
var fileToUpload = @$"{filePath}";
using (var mem = new MemoryStream(File.ReadAllBytes(fileToUpload)))
{
var updated = await dbx.Files.UploadAsync(
folder + "/" + file,
Dropbox.Api.Files.WriteMode.Overwrite.Instance,
body: mem);
var urlHelper = await dbx.Files.GetTemporaryLinkAsync(updated.PathDisplay);
house.MainImage = urlHelper.Link;
db.Estates.Update(house);
db.SaveChanges();
Console.WriteLine("Dropboxa");
}
After this line:
DropboxClient dbx = new DropboxClient(oAuthResponse.access_token);
Debug cursor return the first line. So Application Started again but i dont get any exceptions.
This is Other method
public static async Task<OAuthResponse> MakeRequestAsync()
{
using (var httpClient = new HttpClient())
{
// URL and API credentials
string url = "https://api.dropbox.com/oauth2/token";
string clientId = "My_Key";
string clientSecret = "My_Client_Secret";
// Add basic authentication
var byteArray = System.Text.Encoding.ASCII.GetBytes($"{clientId}:{clientSecret}");
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
// Set up form data
var formData = new Dictionary<string, string>
{
{ "grant_type", "refresh_token" },
{ "refresh_token", "My_Token" }
};
try
{
HttpResponseMessage response = await httpClient.PostAsync(url, new FormUrlEncodedContent(formData));
if (response.IsSuccessStatusCode)
{
string content = await response.Content.ReadAsStringAsync();
// Deserialize the JSON response into an OAuthResponse object
OAuthResponse oauthResponse = System.Text.Json.JsonSerializer.Deserialize<OAuthResponse>(content)!;
Console.WriteLine("Access Token: " + oauthResponse.access_token);
Console.WriteLine("Token Type: " + oauthResponse.token_type);
Console.WriteLine("Expires In: " + oauthResponse.expires_in);
return oauthResponse;
}
else
{
Console.WriteLine($"Error: {response.StatusCode}");
}
}
catch (Exception e)
{
Console.WriteLine($"Error: {e.Message}");
}
return new OAuthResponse();
}
}
I think this bug For the MakeRequestAsync method. Because this is async method. But i cant change method async -> sync. This is C# Console App.
The most likely reason since this is a console app is that your main method is not async. Your console process will terminate when the "main" thread reaches the end of the main method, and this is likely before all async tasks has completed.
Change your main method signature to something like
public static async Task Main() {
await ...
}
and make sure all tasks are awaited, and you are not using any async void methods.