/// <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.Holder = ServiceHandlerContext.CreateWithObject(package); //if (package != null) // message.Blob = dispatcher.Serialization.PayloadSerialize(package); if (responseHeader != null) { message.ResponseChannelPriority = ResponseChannelPriority; } dispatcher.Process(message, options, release); }
/// <summary> /// Tries to compress the outgoing holder. /// </summary> /// <param name="collection">The collection.</param> /// <param name="item">The item.</param> /// <returns>Returns true if the Content is serialized correctly to a binary blob.</returns> public static byte[] SerializeToBlob(this ServiceHandlerCollection <IServiceHandlerSerialization> collection, object item) { var context = ServiceHandlerContext.CreateWithObject(item); //context.ContentType = collection. if (collection.TrySerialize(context)) { return(context.Blob); } throw new PayloadSerializationException(); }
/// <summary> /// This method marshals the RepositoryHolder and transmits it to the remote Microservice. /// </summary> /// <typeparam Name="KT">The key type.</typeparam> /// <typeparam Name="ET">The entity type.</typeparam> /// <param Name="actionType">The action type.</param> /// <param Name="rq">The repository holder request.</param> /// <returns>Returns an async task that will be signalled when the request completes or times out.</returns> protected override async Task <RepositoryHolder <KT, ET> > TransmitInternal <KT, ET>( string actionType, RepositoryHolder <KT, ET> rq, ProcessOptions?routing = null, IPrincipal principal = null) { try { StatisticsInternal.ActiveIncrement(); var payload = TransmissionPayload.Create(Policy.TransmissionPayloadTraceEnabled); payload.SecurityPrincipal = TransmissionPayload.ConvertToClaimsPrincipal(principal ?? Thread.CurrentPrincipal); // Set the process correlation key to the correlation id if passed through the rq settings if (!string.IsNullOrEmpty(rq.Settings?.CorrelationId)) { payload.Message.ProcessCorrelationKey = rq.Settings.CorrelationId; } bool processAsync = rq.Settings?.ProcessAsync ?? false; payload.Message.ChannelPriority = processAsync ? 0 : 1; payload.Options = routing ?? RoutingDefault ?? ProcessOptions.RouteExternal; payload.Message.Holder = ServiceHandlerContext.CreateWithObject(rq); payload.Message.ResponseChannelId = ResponseChannelId; payload.Message.ResponseChannelId = ResponseId.Header.ChannelId; payload.Message.ResponseMessageType = ResponseId.Header.MessageType; payload.Message.ResponseActionType = ResponseId.Header.ActionType; payload.Message.ResponseChannelPriority = payload.Message.ChannelPriority; payload.Message.ChannelId = ChannelId; payload.Message.MessageType = EntityType; payload.Message.ActionType = actionType; payload.MaxProcessingTime = rq.Settings?.WaitTime ?? mDefaultRequestTimespan; return(await OutgoingRequestOut(payload, ProcessResponse <KT, ET>, processAsync)); } catch (Exception ex) { string key = rq != null && rq.Key != null?rq.Key.ToString() : string.Empty; Collector?.LogException($"Error transmitting {actionType}-{key} internally", ex); throw; } }
protected virtual void WriteEntity(FileStream fs, E entity) { var json = Transform.JsonMaker(entity); var holder = ServiceHandlerContext.CreateWithObject(entity); }
/// <summary> /// This method is used to send requests to the remote command. /// </summary> /// <typeparam name="RQ">The request type.</typeparam> /// <typeparam name="RS">The response type.</typeparam> /// <param name="channelId">The header routing information.</param> /// <param name="messageType">The header routing information.</param> /// <param name="actionType">The header routing information.</param> /// <param name="rq">The request object.</param> /// <param name="rqSettings">The request settings. Use this to specifically set the timeout parameters.</param> /// <param name="routingOptions">The routing options by default this will try internal and then external.</param> /// <param name="processResponse"></param> /// <param name="fallbackMaxProcessingTime">This is the fallback max processing time used if the timeout /// is not set in the request settings. /// If this is also null, the max time out will fall back to the policy settings.</param> /// <param name="principal">This is the principal that you wish the command to be executed under. /// By default this is taken from the calling thread if not passed.</param> /// <returns>Returns the async response wrapper.</returns> protected internal virtual async Task <ResponseWrapper <RS> > ProcessOutgoing <RQ, RS>( string channelId, string messageType, string actionType , RQ rq , RequestSettings rqSettings = null , ProcessOptions?routingOptions = null , Func <TaskStatus, TransmissionPayload, bool, ResponseWrapper <RS> > processResponse = null , TimeSpan?fallbackMaxProcessingTime = null , IPrincipal principal = null ) { if (!Policy.OutgoingRequestsEnabled) { throw new OutgoingRequestsNotEnabledException(); } TransmissionPayload payload = null; try { StatisticsInternal.ActiveIncrement(); payload = TransmissionPayload.Create(Policy.TransmissionPayloadTraceEnabled); payload.SecurityPrincipal = TransmissionPayload.ConvertToClaimsPrincipal(principal ?? Thread.CurrentPrincipal); // Set the process correlation key to the correlation id, if passed through the request settings if (!string.IsNullOrEmpty(rqSettings?.CorrelationId)) { payload.Message.ProcessCorrelationKey = rqSettings.CorrelationId; } bool processAsync = rqSettings?.ProcessAsync ?? false; payload.Options = routingOptions ?? ProcessOptions.RouteExternal | ProcessOptions.RouteInternal; //Set the destination message payload.Message.ChannelId = channelId ?? ChannelId; payload.Message.MessageType = messageType; payload.Message.ActionType = actionType; payload.Message.ChannelPriority = processAsync ? 0 : 1; //Set the response path payload.Message.ResponseChannelId = ResponseId.Header.ChannelId; payload.Message.ResponseMessageType = ResponseId.Header.MessageType; payload.Message.ResponseActionType = ResponseId.Header.ActionType; payload.Message.ResponseChannelPriority = payload.Message.ChannelPriority; //Set the payload payload.Message.Holder = ServiceHandlerContext.CreateWithObject(rq); //Set the processing time payload.MaxProcessingTime = rqSettings?.WaitTime ?? fallbackMaxProcessingTime ?? Policy.OutgoingRequestMaxProcessingTimeDefault; //Transmit return(await OutgoingRequestOut(payload, processResponse ?? ProcessOutgoingResponse <RS>, processAsync)); } catch (Exception ex) { string key = payload?.Id.ToString() ?? string.Empty; Collector?.LogException(string.Format("Error transmitting {0}-{1} internally", actionType, key), ex); throw; } }