public WriteName ( string name ) : void | ||
name | string | The name of the element. |
Résultat | void |
public static BsonValue SerializeValue(this ISerializationExpression field, object value) { Ensure.IsNotNull(field, nameof(field)); var tempDocument = new BsonDocument(); using (var bsonWriter = new BsonDocumentWriter(tempDocument)) { var context = BsonSerializationContext.CreateRoot(bsonWriter); bsonWriter.WriteStartDocument(); bsonWriter.WriteName("value"); field.Serializer.Serialize(context, value); bsonWriter.WriteEndDocument(); return tempDocument[0]; } }
public static BsonArray SerializeValues(this ISerializationExpression field, IEnumerable values) { var tempDocument = new BsonDocument(); using (var bsonWriter = new BsonDocumentWriter(tempDocument)) { var context = BsonSerializationContext.CreateRoot(bsonWriter); bsonWriter.WriteStartDocument(); bsonWriter.WriteName("values"); bsonWriter.WriteStartArray(); foreach (var value in values) { field.Serializer.Serialize(context, value); } bsonWriter.WriteEndArray(); bsonWriter.WriteEndDocument(); return (BsonArray)tempDocument[0]; } }
/// <summary> /// /// </summary> /// <param name="storeData"></param> /// <returns></returns> private static BsonDocument ConvertStoreDataToBsonDocument(SessionStateStoreData storeData) { var items = storeData.Items; var document = new BsonDocument(); var documentWriterSettings = new BsonDocumentWriterSettings(); var documentWriter = new BsonDocumentWriter(document, documentWriterSettings); documentWriter.WriteStartDocument(); foreach (string key in items.Keys) { var value = items[key]; documentWriter.WriteName(key); BsonSerializer.Serialize(documentWriter, value.GetType(), value); } documentWriter.WriteEndDocument(); return document; }
public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection values) { if (context == null) { throw TraceException("SetPropertyValues", new ArgumentNullException("context")); } if (values == null) { throw TraceException("SetPropertyValues", new ArgumentNullException("values")); } var userName = (string) context["UserName"]; var isAuthenticated = (bool) context["IsAuthenticated"]; if (string.IsNullOrWhiteSpace(userName) || values.Count == 0) { return; } var updateValues = (from SettingsPropertyValue value in values let allowAnonymous = value.Property.Attributes["AllowAnonymous"].Equals(true) where (value.IsDirty || !value.UsingDefaultValue) && (isAuthenticated || allowAnonymous) select value).ToList(); // If there are no values to update, then we're done here. if (updateValues.Count == 0) { return; } // If the user doesn't exist, and it's anonymous, create it. var user = GetMongoUser(userName); if (user == null) { if (!isAuthenticated) { user = new MongoMembershipUser { UserName = userName, IsAnonymous = true, CreationDate = DateTime.Now, }; try { var users = GetUserCollection(); users.Insert(user); } catch (MongoSafeModeException e) { var message = ProviderResources.CouldNotCreateUser; throw TraceException("SetPropertyValues", new ProviderException(message, e)); } } else { var message = ProviderResources.CouldNotFindUser; throw TraceException("SetPropertyValues", new ProviderException(message)); } } // Create the properties BSON document. var properties = new BsonDocument(); var propertiesWriterSettings = new BsonDocumentWriterSettings(); var propertiesWriter = new BsonDocumentWriter(properties, propertiesWriterSettings); propertiesWriter.WriteStartDocument(); foreach (var value in updateValues) { propertiesWriter.WriteName(value.Name); switch (value.Property.SerializeAs) { case SettingsSerializeAs.String: case SettingsSerializeAs.Xml: BsonSerializer.Serialize(propertiesWriter, typeof (string), value.SerializedValue); break; case SettingsSerializeAs.Binary: BsonSerializer.Serialize(propertiesWriter, typeof (byte[]), value.SerializedValue); break; case SettingsSerializeAs.ProviderSpecific: BsonSerializer.Serialize(propertiesWriter, value.Property.PropertyType, value.PropertyValue); break; default: throw TraceException("SetPropertyValues", new ArgumentOutOfRangeException()); } } propertiesWriter.WriteEndDocument(); // Create the profile BSON document. var profile = SerializationHelper.Serialize(typeof (MongoProfile), new MongoProfile { Properties = properties, LastActivityDate = DateTime.Now, LastUpdateDate = DateTime.Now }); try { var query = Query.EQ("UserName", userName); var update = Update.Set("Profile", profile); var users = GetUserCollection(); users.Update(query, update); } catch (MongoSafeModeException e) { var message = ProviderResources.CouldNotUpdateProfile; throw TraceException("SetPropertyValues", new ProviderException(message, e)); } }
/// <summary> /// Serializes the value. /// </summary> /// <param name="value">The value.</param> /// <returns>The serialized value.</returns> public BsonValue SerializeValue(object value) { var tempDocument = new BsonDocument(); using (var bsonWriter = new BsonDocumentWriter(tempDocument)) { var context = BsonSerializationContext.CreateRoot<BsonDocument>(bsonWriter); bsonWriter.WriteStartDocument(); bsonWriter.WriteName("value"); context.SerializeWithChildContext(_serializer, value); bsonWriter.WriteEndDocument(); return tempDocument[0]; } }
/// <summary> /// Serializes the values. /// </summary> /// <param name="values">The values.</param> /// <returns>The serialized values.</returns> public BsonArray SerializeValues(IEnumerable values) { var tempDocument = new BsonDocument(); using (var bsonWriter = new BsonDocumentWriter(tempDocument)) { var context = BsonSerializationContext.CreateRoot<BsonDocument>(bsonWriter); bsonWriter.WriteStartDocument(); bsonWriter.WriteName("values"); bsonWriter.WriteStartArray(); foreach (var value in values) { context.SerializeWithChildContext(_serializer, value); } bsonWriter.WriteEndArray(); bsonWriter.WriteEndDocument(); return tempDocument[0].AsBsonArray; } }