private void ResyncLocalToEnterpriseTime() { _lastAttemptedResyncInLocalTime = DateTime.Now; using (var client = _offlineCache.CreateClient()) { try { var serverTime = GetCurrentEnterpriseTime(); _lastSuccessfulResyncInLocalTime = _lastAttemptedResyncInLocalTime; _localToEnterpriseOffset = DateTime.Now - serverTime; // update offline cache client.Put(TimeOffsetCacheKey, _localToEnterpriseOffset.TotalMilliseconds.ToString(CultureInfo.InvariantCulture)); } catch (Exception) { if ((DateTime.Now - _lastSuccessfulResyncInLocalTime) > _maxTimeBetweenSync) { LogWarningNoSync(); } // if the process has just started up, and we have not yet been able to connect to the server, // attempt to read last known value from the offline cache if (_localToEnterpriseOffset == TimeSpan.Zero) { double value; var s = client.Get(TimeOffsetCacheKey); _localToEnterpriseOffset = !string.IsNullOrEmpty(s) && double.TryParse(s, NumberStyles.Float, CultureInfo.InvariantCulture, out value) ? TimeSpan.FromMilliseconds(value) : TimeSpan.Zero; } } } }
private void ResyncLocalToEnterpriseTime() { _lastAttemptedResyncInLocalTime = DateTime.Now; using (var client = _offlineCache.CreateClient()) { try { var serverTime = GetCurrentEnterpriseTime(); _lastSuccessfulResyncInLocalTime = _lastAttemptedResyncInLocalTime; _localToEnterpriseOffset = DateTime.Now - serverTime; // update offline cache client.Put(TimeOffsetCacheKey, _localToEnterpriseOffset.TotalMilliseconds.ToString()); } catch (Exception) { if ((DateTime.Now - _lastSuccessfulResyncInLocalTime) > _maxTimeBetweenSync) { LogWarningNoSync(); } // if the process has just started up, and we have not yet been able to connect to the server, // attempt to read last known value from the offline cache if (_localToEnterpriseOffset == TimeSpan.Zero) { var s = client.Get(TimeOffsetCacheKey); _localToEnterpriseOffset = (s == null) ? TimeSpan.Zero : TimeSpan.FromMilliseconds(double.Parse(s)); } } } }
private void StoreOffline(AuditEntryInfo entry) { try { using (var client = _offlineCache.CreateClient()) { // stick it in the offline cache // any unique value can be used as a key, because it will never be accessed by key again client.Put(Guid.NewGuid(), entry); } } catch (Exception e) { throw new AuditException(SR.ExceptionAuditServiceNotReachableAndNoOfflineCache, e); } }
public void PutSettingsValues(SettingsGroupDescriptor group, string user, string instanceKey, Dictionary <string, string> dirtyValues) { // note: if user == null, we are saving shared settings, if user is valued, we are saving user settings // but both are never edited as a single operation // the approach taken here is to create an XML document that represents a diff between // the default settings (as specified by the settings group meta-data) and the modified settings, // and store that document in the configuration store var service = Platform.GetService <IConfigurationService>(); using (service as IDisposable) using (var offlineCacheClient = _offlineCache.CreateClient()) { // first obtain the meta-data for the settings group properties var properties = ListSettingsProperties(group, service); // next we obtain any previously stored configuration document for this settings group var documentKey = new ConfigurationDocumentKey(group.Name, group.Version, user, instanceKey); var document = GetConfigurationDocument(service, documentKey); // parse document var parser = new SettingsParser(); var values = new Dictionary <string, string>(); parser.FromXml(document, values); // update the values that have changed foreach (var kvp in dirtyValues) { values[kvp.Key] = kvp.Value; } // now remove any values that are identical to the default values foreach (var property in properties) { string value; if (values.TryGetValue(property.Name, out value) && Equals(value, property.DefaultValue)) { values.Remove(property.Name); } } try { if (values.Count > 0) { // generate the document, update local cache and server document = parser.ToXml(values); offlineCacheClient.Put(documentKey, document); service.SetConfigurationDocument(new SetConfigurationDocumentRequest(documentKey, document)); } else { // every value is the same as the default, so the document can be removed // update local cache and server offlineCacheClient.Remove(documentKey); service.RemoveConfigurationDocument(new RemoveConfigurationDocumentRequest(documentKey)); } } catch (EndpointNotFoundException e) { Platform.Log(LogLevel.Debug, e, "Unable to save settings to configuration service."); } } }