public Storage(ISettingsManager settingsManager, ILogger logger) { _logger = logger; switch (settingsManager.StorageStrategy) { case StorageStrategy.Speed: _engine = Engine.Create(); break; case StorageStrategy.Esent: _engine = Strategies.Esent.Engine.Create(); break; case StorageStrategy.MemoryMappedFiles: _engine = Strategies.Mmf.Engine.Create(); break; default: _engine = Engine.Create(); break; } _metadata = StorageMetadata.ReadMetadata(_logger); foreach (var databaseParameters in _metadata.Databases) { _engine.OpenDatabase(databaseParameters.Value); } _serializer = DefaultSerializer.Create(_metadata); }
/// <summary> /// Parses the error details from the stream. /// </summary> /// <param name="responseStream">The stream to parse.</param> /// <param name="cancellationToken">Cancellation token used to cancel the request.</param> /// <returns>The error details.</returns> public static async Task <StorageExtendedErrorInformation> ReadAndParseExtendedErrorAsync(Stream responseStream, CancellationToken cancellationToken) { try { StreamReader streamReader = new StreamReader(responseStream); using (JsonReader reader = new JsonTextReader(streamReader)) { reader.DateParseHandling = DateParseHandling.None; JObject dataSet = await JObject.LoadAsync(reader, cancellationToken).ConfigureAwait(false); Dictionary <string, object> properties = dataSet.ToObject <Dictionary <string, object> >(DefaultSerializer.Create()); StorageExtendedErrorInformation errorInformation = new StorageExtendedErrorInformation(); errorInformation.AdditionalDetails = new Dictionary <string, string>(); if (properties.ContainsKey(@"odata.error")) { Dictionary <string, object> errorProperties = ((JObject)properties[@"odata.error"]).ToObject <Dictionary <string, object> >(DefaultSerializer.Create()); if (errorProperties.ContainsKey(@"code")) { #pragma warning disable 618 errorInformation.ErrorCode = (string)errorProperties[@"code"]; #pragma warning restore 618 } if (errorProperties.ContainsKey(@"message")) { Dictionary <string, object> errorMessageProperties = ((JObject)errorProperties[@"message"]).ToObject <Dictionary <string, object> >(DefaultSerializer.Create()); if (errorMessageProperties.ContainsKey(@"value")) { errorInformation.ErrorMessage = (string)errorMessageProperties[@"value"]; } } if (errorProperties.ContainsKey(@"innererror")) { Dictionary <string, object> innerErrorDictionary = ((JObject)errorProperties[@"innererror"]).ToObject <Dictionary <string, object> >(DefaultSerializer.Create()); if (innerErrorDictionary.ContainsKey(@"message")) { errorInformation.AdditionalDetails[Constants.ErrorExceptionMessage] = (string)innerErrorDictionary[@"message"]; } if (innerErrorDictionary.ContainsKey(@"type")) { errorInformation.AdditionalDetails[Constants.ErrorException] = (string)innerErrorDictionary[@"type"]; } if (innerErrorDictionary.ContainsKey(@"stacktrace")) { errorInformation.AdditionalDetails[Constants.ErrorExceptionStackTrace] = (string)innerErrorDictionary[@"stacktrace"]; } } } return(errorInformation); } } catch (Exception) { // Exception cannot be parsed, better to throw the original exception than the error-parsing exception. return(null); } }
private static void WriteEntityContent(TableOperation operation, OperationContext ctx, JsonTextWriter jsonWriter) { ITableEntity entityToWrite = operation.Entity; Dictionary <string, object> propertyDictionary = new Dictionary <string, object>(); foreach (KeyValuePair <string, object> kvp in GetPropertiesWithKeys(entityToWrite, ctx, operation.OperationType)) { if (kvp.Value == null) { continue; } if (kvp.Value.GetType() == typeof(DateTime)) { propertyDictionary[kvp.Key] = ((DateTime)kvp.Value).ToUniversalTime().ToString("o", System.Globalization.CultureInfo.InvariantCulture); propertyDictionary[kvp.Key + Constants.OdataTypeString] = Constants.EdmDateTime; continue; } if (kvp.Value.GetType() == typeof(byte[])) { propertyDictionary[kvp.Key] = Convert.ToBase64String((byte[])kvp.Value); propertyDictionary[kvp.Key + Constants.OdataTypeString] = Constants.EdmBinary; continue; } if (kvp.Value.GetType() == typeof(Int64)) { propertyDictionary[kvp.Key] = kvp.Value.ToString(); propertyDictionary[kvp.Key + Constants.OdataTypeString] = Constants.EdmInt64; continue; } if (kvp.Value.GetType() == typeof(Guid)) { propertyDictionary[kvp.Key] = kvp.Value.ToString(); propertyDictionary[kvp.Key + Constants.OdataTypeString] = Constants.EdmGuid; continue; } if (kvp.Value.GetType() == typeof(Double)) { Double value = ((Double)kvp.Value); if (Double.IsNaN(value)) { propertyDictionary[kvp.Key] = "NaN"; } else if (Double.IsPositiveInfinity(value)) { propertyDictionary[kvp.Key] = "Infinity"; } else if (Double.IsNegativeInfinity(value)) { propertyDictionary[kvp.Key] = "-Infinity"; } else { propertyDictionary[kvp.Key] = Convert.ToString(kvp.Value, System.Globalization.CultureInfo.InvariantCulture); } propertyDictionary[kvp.Key + Constants.OdataTypeString] = Constants.EdmDouble; continue; } propertyDictionary[kvp.Key] = kvp.Value; } JObject json = JObject.FromObject(propertyDictionary, DefaultSerializer.Create()); json.WriteTo(jsonWriter); }
private static Dictionary <string, object> ReadSingleItem(JToken token, out string etag) { Dictionary <string, object> properties = token.ToObject <Dictionary <string, object> >(DefaultSerializer.Create()); // Parse the etag, and remove all the "odata.*" properties we don't use. if (properties.ContainsKey(@"odata.etag")) { etag = (string)properties[@"odata.etag"]; } else { etag = null; } foreach (string odataPropName in properties.Keys.Where(key => key.StartsWith(@"odata.", StringComparison.Ordinal)).ToArray()) { properties.Remove(odataPropName); } // We have to special-case timestamp here, because in the 'minimalmetadata' case, // Timestamp doesn't have an "@odata.type" property - the assumption is that you know // the type. if (properties.ContainsKey(@"Timestamp") && properties[@"Timestamp"].GetType() == typeof(string)) { properties[@"Timestamp"] = DateTime.Parse((string)properties[@"Timestamp"], CultureInfo.InvariantCulture); } // In the full metadata case, this property will exist, and we need to remove it. if (properties.ContainsKey(@"*****@*****.**")) { properties.Remove(@"*****@*****.**"); } // Replace all the 'long's with 'int's (the JSON parser parses all integer types into longs, but the odata spec specifies that they're int's. foreach (KeyValuePair <string, object> odataProp in properties.Where(kvp => (kvp.Value != null) && (kvp.Value.GetType() == typeof(long))).ToArray()) { // We first have to unbox the value into a "long", then downcast into an "int". C# will not combine the operations. properties[odataProp.Key] = (int)(long)odataProp.Value; } foreach (KeyValuePair <string, object> typeAnnotation in properties.Where(kvp => kvp.Key.EndsWith(@"@odata.type", StringComparison.Ordinal)).ToArray()) { properties.Remove(typeAnnotation.Key); string propName = typeAnnotation.Key.Split(new char[] { '@' }, StringSplitOptions.RemoveEmptyEntries)[0]; switch ((string)typeAnnotation.Value) { case Constants.EdmBinary: properties[propName] = Convert.FromBase64String((string)properties[propName]); break; case Constants.EdmBoolean: properties[propName] = Boolean.Parse((string)properties[propName]); break; case Constants.EdmDateTime: properties[propName] = DateTime.Parse((string)properties[propName], CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal); break; case Constants.EdmDouble: properties[propName] = Double.Parse((string)properties[propName], CultureInfo.InvariantCulture); break; case Constants.EdmGuid: properties[propName] = Guid.Parse((string)properties[propName]); break; case Constants.EdmInt32: properties[propName] = Int32.Parse((string)properties[propName], CultureInfo.InvariantCulture); break; case Constants.EdmInt64: properties[propName] = Int64.Parse((string)properties[propName], CultureInfo.InvariantCulture); break; case Constants.EdmString: properties[propName] = (string)properties[propName]; break; default: throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, SR.UnexpectedEDMType, typeAnnotation.Value)); } } return(properties); }