public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            List <JsonInstanceInfo> result = null;

            try
            {
                if (reader.TokenType == JsonToken.StartArray)
                {
                    result = (List <JsonInstanceInfo>)serializer.Deserialize(reader, typeof(List <JsonInstanceInfo>));
                }
                else
                {
                    JsonInstanceInfo singleInst = (JsonInstanceInfo)serializer.Deserialize(reader, typeof(JsonInstanceInfo));
                    if (singleInst != null)
                    {
                        result = new List <JsonInstanceInfo>
                        {
                            singleInst
                        };
                    }
                }
            }
            catch (Exception)
            {
                result = new List <JsonInstanceInfo>();
            }

            if (result == null)
            {
                result = new List <JsonInstanceInfo>();
            }

            return(result);
        }
 public JsonInstanceInfoRoot(JsonInstanceInfo info)
 {
     Instance = info;
 }
Example #3
0
        public virtual async Task <EurekaHttpResponse <InstanceInfo> > SendHeartBeatAsync(string appName, string id, InstanceInfo info, InstanceStatus overriddenStatus)
        {
            if (info == null)
            {
                throw new ArgumentNullException(nameof(info));
            }

            if (string.IsNullOrEmpty(appName))
            {
                throw new ArgumentException(nameof(appName));
            }

            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentException(nameof(id));
            }

            var queryArgs = new Dictionary <string, string>()
            {
                { "status", info.Status.ToString() },
                { "lastDirtyTimestamp", DateTimeConversions.ToJavaMillis(new DateTime(info.LastDirtyTimestamp, DateTimeKind.Utc)).ToString() }
            };

            if (overriddenStatus != InstanceStatus.UNKNOWN)
            {
                queryArgs.Add("overriddenstatus", overriddenStatus.ToString());
            }

            HttpClient client     = GetHttpClient(_config);
            var        requestUri = GetRequestUri(_serviceUrl + "apps/" + info.AppName + "/" + id, queryArgs);
            var        request    = GetRequestMessage(HttpMethod.Put, requestUri);

#if NET451
            // If certificate validation is disabled, inject a callback to handle properly
            RemoteCertificateValidationCallback prevValidator = null;
            if (!_config.ValidateCertificates)
            {
                prevValidator = ServicePointManager.ServerCertificateValidationCallback;
                ServicePointManager.ServerCertificateValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
            }
#endif
            try
            {
                using (HttpResponseMessage response = await client.SendAsync(request))
                {
                    Stream stream = await response.Content.ReadAsStreamAsync();

                    JsonInstanceInfo jinfo = JsonInstanceInfo.Deserialize(stream);



                    InstanceInfo infoResp = null;
                    if (jinfo != null)
                    {
                        infoResp = InstanceInfo.FromJsonInstance(jinfo);
                    }

                    _logger?.LogDebug("SendHeartbeatAsync {0}, status: {1}, instanceInfo: {2}",
                                      requestUri.ToString(), response.StatusCode, ((infoResp != null) ? infoResp.ToString() : "null"));
                    EurekaHttpResponse <InstanceInfo> resp = new EurekaHttpResponse <InstanceInfo>(response.StatusCode, infoResp);
                    resp.Headers = response.Headers;
                    return(resp);
                }
            }
            catch (Exception e)
            {
                _logger?.LogError("SendHeartbeatAsync Exception: {0}", e);
                throw;
            }
#if NET451
            finally
            {
                ServicePointManager.ServerCertificateValidationCallback = prevValidator;
            }
#endif
        }