/// <summary> /// Send a request sync /// </summary> /// <typeparam name="T"></typeparam> /// <param name="event"></param> /// <param name="triggerOption"></param> /// <returns></returns> public T Send <T>(IRequestTrigger @event, TriggerOption triggerOption) { var asyncTask = SendAsync <T>(@event, triggerOption); asyncTask.ConfigureAwait(false); return(asyncTask.Result); }
private async Task <T> ProcessRequest <T>(string eventName, string message, TriggerOption triggerOption) { var handlerFactories = _requestHandlerManager.GetHandlerFactoriesForRequest(eventName); var requestContext = new RequestHandlerContext(); if (handlerFactories.Count == 0) { return(default(T)); } foreach (var aHandlerFactory in handlerFactories) { if (requestContext.IsEnd) { break; } SetTheIocResolverIfIocHandlerFactory(aHandlerFactory); try { var aHandler = aHandlerFactory.GetHandler(); if (aHandler is IDynamicRequestHandler) { dynamic eventData = JObject.Parse(message); await((IDynamicRequestHandler)aHandler).HandleAsync(eventData, requestContext); } else { var requestParamType = aHandlerFactory.GetLocalEventType(); if (requestParamType != null) { var integrationEvent = JsonConvert.DeserializeObject(message, requestParamType); var concreteType = typeof(IRequestHandler <>).MakeGenericType(requestParamType); await(Task) concreteType.GetMethod("HandleAsync").Invoke(aHandler, new object[] { integrationEvent, requestContext }); } } aHandlerFactory.ReleaseHandler(aHandler); } catch (Exception ex) { LogMessage(LogLevel.ERROR, "There has raised a error when send request.", ex); if (!triggerOption.IsIgnoreException) { throw ex; } } } return(JsonConvert.DeserializeObject <T>(JsonConvert.SerializeObject(requestContext.Response))); }
private async Task ProcessEvent(string eventName, string message, TriggerOption triggerOption) { var handlerFactories = _subcriptionManager.GetHandlerFactoriesForEvent(eventName); foreach (var aHandlerFactory in handlerFactories) { try { SetTheIocResolverIfIocHandlerFactory(aHandlerFactory); var aHandler = aHandlerFactory.GetHandler(); if (aHandler is IDynamicEventHandler) { dynamic eventData = JObject.Parse(message); await((IDynamicEventHandler)aHandler).HandleAsync(eventData); } else { var subscriptionParamType = aHandlerFactory.GetLocalEventType(); if (subscriptionParamType != null) { var integrationEvent = JsonConvert.DeserializeObject(message, subscriptionParamType); var concreteType = typeof(IEventHandler <>).MakeGenericType(subscriptionParamType); await(Task) concreteType.GetMethod("HandleAsync").Invoke(aHandler, new object[] { integrationEvent }); } } aHandlerFactory.ReleaseHandler(aHandler); } catch (Exception ex) { LogMessage(LogLevel.ERROR, "There has caught a error when publishing event.", ex); if (!triggerOption.IsIgnoreException) { throw ex; } } } }
/// <summary> /// Send a request async /// </summary> /// <typeparam name="T"></typeparam> /// <param name="event"></param> /// <param name="triggerOption"></param> /// <returns></returns> public Task <T> SendAsync <T>(IRequestTrigger @event, TriggerOption triggerOption) { return(ProcessRequest <T>(_requestHandlerManager.GetRequestName(@event.GetType()), JsonConvert.SerializeObject(@event), triggerOption)); }
/// <summary> /// Publish a event /// </summary> /// <param name="event"></param> /// <param name="triggerOption"></param> /// <returns></returns> public Task Publish(IEventTrigger @event, TriggerOption triggerOption) { return(ProcessEvent(_subcriptionManager.GetEventName(@event.GetType()), JsonConvert.SerializeObject(@event), triggerOption)); }