asp.net - Unauthorized error in c# while on background thread -
i have created asp.net web application in file needs deleted. below code performs operation perfectly
file.delete(path);
however when tried operation on background thread this
task.run(() => file.delete(path));
then throws exception access path denied. have tried giving iis_iusrs , iusr full access path, still error persists. can explain what's going on here. howcome file getting deleted first code i.e. on main thread, not on background thread. backgroung threads in c# run different identity?
the main difference between 2 approaches provided file.delete(path)
calls winapi function deletefile
immediately, , task.run(() => file.delete(path))
same delay.
something can happen during short period of time. e.g. windows search service can open file indexing, antivirus can open file analysis, or other process can (i assume) newly created file.
if sure application doesn't reopen file later , doesn't hold file handle anywhere, recommend use multiple attempts avoid such issues:
task.run(() => { const int attemptsnum = 10; (int attempt = 0; attempt < attemptsnum; attempt++) { try { file.delete(path); break; } catch { if (attempt == attemptsnum - 1) { throw; // or log exception here } else { await task.delay(500); } } } });
wiki
Comments
Post a Comment