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); } } } }
public TesouroDiretoService(IOptions <ExternalServicesConfiguration> config, IMapper mapper, IMemoryCache memoryCache, IHttpClientFactory httpClientFactory) { _config = config.Value.GetConfiguration(_serviceKey); _mapper = mapper; _memoryCache = memoryCache; _httpClientFactory = httpClientFactory; }
/// <summary> /// Checks if eventSubscriberConf.Endpoint should be concatenated with a BaseURL /// and OR add the default Endpoint /// </summary> /// <param name="eventSubscriberConf"></param> /// <param name="subscriberConfig"></param> /// <returns></returns> public static string Endpoint(EventSubscriberConfiguration eventSubscriberConf, ExternalServiceConfiguration externalService) { AuthConfig authConfig = externalService.Auth; Enum.TryParse(authConfig.Type, out AuthType authType); if (eventSubscriberConf.Endpoint == null) { eventSubscriberConf.Endpoint = externalService.Config.BaseURL + EventManagerConstants.EventReceptionPath; } else { eventSubscriberConf.Endpoint = externalService.Config.BaseURL + eventSubscriberConf.Endpoint; } return(eventSubscriberConf.Endpoint); }