ā” Async Method with current code.

I am having a hell of a brain fart, probably my damn ASD again. Can someone help me fix this?
private static string GetFilePath(StringBuilder builder, TrackViewModel track, string downloadPath, FileFormat fileFormat)
{
// Change ReleaseDate format to yyyy-MM-dd (Added by Core Dream Studios)

DateTime formattedDate = track.ReleaseDate;
var formattedDateString = formattedDate.ToString("yyyy-MM-dd");

builder.Clear();
builder.Append(formattedDateString);
builder.Append(" - ");
builder.Append(track.ArtistsTitle);
builder.Append(" - ");
builder.Append(track.Title);

if (!string.IsNullOrWhiteSpace(track.Version))
{
builder.Append('(');
builder.Append(track.Version);
builder.Append(')');
}

builder.Append(GetFileExtension(fileFormat));

var fileName = builder.ToString().SanitizeAsFileName();

var archivedFolderPath = $@"{downloadPath}\{formattedDateString} - {track.CatalogId} - {track.Title}";

// Create a subfolder inside the users download path based on the year, catalog id then the artist.
if (!Directory.Exists(archivedFolderPath))
Directory.CreateDirectory(archivedFolderPath);

// Added a ability to download coverart.
// Credit to @mtreit (https://github.com/treit)
// Credit to @angius (https://github.com/Atulin)


var cover = $"https://cdx.monstercat.com/?width={3000}&encoding=jpg&url=https://www.monstercat.com/release/{track.CatalogId}/cover";
DownloadCoverArtAsync(cover, archivedFolderPath);

return Path.Combine(archivedFolderPath, fileName!);
}

private static async Task DownloadCoverArtAsync(string coverURL, string downloadPath)
{
var _client = new HttpClient();
using var stream = await _client.GetStreamAsync(coverURL);
using var fs = new FileStream($@"{downloadPath}\cover.jpg", FileMode.Create);

stream.CopyTo(fs);
}
private static string GetFilePath(StringBuilder builder, TrackViewModel track, string downloadPath, FileFormat fileFormat)
{
// Change ReleaseDate format to yyyy-MM-dd (Added by Core Dream Studios)

DateTime formattedDate = track.ReleaseDate;
var formattedDateString = formattedDate.ToString("yyyy-MM-dd");

builder.Clear();
builder.Append(formattedDateString);
builder.Append(" - ");
builder.Append(track.ArtistsTitle);
builder.Append(" - ");
builder.Append(track.Title);

if (!string.IsNullOrWhiteSpace(track.Version))
{
builder.Append('(');
builder.Append(track.Version);
builder.Append(')');
}

builder.Append(GetFileExtension(fileFormat));

var fileName = builder.ToString().SanitizeAsFileName();

var archivedFolderPath = $@"{downloadPath}\{formattedDateString} - {track.CatalogId} - {track.Title}";

// Create a subfolder inside the users download path based on the year, catalog id then the artist.
if (!Directory.Exists(archivedFolderPath))
Directory.CreateDirectory(archivedFolderPath);

// Added a ability to download coverart.
// Credit to @mtreit (https://github.com/treit)
// Credit to @angius (https://github.com/Atulin)


var cover = $"https://cdx.monstercat.com/?width={3000}&encoding=jpg&url=https://www.monstercat.com/release/{track.CatalogId}/cover";
DownloadCoverArtAsync(cover, archivedFolderPath);

return Path.Combine(archivedFolderPath, fileName!);
}

private static async Task DownloadCoverArtAsync(string coverURL, string downloadPath)
{
var _client = new HttpClient();
using var stream = await _client.GetStreamAsync(coverURL);
using var fs = new FileStream($@"{downloadPath}\cover.jpg", FileMode.Create);

stream.CopyTo(fs);
}
Thanks
57 Replies
Angius
Angius•2y ago
DownloadCoverArtAsync(cover, archivedFolderPath); await this call private static string GetFilePath(StringBuilder builder, TrackViewModel track, string downloadPath, FileFormat fileFormat) make this method async Task<string> And await it wherever you call it
Temporal Nightmare
Temporal NightmareOP•2y ago
Temporal Nightmare
Temporal NightmareOP•2y ago
that broke something else now 😦 CS0104 'File' is an ambiguous reference between 'System.Net.WebRequestMethods.File' and 'System.IO.File' S
Angius
Angius•2y ago
Remove the reference to System.Net.Web or whatever it is that brings this class
Temporal Nightmare
Temporal NightmareOP•2y ago
Temporal Nightmare
Temporal NightmareOP•2y ago
grrrrrr OK
Temporal Nightmare
Temporal NightmareOP•2y ago
Temporal Nightmare
Temporal NightmareOP•2y ago
nothing oh, i see it
Temporal Nightmare
Temporal NightmareOP•2y ago
Temporal Nightmare
Temporal NightmareOP•2y ago
nope
Temporal Nightmare
Temporal NightmareOP•2y ago
Angius
Angius•2y ago
There you have it
Temporal Nightmare
Temporal NightmareOP•2y ago
all because of the damn Task<string>
Angius
Angius•2y ago
Well, just await it If a method is async, await it await GetFilePath(...)
Temporal Nightmare
Temporal NightmareOP•2y ago
Temporal Nightmare
Temporal NightmareOP•2y ago
for fuck sake ALL I WANTED WAS A DAMN IMAGE :*(
Angius
Angius•2y ago
What's wrong now?
Temporal Nightmare
Temporal NightmareOP•2y ago
see the squiggly?
Angius
Angius•2y ago
Yes, but I don't see the error message
Temporal Nightmare
Temporal NightmareOP•2y ago
Temporal Nightmare
Temporal NightmareOP•2y ago
THIS IS FUCKED it was compiling fine before all of this
Angius
Angius•2y ago
Do you want help or do you just need to vent? If it's the former, provide error messages If it's the latter, let me know so I can go do some other things
Temporal Nightmare
Temporal NightmareOP•2y ago
sorry
Temporal Nightmare
Temporal NightmareOP•2y ago
Temporal Nightmare
Temporal NightmareOP•2y ago
Temporal Nightmare
Temporal NightmareOP•2y ago
this
Angius
Angius•2y ago
Well, your method returns a Task it seems void -> Task T -> Task<T>
Temporal Nightmare
Temporal NightmareOP•2y ago
i will repost code since we changed 100 things
Angius
Angius•2y ago
Right now the method is akin to void, but you're trying to return a string from it
Temporal Nightmare
Temporal NightmareOP•2y ago
private static async Task DownloadCoverArtAsync(string coverURL, string downloadPath)
{
var _client = new HttpClient();
using var stream = await _client.GetStreamAsync(coverURL);
using var fs = new FileStream($@"{downloadPath}\cover.jpg", FileMode.Create);

stream.CopyTo(fs);
}


private static async Task GetFilePath(StringBuilder builder, TrackViewModel track, string downloadPath, FileFormat fileFormat)
{
// Change ReleaseDate format to yyyy-MM-dd (Added by Core Dream Studios)

DateTime formattedDate = track.ReleaseDate;
var formattedDateString = formattedDate.ToString("yyyy-MM-dd");

builder.Clear();
builder.Append(formattedDateString);
builder.Append(" - ");
builder.Append(track.ArtistsTitle);
builder.Append(" - ");
builder.Append(track.Title);

if (!string.IsNullOrWhiteSpace(track.Version))
{
builder.Append('(');
builder.Append(track.Version);
builder.Append(')');
}

builder.Append(GetFileExtension(fileFormat));

var fileName = builder.ToString().SanitizeAsFileName();

var archivedFolderPath = $@"{downloadPath}\{formattedDateString} - {track.CatalogId} - {track.Title}";

// Create a subfolder inside the users download path based on the year, catalog id then the artist.
if (!Directory.Exists(archivedFolderPath))
Directory.CreateDirectory(archivedFolderPath);

// Added a ability to download coverart.
// Credit to @mtreit (https://github.com/treit)
// Credit to @angius (https://github.com/Atulin)


var cover = $"https://cdx.monstercat.com/?width={3000}&encoding=jpg&url=https://www.monstercat.com/release/{track.CatalogId}/cover";
await DownloadCoverArtAsync(cover, archivedFolderPath);

return Path.Combine(archivedFolderPath, fileName!);
}
private static async Task DownloadCoverArtAsync(string coverURL, string downloadPath)
{
var _client = new HttpClient();
using var stream = await _client.GetStreamAsync(coverURL);
using var fs = new FileStream($@"{downloadPath}\cover.jpg", FileMode.Create);

stream.CopyTo(fs);
}


private static async Task GetFilePath(StringBuilder builder, TrackViewModel track, string downloadPath, FileFormat fileFormat)
{
// Change ReleaseDate format to yyyy-MM-dd (Added by Core Dream Studios)

DateTime formattedDate = track.ReleaseDate;
var formattedDateString = formattedDate.ToString("yyyy-MM-dd");

builder.Clear();
builder.Append(formattedDateString);
builder.Append(" - ");
builder.Append(track.ArtistsTitle);
builder.Append(" - ");
builder.Append(track.Title);

if (!string.IsNullOrWhiteSpace(track.Version))
{
builder.Append('(');
builder.Append(track.Version);
builder.Append(')');
}

builder.Append(GetFileExtension(fileFormat));

var fileName = builder.ToString().SanitizeAsFileName();

var archivedFolderPath = $@"{downloadPath}\{formattedDateString} - {track.CatalogId} - {track.Title}";

// Create a subfolder inside the users download path based on the year, catalog id then the artist.
if (!Directory.Exists(archivedFolderPath))
Directory.CreateDirectory(archivedFolderPath);

// Added a ability to download coverart.
// Credit to @mtreit (https://github.com/treit)
// Credit to @angius (https://github.com/Atulin)


var cover = $"https://cdx.monstercat.com/?width={3000}&encoding=jpg&url=https://www.monstercat.com/release/{track.CatalogId}/cover";
await DownloadCoverArtAsync(cover, archivedFolderPath);

return Path.Combine(archivedFolderPath, fileName!);
}
latest
Angius
Angius•2y ago
It is exactly as I said
Temporal Nightmare
Temporal NightmareOP•2y ago
yes i NEED the string in order to do the file saving
Angius
Angius•2y ago
So... return a Task<string> Not a Task
Temporal Nightmare
Temporal NightmareOP•2y ago
oh so put return Task<string> Path.Combine(archivedFolderPath, fileName!); ?
Angius
Angius•2y ago
No
Temporal Nightmare
Temporal NightmareOP•2y ago
if I do Task<string> it breaks the entire app
Angius
Angius•2y ago
Change the return type of this method
Temporal Nightmare
Temporal NightmareOP•2y ago
this is not m,y app
Angius
Angius•2y ago
From Task to Task<string>
Temporal Nightmare
Temporal NightmareOP•2y ago
ok
Temporal Nightmare
Temporal NightmareOP•2y ago
Temporal Nightmare
Temporal NightmareOP•2y ago
ok well im just gonna remove the ability for cover art im not even getting paid 😦 everything is broken when I use Task<string>?
Angius
Angius•2y ago
Well, the last two errors are obvious at least Wherever you call this method, you don't await it Asynchronous code is "infectious"
Temporal Nightmare
Temporal NightmareOP•2y ago
i dont even need it awaited/async its ONE image
Angius
Angius•2y ago
Once something is async, the entire calling chain needs to be async all the way down to Main
Temporal Nightmare
Temporal NightmareOP•2y ago
im not downloading wikipedia here
Angius
Angius•2y ago
Just use synchronous versions of those methods then ĀÆ\_(惄)_/ĀÆ HttpClient should have a synchronous GetStream method
Temporal Nightmare
Temporal NightmareOP•2y ago
nope, ilooked
Temporal Nightmare
Temporal NightmareOP•2y ago
Temporal Nightmare
Temporal NightmareOP•2y ago
somehow that goddamn code broke this too and i dont know mvvm so im not touching this im just removing cover download they can add their own code its call gitHUB for a reason
mtreit
mtreit•2y ago
You want a synchronous version of downloading a file with HttpClient?
Temporal Nightmare
Temporal NightmareOP•2y ago
yes 😦
mtreit
mtreit•2y ago
var uri = "https://treit.github.io/images/bdn1.jpg";
var client = new HttpClient();
var msg = new HttpRequestMessage(HttpMethod.Get, uri);
using var response = client.Send(msg);
using var fs = new FileStream(@"c:\temp\bdn1.jpg", FileMode.Create);
var stream = response.Content.ReadAsStream();
stream.CopyTo(fs);
var uri = "https://treit.github.io/images/bdn1.jpg";
var client = new HttpClient();
var msg = new HttpRequestMessage(HttpMethod.Get, uri);
using var response = client.Send(msg);
using var fs = new FileStream(@"c:\temp\bdn1.jpg", FileMode.Create);
var stream = response.Content.ReadAsStream();
stream.CopyTo(fs);
Temporal Nightmare
Temporal NightmareOP•2y ago
i appreciate it
mtreit
mtreit•2y ago
Here you go
Temporal Nightmare
Temporal NightmareOP•2y ago
oh didn't know you could use HttpRequestMessage gonna add that for homework I need some air, letting my BP control my emotions heh I appreciate the non async version though if it was my project, id do it that way but this was someone elses hug @ZZZZZZZZZZZZZZZZZZZZZZZZZ and @mtreit i feel better now, took some water too heh i need to relax and try to google some more too
Accord
Accord•2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?