private static async Task AddNotificationReceivers( Application application, IServiceContainer serviceContainer, IMessagingHubClient client, ITypeResolver typeResolver, IStateManager stateManager, ISessionManager sessionManager) { if (application.NotificationReceivers != null && application.NotificationReceivers.Length > 0) { foreach (var applicationReceiver in application.NotificationReceivers) { INotificationReceiver receiver; if (applicationReceiver.Response?.MediaType != null) { var content = applicationReceiver.Response.ToDocument(); receiver = new LambdaNotificationReceiver( (notification, c) => client.SendMessageAsync(content, notification.From, c)); } else if (!string.IsNullOrWhiteSpace(applicationReceiver.ForwardTo)) { var tunnelExtension = serviceContainer.GetService <ITunnelExtension>(); var destination = Identity.Parse(applicationReceiver.ForwardTo); receiver = new LambdaNotificationReceiver( (notification, c) => tunnelExtension.ForwardNotificationAsync(notification, destination, c)); } else { receiver = await CreateAsync <INotificationReceiver>( applicationReceiver.Type, serviceContainer, applicationReceiver.Settings, typeResolver) .ConfigureAwait(false); } if (applicationReceiver.OutState != null) { receiver = new SetStateNotificationReceiver(receiver, stateManager, applicationReceiver.OutState); } Func <Notification, Task <bool> > notificationPredicate = BuildPredicate <Notification>(applicationReceiver, stateManager, sessionManager); if (applicationReceiver.EventType != null) { var currentNotificationPredicate = notificationPredicate; notificationPredicate = async(n) => await currentNotificationPredicate(n) && n.Event.Equals(applicationReceiver.EventType); } client.AddNotificationReceiver(receiver, notificationPredicate, applicationReceiver.Priority); } } }
private static async Task AddCommandReceivers( Application application, IServiceContainer serviceContainer, IMessagingHubClient client, ITypeResolver typeResolver) { if (application.CommandReceivers == null || application.CommandReceivers.Length == 0) { return; } foreach (var commandReceiver in application.CommandReceivers) { var receiver = await CreateAsync <ICommandReceiver>( commandReceiver.Type, serviceContainer, commandReceiver.Settings, typeResolver) .ConfigureAwait(false); Func <Command, Task <bool> > predicate = c => Task.FromResult(true); if (commandReceiver.Method.HasValue) { var currentPredicate = predicate; predicate = async(c) => await currentPredicate(c) && c.Method == commandReceiver.Method.Value; } if (commandReceiver.Uri != null) { var limeUriRegex = new Regex(commandReceiver.Uri, RegexOptions.Compiled | RegexOptions.IgnoreCase); var currentPredicate = predicate; predicate = async(c) => await currentPredicate(c) && limeUriRegex.IsMatch(c.Uri.ToString()); } if (commandReceiver.ResourceUri != null) { var resourceUriRegex = new Regex(commandReceiver.ResourceUri, RegexOptions.Compiled | RegexOptions.IgnoreCase); var currentPredicate = predicate; predicate = async(c) => await currentPredicate(c) && resourceUriRegex.IsMatch(c.GetResourceUri().ToString()); } if (commandReceiver.MediaType != null) { var currentPredicate = predicate; var mediaTypeRegex = new Regex(commandReceiver.MediaType, RegexOptions.Compiled | RegexOptions.IgnoreCase); predicate = async(c) => await currentPredicate(c) && mediaTypeRegex.IsMatch(c.Type.ToString()); } client.AddCommandReceiver(receiver, predicate, commandReceiver.Priority); } }
public static async Task AddMessageReceivers( Application application, IServiceContainer serviceContainer, IMessagingHubClient client, ITypeResolver typeResolver, IStateManager stateManager, ISessionManager sessionManager) { if (application.MessageReceivers != null && application.MessageReceivers.Length > 0) { foreach (var applicationReceiver in application.MessageReceivers) { IMessageReceiver receiver; if (applicationReceiver.Response?.MediaType != null) { var content = applicationReceiver.Response.ToDocument(); receiver = new LambdaMessageReceiver( (message, c) => client.SendMessageAsync(content, message.From, c)); } else if (!string.IsNullOrWhiteSpace(applicationReceiver.ForwardTo)) { var tunnelExtension = serviceContainer.GetService <ITunnelExtension>(); var destination = Identity.Parse(applicationReceiver.ForwardTo); receiver = new LambdaMessageReceiver( (message, c) => tunnelExtension.ForwardMessageAsync(message, destination, c)); } else { var receiverType = typeResolver.Resolve(applicationReceiver.Type); receiver = await BuildByLifetimeAsync( applicationReceiver.Lifetime ?? application.DefaultMessageReceiverLifetime, receiverType, applicationReceiver.Settings, serviceContainer); } if (applicationReceiver.OutState != null) { receiver = new SetStateMessageReceiver(receiver, stateManager, applicationReceiver.OutState); } Func <Message, Task <bool> > messagePredicate = BuildPredicate <Message>(applicationReceiver, stateManager, sessionManager); if (applicationReceiver.MediaType != null) { var currentMessagePredicate = messagePredicate; var mediaTypeRegex = new Regex(applicationReceiver.MediaType, RegexOptions.Compiled | RegexOptions.IgnoreCase); messagePredicate = async(m) => await currentMessagePredicate(m) && mediaTypeRegex.IsMatch(m.Type.ToString()); } if (applicationReceiver.Content != null) { var currentMessagePredicate = messagePredicate; var contentRegex = new Regex(applicationReceiver.Content, RegexOptions.Compiled | RegexOptions.IgnoreCase); messagePredicate = async(m) => await currentMessagePredicate(m) && contentRegex.IsMatch(m.Content.ToString()); } client.AddMessageReceiver(receiver, messagePredicate, applicationReceiver.Priority); } } }