/// <summary>
        /// This method creates a service message and injects it in to the execution path and bypasses the listener infrastructure.
        /// </summary>
        /// <param name="dispatcher">The Microservice dispatcher.</param>
        /// <param name="header">The message header to identify the recipient.</param>
        /// <param name="package">The object package to process.</param>
        /// <param name="ChannelPriority">The priority that the message should be processed. The default is 1. If this message is not a valid value, it will be matched to the nearest valid value.</param>
        /// <param name="options">The process options.</param>
        /// <param name="release">The release action which is called when the payload has been executed by the receiving commands.</param>
        /// <param name="responseHeader">This is the optional response header</param>
        /// <param name="ResponseChannelPriority">This is the response channel priority. This will be set if the response header is not null. The default priority is 1.</param>
        /// <param name="originatorServiceId">This optional parameter allows you to set the originator serviceId</param>
        public static void Process(this IMicroserviceDispatch dispatcher
                                   , ServiceMessageHeader header
                                   , object package                      = null
                                   , int ChannelPriority                 = 1
                                   , ProcessOptions options              = ProcessOptions.RouteExternal | ProcessOptions.RouteInternal
                                   , Action <bool, Guid> release         = null
                                   , ServiceMessageHeader responseHeader = null
                                   , int ResponseChannelPriority         = 1
                                   , string originatorServiceId          = null
                                   )
        {
            var message = new ServiceMessage(header, responseHeader);

            if (originatorServiceId != null)
            {
                message.OriginatorServiceId = originatorServiceId;
            }

            message.ChannelPriority = ChannelPriority;

            message.Blob = SerializationHolder.CreateWithObject(package);
            //if (package != null)
            //    message.Blob = dispatcher.Serialization.PayloadSerialize(package);

            if (responseHeader != null)
            {
                message.ResponseChannelPriority = ResponseChannelPriority;
            }

            dispatcher.Process(message, options, release);
        }
        /// <summary>
        /// Serializes the summary JSON object in the holder and sets the byte array.
        /// </summary>
        /// <param name="holder">The holder to set.</param>
        public override void Serialize(SerializationHolder holder)
        {
            var stats = holder.Object as MicroserviceStatistics;

            if (stats == null)
            {
                throw new ArgumentOutOfRangeException("The holder object is not of type MicroserviceStatistics");
            }

            dynamic message = new ExpandoObject();

            message.Id     = stats.Id.ExternalServiceId;
            message.Status = stats.Status;
            message.TS     = DateTime.UtcNow.ToString("O");
            message.Engine = $"{stats.Id.ServiceVersionId}/{stats.Id.ServiceEngineVersionId}";
            message.Uptime = stats.Uptime;

            message.Active      = stats.Tasks.Availability.Active;
            message.Queued      = stats.Tasks.Queues.Waiting;
            message.Avalability = stats.Tasks.Availability;

            var authorData = JsonConvert.SerializeObject(message);

            //var authorData = JsonConvert.SerializeObject(stats);

            holder.SetBlob(Encoding.UTF8.GetBytes(authorData), maxLength: 508);
        }
예제 #3
0
        /// <summary>
        /// Returns true if the holder can be deserialized.
        /// </summary>
        /// <param name="holder">The holder.</param>
        /// <returns>
        /// Returns true if it can be deserialized.
        /// </returns>
        /// <exception cref="ArgumentNullException">holder</exception>
        public virtual bool SupportsDeserialization(SerializationHolder holder)
        {
            if (holder == null)
            {
                throw new ArgumentNullException("holder");
            }

            return((holder.ContentType ?? "").Equals(ContentType, StringComparison.InvariantCultureIgnoreCase));
        }
예제 #4
0
        /// <summary>
        /// Returns true if the Content in the holder can be serialized.
        /// </summary>
        /// <param name="holder">The holder.</param>
        /// <returns>
        /// Returns true if it can be serialized.
        /// </returns>
        /// <exception cref="ArgumentNullException">holder</exception>
        public virtual bool SupportsSerialization(SerializationHolder holder)
        {
            if (holder == null)
            {
                throw new ArgumentNullException("holder");
            }

            return(holder.HasObject);
        }
예제 #5
0
        /// <summary>
        /// Deserializes the specified holder.
        /// </summary>
        /// <param name="holder">The holder.</param>
        /// <exception cref="NotSupportedException">The deserialize action is not set.</exception>
        public override void Deserialize(SerializationHolder holder)
        {
            if (Deserializer == null)
            {
                throw new NotSupportedException("The deserialize action is not set.");
            }

            Deserializer(holder);
        }
        /// <summary>
        /// Checks the incoming holder to ensure that it is correctly configured.
        /// </summary>
        /// <param name="holder">The holder.</param>
        /// <returns>Returns true if the checks are passed.</returns>
        /// <exception cref="ArgumentNullException">holder</exception>
        protected virtual bool HolderChecks(SerializationHolder holder)
        {
            if (holder == null)
            {
                throw new ArgumentNullException("holder", "The serialization holder cannot be null.");
            }

            return(holder.Blob != null);
        }
        /// <summary>
        /// A boolean function that returns true if the compression type is supported.
        /// </summary>
        /// <param name="holder">The serialization holder.</param>
        /// <returns>
        /// Returns true when supported.
        /// </returns>
        /// <exception cref="ArgumentNullException">holder</exception>
        public virtual bool SupportsContentEncoding(SerializationHolder holder)
        {
            if (holder == null)
            {
                throw new ArgumentNullException("holder");
            }

            return(holder.ContentEncoding != null &&
                   string.Equals(holder.ContentEncoding, ContentEncoding, StringComparison.InvariantCultureIgnoreCase));
        }
        /// <summary>
        /// This method attempts to deserialize the binary blob and sets the object in the holder.
        /// </summary>
        /// <param name="holder">The serialization holder.</param>
        /// <returns>
        /// Returns true if the operation is successful.
        /// </returns>
        public bool TryPayloadDeserialize(SerializationHolder holder)
        {
            IPayloadSerializer serializer;

            if (!TryGetSerializer(holder.ContentType, out serializer))
            {
                return(false);
            }

            return(serializer.TryDeserialize(holder));
        }
예제 #9
0
        public override void Serialize(SerializationHolder holder)
        {
            using (var stream = new MemoryStream())
                using (var streamWriter = new StreamWriter(stream))
                    using (var textWriter = new JsonTextWriter(streamWriter))
                    {
                        mJsonSerializer.Serialize(textWriter, holder.Object);
                        streamWriter.Flush();
                        stream.Position = 0;
                        holder.SetBlob(stream.ToArray());
                    }

            holder.ContentType = ContentType + $"; type=\"{holder.Object.GetType().ToString()}\"";
        }
예제 #10
0
        /// <summary>
        /// Tries to serialize the outgoing payload.
        /// </summary>
        /// <param name="holder">The holder.</param>
        /// <returns>
        /// Returns true if the Content is serialized correctly to a binary blob.
        /// </returns>
        public virtual bool TrySerialize(SerializationHolder holder)
        {
            if (!SupportsSerialization(holder))
            {
                return(false);
            }

            try
            {
                Serialize(holder);
                return(true);
            }
            catch (Exception) { }

            return(false);
        }
예제 #11
0
        /// <summary>
        /// Returns true if the serializer supports this content type for serialization.
        /// </summary>
        /// <param name="holder">The holder.</param>
        /// <returns>
        /// Returns true if supported.
        /// </returns>
        public virtual bool SupportsContentTypeSerialization(SerializationHolder holder)
        {
            if (!holder.HasObject)
            {
                return(false);
            }

            Type cType = holder.ObjectType ?? holder.Object?.GetType();

            if (cType == null)
            {
                return(false);
            }

            return(SupportsContentTypeSerialization(cType));
        }
        /// <summary>
        /// This method deserializes the binary blob and returns the object.
        /// </summary>
        /// <param name="holder">The binary holder.</param>
        /// <returns>Returns the object deserialized from the binary blob.</returns>
        public object PayloadDeserialize(SerializationHolder holder)
        {
            if (holder?.Blob == null || mPayloadSerializers.Count == 0)
            {
                return(null);
            }

            var mimeType = PrepareMimeType(holder.ContentType);

            var serializer = mPayloadSerializers[mimeType];

            if (serializer != null &&
                serializer.TryDeserialize(holder))
            {
                return(holder.Object);
            }

            return(null);
        }
        /// <summary>
        /// This method deserializes the binary blob and returns the object.
        /// </summary>
        /// <typeparam name="P">The payload message type.</typeparam>
        /// <param name="holder">The binary holder.</param>
        /// <returns>Returns the object deserialized from the binary blob.</returns>
        public P PayloadDeserialize <P>(SerializationHolder holder)
        {
            if (holder?.Blob == null || mPayloadSerializers.Count == 0)
            {
                return(default(P));
            }

            var mimeType = PrepareMimeType(holder.ContentType);

            var serializer = mPayloadSerializers[mimeType];

            if (serializer != null &&
                serializer.TryDeserialize(holder) &&
                holder.ObjectType == typeof(P))
            {
                return((P)holder.Object);
            }

            return(default(P));
        }
        /// <summary>
        /// Encodes the blobs from compressed to uncompressed.
        /// </summary>
        /// <param name="holder">The holder.</param>
        /// <param name="getStream">The get decompressor stream function.</param>
        /// <returns>Returns true if encoded without error.</returns>
        protected virtual bool Decompress(SerializationHolder holder, Func <Stream, Stream> getStream)
        {
            try
            {
                using (MemoryStream msIn = new MemoryStream(holder.Blob))
                    using (Stream decompress = getStream(msIn))
                        using (MemoryStream msOut = new MemoryStream())
                        {
                            decompress.CopyTo(msOut);
                            decompress.Close();
                            holder.SetBlob(msOut.ToArray(), holder.ContentType);
                        }
            }
            catch (Exception ex)
            {
                return(false);
            }

            return(true);
        }
        /// <summary>
        /// Encodes the blobs from uncompressed to compressed.
        /// </summary>
        /// <param name="holder">The holder.</param>
        /// <param name="getStream">The get compressor stream function.</param>
        /// <param name="contentEncoding">The content encoding parameter.</param>
        /// <returns>Returns true if encoded without error.</returns>
        /// <exception cref="ArgumentNullException">holder</exception>
        protected virtual bool Compress(SerializationHolder holder, Func <Stream, Stream> getStream, string contentEncoding)
        {
            try
            {
                using (MemoryStream ms = new MemoryStream())
                    using (Stream compress = getStream(ms))
                    {
                        compress.Write(holder.Blob, 0, holder.Blob.Length);
                        compress.Close();

                        holder.SetBlob(ms.ToArray(), holder.ContentType, contentEncoding);
                    }
            }
            catch (Exception ex)
            {
                return(false);
            }

            return(true);
        }
 /// <summary>
 /// Tries to decompress the incoming holder.
 /// </summary>
 /// <param name="holder">The holder.</param>
 /// <returns>
 /// Returns true if the incoming binary payload is successfully decompressed.
 /// </returns>
 /// <exception cref="NotImplementedException"></exception>
 public virtual bool TryDecompression(SerializationHolder holder)
 {
     return(HolderChecks(holder) && Decompress(holder, GetDecompressionStream));
 }
 /// <summary>
 /// Tries to compress the outgoing payload.
 /// </summary>
 /// <param name="holder">The holder.</param>
 /// <returns>
 /// Returns true if the Content is compressed correctly to a binary blob.
 /// </returns>
 /// <exception cref="NotImplementedException"></exception>
 public virtual bool TryCompression(SerializationHolder holder)
 {
     return(HolderChecks(holder) && Compress(holder, GetCompressionStream, ContentEncoding));
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="SerializationHolderContentTypeException"/> class.
 /// </summary>
 /// <param name="holder">The holder.</param>
 /// <param name="expectedType">The expected content type.</param>
 public SerializationHolderContentTypeException(SerializationHolder holder, string expectedType)
 {
     ContentTypeExpected = expectedType;
     ContentTypeActual   = holder?.ContentType;
 }
예제 #19
0
 /// <summary>
 /// Returns true if the Content in the holder can be serialized.
 /// </summary>
 /// <param name="holder">The holder.</param>
 /// <returns>
 /// Returns true if it can be serialized.
 /// </returns>
 public override bool SupportsSerialization(SerializationHolder holder)
 {
     return(Serializer != null &&
            (FnCanSerialize?.Invoke(holder) ?? true) &&
            base.SupportsSerialization(holder));
 }
예제 #20
0
 public override void Deserialize(SerializationHolder holder)
 {
     //mJsonSerializer.Deserialize(
 }
예제 #21
0
 /// <summary>
 /// Serializes the specified object in the holder and sets the byte array.
 /// </summary>
 /// <param name="holder">The holder.</param>
 public abstract void Serialize(SerializationHolder holder);
 /// <summary>
 /// This method is not supported.
 /// </summary>
 /// <param name="holder">The holder.</param>
 /// <exception cref="NotImplementedException"></exception>
 public override void Deserialize(SerializationHolder holder)
 {
     throw new NotImplementedException();
 }
 /// <summary>
 /// Returns true if the Content in the holder can be serialized.
 /// </summary>
 /// <param name="holder">The holder.</param>
 /// <returns>
 /// Returns true if it can be serialized.
 /// </returns>
 public override bool SupportsSerialization(SerializationHolder holder)
 {
     return(base.SupportsSerialization(holder) && holder.ObjectType == typeof(MicroserviceStatistics));
 }