private async ValueTask <dynamic> ProcessEventRpc(string routingKey, string eventName, ReadOnlyMemory <byte> message, CancellationToken cancel = default)
        {
            dynamic output = null;

            if (SubsManager.HasSubscriptionsForEvent(routingKey))
            {
                var subscriptions = SubsManager.GetHandlersForEvent(routingKey);

                if (!subscriptions.Any())
                {
                    Logger.LogError("ProcessEventRpc subscriptions no items! " + routingKey);
                }
                foreach (var subscription in subscriptions)
                {
                    switch (subscription.SubscriptionManagerType)
                    {
                    case SubscriptionManagerType.Rpc:
                        try
                        {
                            if (EventHandler is null)
                            {
                                Logger.LogError("ProcessEventRpc _eventHandler is null!");
                            }
                            else
                            {
                                var eventType = SubsManager.GetEventTypeByName(routingKey);
                                if (eventType is null)
                                {
                                    Logger.LogError("ProcessEventRpc: eventType is null! " + routingKey);
                                    return(null);
                                }
                                var eventReplyType = SubsManager.GetEventReplyTypeByName(routingKey);
                                if (eventReplyType is null)
                                {
                                    Logger.LogError("ProcessEventRpc: eventReplyType is null! " + routingKey);
                                    return(null);
                                }

                                using (var ms = new MemoryStream(message.ToArray()))
                                {
                                    var integrationEvent = Serializer.Deserialize(eventType, ms);
                                    var concreteType     =
                                        typeof(IIntegrationRpcHandler <,>).MakeGenericType(eventType,
                                                                                           eventReplyType);

                                    output = await((dynamic)concreteType.GetMethod("HandleRpcAsync")
                                                   .Invoke(EventHandler, new[] { integrationEvent, cancel })).ConfigureAwait(false);

                                    if (output is null)
                                    {
                                        Logger.LogError("ProcessEventRpc output is null!");
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Logger.LogError("ProcessEventRpc: " + ex.Message + " - " + ex.StackTrace);
                        }
                        break;

                    case SubscriptionManagerType.Dynamic:
                        break;

                    case SubscriptionManagerType.Typed:
                        break;

                    case SubscriptionManagerType.Queue:
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }
            }
            else
            {
                Logger.LogError("ProcessEventRpc HasSubscriptionsForEvent " + routingKey + " No Subscriptions!");
            }
            return(output);
        }//ProcessEventRpc.
Esempio n. 2
0
        private async ValueTask ProcessEventReply(string routingKey, string eventName, ReadOnlyMemory <byte> message, TaskCompletionSource <dynamic> tcs,
                                                  CancellationToken cancel = default)
        {
            if (SubsManager.HasSubscriptionsForEventReply(routingKey))
            {
                var subscriptions = SubsManager.GetHandlersForEventReply(routingKey);
                if (!subscriptions.Any())
                {
                    Logger.LogError("ProcessEventReply subscriptions no items! " + routingKey);
                }
                foreach (var subscription in subscriptions)
                {
                    switch (subscription.SubscriptionManagerType)
                    {
                    case SubscriptionManagerType.Rpc:
                        break;

                    case SubscriptionManagerType.RpcClient:
                        try
                        {
                            if (EventHandler is null)
                            {
                                Logger.LogError("ProcessEventReplyClient _eventHandler is null!");
                            }
                            else
                            {
                                //var eventType = SubsManager.GetEventTypeByName(routingKey);
                                //if (eventType is null)
                                //{
                                //    Logger.LogError("ProcessEventReplyClient: eventType is null! " + routingKey);
                                //    return;
                                //}

                                var eventResultType = SubsManager.GetEventReplyTypeByName(routingKey);
                                if (eventResultType is null)
                                {
                                    Logger.LogError("ProcessEventReplyClient: eventResultType is null! " + routingKey);
                                    return;
                                }

                                await using var ms = new MemoryStream(message.ToArray());
                                var integrationEvent = Serializer.Deserialize(eventResultType, ms);
                                tcs.TrySetResult(integrationEvent);
                                var concreteType =
                                    typeof(IIntegrationRpcClientHandler <>).MakeGenericType(
                                        eventResultType);
                                await((ValueTask)concreteType.GetMethod("HandleReply")
                                      .Invoke(EventHandler, new[] { integrationEvent, cancel })).ConfigureAwait(false);
                            }
                        }
                        catch (Exception ex)
                        {
                            Logger.LogError("ProcessEventReplyClient: " + ex.Message + " - " + ex.StackTrace);
                        }
                        break;

                    case SubscriptionManagerType.RpcServer:
                        break;

                    case SubscriptionManagerType.Dynamic:
                        break;

                    case SubscriptionManagerType.Typed:
                        break;

                    case SubscriptionManagerType.Queue:
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }
            }
            else
            {
                Logger.LogError("ProcessEventReplyClient HasSubscriptionsForEventReply " + routingKey + " No Subscriptions!");
            }
        }