Пример #1
0
        public EventManagerMiddleware(
            RequestDelegate next,
            IOptions <EventManagerConfiguration> config
            )
        {
            _next   = next;
            _config = config.Value;
            EventManagerConstants.EventReceptionPath = !string.IsNullOrEmpty(_config.EventReceptionPath) ? _config.EventReceptionPath : EventManagerConstants.EventReceptionPath;
            EventManagerConstants.ReplyEventPrefix   = !string.IsNullOrEmpty(_config.ReplyEventPrefix) ? _config.ReplyEventPrefix : EventManagerConstants.ReplyEventPrefix;
            EventDispatcher = EventDispatcher.Instance;

            foreach (SubscriptionConfiguration subscriptionConf in _config.Subscriptions)
            {
                foreach (EventSubscriberConfiguration eventSubscriberConf in subscriptionConf.Subscribers)
                {
                    ExternalServiceConfiguration externalService = _config.ExternalServices.Find(x => x.Name == eventSubscriberConf.Name);

                    if (externalService == null)
                    {
                        continue;
                    }

                    IAuthHandler auth = AuthFactory.Create(externalService.Auth);

                    if (!auth.Valid(externalService.Config, eventSubscriberConf))
                    {
                        throw new ArgumentException($"EventManagerMiddleware ERROR: externalService is not Valid for the externalService.Auth.Type `{externalService.Auth.Type}` and name `{externalService.Name}`, so it wont be registered with EventDispatcher.Register");
                    }
                    else
                    {
                        List <Func <Event, HttpResponseMessage> > callbacks = new List <Func <Event, HttpResponseMessage> >();

                        Subscriber subscriber = new Subscriber(eventSubscriberConf.Name)
                        {
                            Config = new SubscriberConfig
                            {
                                MaxTries    = externalService.Config.MaxRetries,
                                RequestRate = externalService.Config.RequestRate
                            }
                        };

                        eventSubscriberConf.Endpoint = AuthFactory.Endpoint(eventSubscriberConf, externalService);

                        Subscription subscription = new Subscription()
                        {
                            Subscriber  = subscriber,
                            EventName   = subscriptionConf.EventName,
                            Method      = new HttpMethod(eventSubscriberConf.Method),
                            EndPoint    = eventSubscriberConf.Endpoint,
                            CallBacks   = callbacks,
                            IsExternal  = true,
                            Auth        = auth,
                            Synchronous = eventSubscriberConf.Synchronous
                        };

                        EventDispatcher.Register(subscription);
                    }
                }
            }
        }