private async static Task WritePayloadToSyncFileAsync(string lockFile, string protectedFile) { string pid = Process.GetCurrentProcess().Id.ToString(CultureInfo.InvariantCulture); string errorFile = protectedFile + $"{pid}.e.txt"; string pidFIle = Path.Combine(Path.GetDirectoryName(protectedFile), "finished", pid + ".txt"); Console.WriteLine("Starting process: " + pid); CrossPlatLock crossPlatLock = null; try { crossPlatLock = new CrossPlatLock(lockFile); using (StreamWriter sw = new StreamWriter(protectedFile, true)) { sw.WriteLine($"< {pid} {DateTime.UtcNow.ToString("o", CultureInfo.InvariantCulture)}"); // increase contention by simulating a slow writer await Task.Delay(s_artificialContention).ConfigureAwait(false); sw.WriteLine($"> {pid} {DateTime.UtcNow.ToString("o", CultureInfo.InvariantCulture)}"); Console.WriteLine("Process finished: " + pid); } } catch (Exception e) { File.WriteAllText(errorFile, e.ToString()); throw; } finally { File.WriteAllText(pidFIle, "done"); crossPlatLock.Dispose(); } }
// Triggered right after ADAL accessed the cache. private void AfterAccessNotification(TokenCacheNotificationArgs args) { _logger.TraceEvent(TraceEventType.Information, /*id*/ 0, $"After access"); try { // if the access operation resulted in a cache update if (HasStateChanged) { _logger.TraceEvent(TraceEventType.Information, /*id*/ 0, $"After access, cache in memory HasChanged"); try { _logger.TraceEvent(TraceEventType.Information, /*id*/ 0, $"Before Write Store"); byte[] data = SerializeAdalV3(); _logger.TraceEvent(TraceEventType.Information, /*id*/ 0, $"Serializing '{data.Length}' bytes"); _store.WriteData(data); _logger.TraceEvent(TraceEventType.Information, /*id*/ 0, $"After write store"); HasStateChanged = false; } catch (Exception e) { _logger.TraceEvent(TraceEventType.Error, /*id*/ 0, $"An exception was encountered while serializing the {nameof(AdalCache)} : {e}"); _logger.TraceEvent(TraceEventType.Error, /*id*/ 0, $"No data found in the store, clearing the cache in memory."); // The cache is corrupt clear it out DeserializeAdalV3(null); _store.Clear(); throw; } } } finally { _logger.TraceEvent(TraceEventType.Information, /*id*/ 0, $"Releasing lock"); _cacheLock?.Dispose(); _cacheLock = null; } }