public static Forecast GetForecast(string accessToken, string userId) { AWSXRayRecorder.Instance.BeginSubsegment("MyBudgetExplorer.Models.Cache.GetForecast()"); try { var binaryFormatter = new BinaryFormatter(); var aesKey = SHA256.Create().ComputeHash(Encoding.UTF8.GetBytes(userId)); var aesIV = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(userId)); var hash = BitConverter.ToString(aesKey).Replace("-", "").ToLower(); var fileName = $"forecast.{hash}"; var tempFilePath = Path.Combine(Path.GetTempPath(), fileName); byte[] encrypted = null; // Local File Forecast forecast = GetLocalForecast(userId); if (forecast != null) { return(forecast); } // Create Forecast forecast = Forecast.Create(accessToken, userId); // Serialize & Encrypt Forecast using (var ms = new MemoryStream()) { binaryFormatter.Serialize(ms, forecast); encrypted = EncryptAES(ms.ToArray(), aesKey, aesIV); } // Store Local File if (encrypted != null && encrypted.Length > 0) { File.WriteAllBytes(tempFilePath, encrypted); File.SetCreationTimeUtc(tempFilePath, forecast.LastModifiedOn); File.SetLastWriteTimeUtc(tempFilePath, forecast.LastModifiedOn); } return(forecast); } catch (Exception e) { AWSXRayRecorder.Instance.AddException(e); throw; } finally { AWSXRayRecorder.Instance.EndSubsegment(); } }