/// <summary> /// Configures the encoding of JSON stream /// </summary> /// <param name="config">The configuration object</param> /// <param name="encoding">Encoding to use for serialization and deserialization</param> public static void Encoding(this SerializationExtentions <JsonSerializer> config, Encoding encoding) { if (encoding == null) { throw new ArgumentNullException("encoding"); } config.Settings.SetProperty <JsonMessageSerializer>(s => s.Encoding, encoding); }
static async Task AsyncMain() { Console.Title = "Samples.Serialization.ExternalBson"; #region config EndpointConfiguration endpointConfiguration = new EndpointConfiguration("Samples.Serialization.ExternalBson"); SerializationExtentions <NewtonsoftSerializer> serialization = endpointConfiguration.UseSerialization <NewtonsoftSerializer>(); serialization.ReaderCreator(stream => new BsonReader(stream)); serialization.WriterCreator(stream => new BsonWriter(stream)); // register the mutator so the the message on the wire is written endpointConfiguration.RegisterComponents(components => { components.ConfigureComponent <MessageBodyWriter>(DependencyLifecycle.InstancePerCall); }); #endregion endpointConfiguration.SendFailedMessagesTo("error"); endpointConfiguration.UsePersistence <InMemoryPersistence>(); endpointConfiguration.EnableInstallers(); IEndpointInstance endpoint = await Endpoint.Start(endpointConfiguration); try { #region message CreateOrder message = new CreateOrder { OrderId = 9, Date = DateTime.Now, CustomerId = 12, OrderItems = new List <OrderItem> { new OrderItem { ItemId = 6, Quantity = 2 }, new OrderItem { ItemId = 5, Quantity = 4 }, } }; await endpoint.SendLocal(message); #endregion Console.WriteLine("Order Sent"); Console.WriteLine("Press any key to exit"); Console.ReadKey(); } finally { await endpoint.Stop(); } }
void Bson(EndpointConfiguration endpointConfiguration) { #region NewtonsoftBson 0.3-pre SerializationExtentions <NewtonsoftSerializer> serialization = endpointConfiguration.UseSerialization <NewtonsoftSerializer>(); serialization.ReaderCreator(stream => new BsonReader(stream)); serialization.WriterCreator(stream => new BsonWriter(stream)); #endregion }
/// <summary> /// Configures the serializer to use a custom namespace. (http://tempuri.net) is the default. /// <para>If the provided namespace ends with trailing forward slashes, those will be removed on the fly.</para> /// </summary> /// <param name="config">The <see cref="SerializationExtentions{T}"/> to add a namespace to.</param> /// <param name="namespaceToUse"> /// Namespace to use for interop scenarios. /// Note that this namespace is not validate or used for any logic inside NServiceBus. /// It is only for scenarios where a transport (or other infrastructure) requires message xml contents to have a specific namespace. /// </param> public static SerializationExtentions <XmlSerializer> Namespace(this SerializationExtentions <XmlSerializer> config, string namespaceToUse) { if (string.IsNullOrEmpty(namespaceToUse)) { throw new ConfigurationErrorsException("Can't use a null or empty string as the xml namespace"); } config.Settings.SetProperty <XmlMessageSerializer>(s => s.Namespace, namespaceToUse); return(config); }
void Bson(BusConfiguration busConfiguration) { #region NewtonsoftBson SerializationExtentions <NewtonsoftSerializer> serialization = busConfiguration.UseSerialization <NewtonsoftSerializer>(); serialization.ReaderCreator(stream => new BsonReader(stream)); serialization.WriterCreator(stream => new BsonWriter(stream)); #endregion }
static void Main() { Console.Title = "Samples.Serialization.ExternalBson"; #region config var busConfiguration = new BusConfiguration(); busConfiguration.EndpointName("Samples.Serialization.ExternalBson"); SerializationExtentions <NewtonsoftSerializer> serialization = busConfiguration.UseSerialization <NewtonsoftSerializer>(); serialization.ReaderCreator(stream => new BsonReader(stream)); serialization.WriterCreator(stream => new BsonWriter(stream)); // register the mutator so the the message on the wire is written busConfiguration.RegisterComponents(components => { components.ConfigureComponent <MessageBodyWriter>(DependencyLifecycle.InstancePerCall); }); #endregion busConfiguration.UsePersistence <InMemoryPersistence>(); busConfiguration.EnableInstallers(); using (var bus = Bus.Create(busConfiguration).Start()) { #region message var message = new CreateOrder { OrderId = 9, Date = DateTime.Now, CustomerId = 12, OrderItems = new List <OrderItem> { new OrderItem { ItemId = 6, Quantity = 2 }, new OrderItem { ItemId = 5, Quantity = 4 }, } }; bus.SendLocal(message); #endregion Console.WriteLine("Order Sent"); Console.WriteLine("Press any key to exit"); Console.ReadKey(); } }
/// <summary> /// Tells the serializer to sanitize the input data from illegal characters /// </summary> public static SerializationExtentions <XmlSerializer> SanitizeInput(this SerializationExtentions <XmlSerializer> config) { config.Settings.SetProperty <XmlMessageSerializer>(s => s.SanitizeInput, true); return(config); }
/// <summary> /// Tells the serializer to not wrap properties which have either XDocument or XElement with a "PropertyName" element. /// By default the xml serializer serializes the following message /// </summary> /// <code> /// interface MyMessage { XDocument Property { get; set; } } /// </code> /// into the following structure /// <code> /// <MyMessage> /// <Property> /// ... Content of the XDocument /// </Property> /// </MyMessage> /// </code> /// This flag allows to omit the property tag wrapping. Which results to /// <code> /// <MyMessage> /// ... Content of the XDocument /// </MyMessage> /// </code> /// When this feature is enable the root element of the XDocument must match the name of the property. The following would not work and lead to deserialization error: /// <code> /// <MyMessage> /// <Root> /// ... /// </Root> /// </MyMessage> /// </code> public static SerializationExtentions <XmlSerializer> DontWrapRawXml(this SerializationExtentions <XmlSerializer> config) { config.Settings.SetProperty <XmlMessageSerializer>(s => s.SkipWrappingRawXml, true); return(config); }