Example #1
0
        public async Task <NullableValue> RegisterServiceInstanceAsync(ServiceInstanceRequest serviceInstanceRequest,
                                                                       CancellationToken cancellationToken = default(CancellationToken))
        {
            if (!_connectionManager.Ready)
            {
                return(NullableValue.Null);
            }

            var connection = _connectionManager.GetConnection();

            return(await new Call(_logger, _connectionManager).Execute(async() =>
            {
                var client = new Register.RegisterClient(connection);
                var instance = new ServiceInstance
                {
                    ServiceId = serviceInstanceRequest.ServiceId,
                    InstanceUUID = serviceInstanceRequest.InstanceUUID,
                    Time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
                };

                instance.Properties.Add(new KeyStringValuePair
                {
                    Key = OS_NAME, Value = serviceInstanceRequest.Properties.OsName
                });
                instance.Properties.Add(new KeyStringValuePair
                {
                    Key = HOST_NAME, Value = serviceInstanceRequest.Properties.HostName
                });
                instance.Properties.Add(new KeyStringValuePair
                {
                    Key = PROCESS_NO, Value = serviceInstanceRequest.Properties.ProcessNo.ToString()
                });
                instance.Properties.Add(new KeyStringValuePair
                {
                    Key = LANGUAGE, Value = serviceInstanceRequest.Properties.Language
                });
                foreach (var ip in serviceInstanceRequest.Properties.IpAddress)
                {
                    instance.Properties.Add(new KeyStringValuePair
                    {
                        Key = IPV4, Value = ip
                    });
                }

                var serviceInstances = new ServiceInstances();
                serviceInstances.Instances.Add(instance);
                var mapping = await client.doServiceInstanceRegisterAsync(serviceInstances,
                                                                          null, _config.GetTimeout(), cancellationToken);
                foreach (var serviceInstance in mapping.ServiceInstances)
                {
                    if (serviceInstance.Key == serviceInstanceRequest.InstanceUUID)
                    {
                        return new NullableValue(serviceInstance.Value);
                    }
                }
                return NullableValue.Null;
            },
                                                                       () => NullableValue.Null,
                                                                       () => ExceptionHelpers.RegisterServiceInstanceError));
        }
Example #2
0
        private int instance(Channel connection, string instanceUUID, int serviceId)
        {
            var serviceInstanceId = 0;

            var properties = new AgentOsInfoRequest
            {
                HostName  = DnsHelpers.GetHostName(),
                IpAddress = DnsHelpers.GetIpV4s(),
                OsName    = PlatformInformation.GetOSName(),
                ProcessNo = Process.GetCurrentProcess().Id,
                Language  = "dotnet"
            };
            var request = new ServiceInstanceRequest
            {
                ServiceId    = serviceId,
                InstanceUUID = instanceUUID,
                Properties   = properties
            };

            var client   = new Register.RegisterClient(connection);
            var instance = new ServiceInstance
            {
                ServiceId    = request.ServiceId,
                InstanceUUID = request.InstanceUUID,
                Time         = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            };

            instance.Properties.Add(new KeyStringValuePair
            {
                Key = OS_NAME, Value = request.Properties.OsName
            });
            instance.Properties.Add(new KeyStringValuePair
            {
                Key = HOST_NAME, Value = request.Properties.HostName
            });
            instance.Properties.Add(new KeyStringValuePair
            {
                Key = PROCESS_NO, Value = request.Properties.ProcessNo.ToString()
            });
            instance.Properties.Add(new KeyStringValuePair
            {
                Key = LANGUAGE, Value = request.Properties.Language
            });
            foreach (var ip in request.Properties.IpAddress)
            {
                instance.Properties.Add(new KeyStringValuePair {
                    Key = IPV4, Value = ip
                });
            }

            var serviceInstances = new ServiceInstances();

            serviceInstances.Instances.Add(instance);

            try
            {
                var value = Task.Run(async() =>
                {
                    return(await Polly.Polling(3,
                                               () => client.doServiceInstanceRegisterAsync(serviceInstances)));
                }).Result;

                //var reply = Task.Run(async () =>
                //{
                //    return await client.doServiceInstanceRegisterAsync(serviceInstances);
                //}).Result;
                //foreach (var serviceInstance in reply.ServiceInstances)
                //{
                //    serviceInstanceId = serviceInstance.Value;
                //}

                serviceInstanceId = value;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(serviceInstanceId);
        }