예제 #1
0
        /// <summary>
        /// Invoke a synchronous service with the specified payload. The method call block until the method return.
        /// </summary>
        /// <typeparam name="TI">The payload type.</typeparam>
        /// <typeparam name="TO">The return type.</typeparam>
        /// <param name="serviceName">The name of the service, as configured in Argilla.Node.ServiceName.</param>
        /// <param name="payload">The payload used for invoke the service.</param>
        /// <returns></returns>
        public static TO Invoke <TI, TO>(string serviceName, TI payload)
        {
            TO outputValue = default;

            PayloadSync payloadSync = new PayloadSync()
            {
                Payload = payload
            };

            string json = CustomJsonSerializer.Serialize(payloadSync);

            Exception lastException = null;

            try
            {
                Endpoint endpoint = GetFreeEndpoint(serviceName);

                string jsonResult = HttpHelper.Post(endpoint.EndpointSync, json);

                outputValue = CustomJsonSerializer.Deserialize <TO>(jsonResult);

                lastException = null;
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);

                ResolveResponse resolveResponse = Resolve(serviceName);

                foreach (Endpoint endpoint in resolveResponse.Endpoints)
                {
                    try
                    {
                        string jsonResult = HttpHelper.Post(endpoint.EndpointSync, json);

                        outputValue = CustomJsonSerializer.Deserialize <TO>(jsonResult);

                        lastException = null;

                        break;
                    }
                    catch (Exception e)
                    {
                        lastException = e;
                    }
                }
            }

            if (lastException != null)
            {
                logger.Error(lastException, lastException.Message);

                throw lastException;
            }

            logger.Debug(String.Format("Output value: {0}", outputValue));

            return(outputValue);
        }
예제 #2
0
        public ActionResult Callback([FromBody] PayloadSync payload)
        {
            string content = CustomJsonSerializer.Serialize(payload.Payload);

            MethodInfo methodInfo = ArgillaSettings.Current.MessageReceivedHandler;

            ParameterInfo[] pis        = methodInfo.GetParameters();
            ParameterInfo   argumentPI = pis[0];
            Type            t          = pis[0].ParameterType;

            object o = CustomJsonSerializer.Deserialize(content, t);
            object a = ArgillaSettings.Current.MessageReceivedHandler.Invoke(null, new[] { o });

            string result = CustomJsonSerializer.Serialize(a);

            return(new ContentResult()
            {
                Content = result, ContentType = "application/json", StatusCode = 200
            });
        }