/// <summary> /// Dispatch a message through the pipeline. If the message will be consumed, the accept function /// is called to allow the endpoint to acknowledge the message reception if applicable /// </summary> /// <param name="pipeline">The pipeline instance</param> /// <param name="message">The message to dispatch</param> /// <param name="acknowledge">The function to call if the message will be consumed by the pipeline</param> public static bool Dispatch <T>(this IInboundMessagePipeline pipeline, T message, Func <T, bool> acknowledge) where T : class { bool consumed = false; using (var bodyStream = new MemoryStream()) { ReceiveContext receiveContext = ReceiveContext.FromBodyStream(bodyStream); pipeline.Configure(x => receiveContext.SetBus(x.Bus)); var context = new ConsumeContext <T>(receiveContext, message); using (context.CreateScope()) { foreach (var consumer in pipeline.Enumerate(context)) { if (!acknowledge(message)) { return(false); } acknowledge = x => true; consumed = true; consumer(context); } } } return(consumed); }
/// <summary> /// Subscribe a component to the pipeline that handles every message /// </summary> /// <typeparam name="TComponent"></typeparam> /// <param name="pipeline">The pipeline to configure</param> /// <param name="instance">The instance that will handle the messages</param> /// <returns></returns> public static UnsubscribeAction ConnectInstance <TComponent>(this IInboundMessagePipeline pipeline, TComponent instance) where TComponent : class { return(pipeline.Configure(x => { IInstanceConnector connector = InstanceConnectorCache.GetInstanceConnector <TComponent>(); return connector.Connect(x, instance); })); }
/// <summary> /// Subscribe a component type to the pipeline that is resolved from the container for each message /// </summary> /// <typeparam name="TComponent"></typeparam> /// <param name="pipeline">The pipeline to configure</param> /// <returns></returns> public static UnsubscribeAction ConnectConsumer <TComponent>(this IInboundMessagePipeline pipeline) where TComponent : class, new() { return(pipeline.Configure(x => { var consumerFactory = new DelegateConsumerFactory <TComponent>(() => new TComponent()); IConsumerConnector connector = ConsumerConnectorCache.GetConsumerConnector <TComponent>(); return connector.Connect(x, consumerFactory); })); }
public static UnsubscribeAction ConnectHandler <TMessage>(this IInboundMessagePipeline pipeline, Action <TMessage> handler, Predicate <TMessage> condition) where TMessage : class { return(pipeline.Configure(x => { var connector = new HandlerSubscriptionConnector <TMessage>(); return connector.Connect(x, HandlerSelector.ForSelectiveHandler(condition, handler)); })); }
/// <summary> /// Subscribe a component type to the pipeline that is resolved from the container for each message /// </summary> /// <typeparam name="TConsumer"></typeparam> /// <param name="pipeline">The pipeline to configure</param> /// <param name="consumerFactory"></param> /// <returns></returns> public static UnsubscribeAction ConnectConsumer <TConsumer>(this IInboundMessagePipeline pipeline, Func <TConsumer> consumerFactory) where TConsumer : class { return(pipeline.Configure(x => { var factory = new DelegateConsumerFactory <TConsumer>(consumerFactory); IConsumerConnector connector = ConsumerConnectorCache.GetConsumerConnector <TConsumer>(); return connector.Connect(x, factory); })); }