示例#1
0
        /// <summary>
        /// Effectue l'injection des services demandés.
        /// </summary>
        /// <param name="factory">Factory pour la communication avec le BusinessCore.</param>
        /// <returns>Message de retour</returns>
        public async Task <HttpResponseMessageResult> InjectConfig(ServiceFacade factory)
        {
            int cpt = 1;
            HttpResponseMessageResult res = new HttpResponseMessageResult()
            {
                IsSuccess = true
            };

            foreach (Tuple <string, string, string, IEnumerable <KeyValuePair <string, string> >, KeyValuePair <string, string>, string> elt in _jsonServiceObjects)
            {
                try
                {
                    string serviceName    = elt.Item1;
                    string httpVerb       = elt.Item2;
                    string subServiceName = elt.Item3;
                    List <KeyValuePair <string, string> > qryStr       = null;
                    KeyValuePair <string, string>         pairedReturn = elt.Item5;
                    string body = elt.Item6;

                    AbstractServiceFactory service = factory[serviceName];
                    if (service == null)
                    {
                        res.IsSuccess = false;
                        res.Message  += $"Service n° {cpt} - Le nom de service [{serviceName}] n'est pas connu.";
                        return(res);
                    }
                    if (!string.IsNullOrWhiteSpace(pairedReturn.Value) && _references.ContainsKey(pairedReturn.Value))
                    {
                        res.IsSuccess = false;
                        res.Message  += $"Service n° {cpt} - Une référence ({pairedReturn.Value}) existe déjà.";
                        return(res);
                    }

                    // Transforme le body
                    body = ReplaceGoodIds(body);
                    // Transforme les querystring
                    if (elt.Item4 != null)
                    {
                        qryStr = new List <KeyValuePair <string, string> >();
                        foreach (KeyValuePair <string, string> kvp in elt.Item4)
                        {
                            qryStr.Add(new KeyValuePair <string, string>(kvp.Key, ReplaceGoodIds(kvp.Value)));
                        }
                    }

                    res.Message += $"Appel au service : [{serviceName}]/{subServiceName} ({httpVerb})"; // Globalisation
                    HttpResponseMessageResult call = new HttpResponseMessageResult()
                    {
                        IsSuccess = true
                    };
                    if (httpVerb == HTTP_VERB_POST)
                    {
                        call = await service.Post(subServiceName, qryStr, body);
                    }
                    if (httpVerb == HTTP_VERB_PUT)
                    {
                        call = await service.Put(subServiceName, qryStr, body);
                    }
                    if (httpVerb == HTTP_VERB_GET)
                    {
                        call = await service.Get(subServiceName, qryStr);
                    }

                    if ((call != null) && !call.IsSuccess)
                    {
                        res.IsSuccess = false;
                        res.Message  += $"Service n° {cpt} - Retour en erreur : {call.Message}";
                        res.Append(call);
                        return(res);
                    }

                    JToken returnObj = null;
                    if ((call != null) && !string.IsNullOrWhiteSpace(call.Json))
                    {
                        returnObj = JsonConvert.DeserializeObject(call.Json, new JsonSerializerSettings()
                        {
                            ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                        }) as JToken;
                    }
                    if (!string.IsNullOrWhiteSpace(pairedReturn.Key) && !string.IsNullOrWhiteSpace(pairedReturn.Value) && (returnObj != null) && (returnObj[pairedReturn.Key] != null))
                    {
                        long id = JsonConvert.DeserializeObject <long>(returnObj[pairedReturn.Key].ToString());
                        _references.Add(pairedReturn.Value, id);
                    }

                    res.Append(call);
                }
                catch (Exception ex)
                {
                    res.IsSuccess = false;
                    res.Message  += $"Service n° {cpt} - Exception : {ex.Message}";
                }
                cpt++;
            }
            return(res);
        }