public static async Task <IList <IServiceInstance> > GetInstancesWithCacheAsync(
            this IServiceInstanceProvider serviceInstanceProvider,
            string serviceId,
            IDistributedCache distributedCache        = null,
            DistributedCacheEntryOptions cacheOptions = null,
            string serviceInstancesKeyPrefix          = "ServiceInstances:")
        {
            // if distributed cache was provided, just make the call back to the provider
            if (distributedCache != null)
            {
                // check the cache for existing service instances
                var instanceData = await distributedCache.GetAsync(serviceInstancesKeyPrefix + serviceId).ConfigureAwait(false);

                if (instanceData != null && instanceData.Length > 0)
                {
                    return(DeserializeFromCache <List <SerializableIServiceInstance> >(instanceData).ToList <IServiceInstance>());
                }
            }

            // cache not found or instances not found, call out to the provider
            var instances = serviceInstanceProvider.GetInstances(serviceId);

            if (distributedCache != null)
            {
                await distributedCache.SetAsync(serviceInstancesKeyPrefix + serviceId, SerializeForCache(MapToSerializable(instances)), cacheOptions ?? new DistributedCacheEntryOptions()).ConfigureAwait(false);
            }

            return(instances);
        }
Example #2
0
        public async Task <WebConnectionInfo> CreateWebConnectionInfoAsync(string token, string instanceId)
        {
            IServiceInstanceProvider serviceInstanceProvider = _serviceLocator.GetInstance <IServiceInstanceProvider>();
            HealthServiceInstance    serviceInstance         = await serviceInstanceProvider.GetHealthServiceInstanceAsync(instanceId);

            IWebHealthVaultConnection connection = Ioc.Container.Locate <IWebHealthVaultConnection>(
                extraData:
                new
            {
                serviceLocator = _serviceLocator
            });

            WebHealthVaultConnection webHealthVaultConnection = connection as WebHealthVaultConnection;

            webHealthVaultConnection.ServiceInstance = serviceInstance;
            webHealthVaultConnection.UserAuthToken   = token;

            IPersonClient personClient = webHealthVaultConnection.CreatePersonClient();

            var personInfo = await personClient.GetPersonInfoAsync();

            WebConnectionInfo webConnectionInfo = new WebConnectionInfo
            {
                PersonInfo        = personInfo,
                ServiceInstanceId = instanceId,
                SessionCredential = webHealthVaultConnection.SessionCredential,
                UserAuthToken     = token
            };

            return(webConnectionInfo);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="RandomLoadBalancer"/> class.
 /// Returns random service instances, with option caching of service lookups
 /// </summary>
 /// <param name="serviceInstanceProvider">Provider of service instance information</param>
 /// <param name="distributedCache">For caching service instance data</param>
 /// <param name="cacheEntryOptions">Configuration for cache entries of service instance data</param>
 /// <param name="logger">For logging</param>
 public RandomLoadBalancer(IServiceInstanceProvider serviceInstanceProvider, IDistributedCache distributedCache = null, DistributedCacheEntryOptions cacheEntryOptions = null, ILogger logger = null)
 {
     _serviceInstanceProvider = serviceInstanceProvider ?? throw new ArgumentNullException(nameof(serviceInstanceProvider));
     _distributedCache        = distributedCache;
     _cacheOptions            = cacheEntryOptions;
     _logger = logger;
 }
 public ServiceRegistrar(IServiceInstanceProvider instanceProvider, IServiceInstanceChecker instanceChecker,
                         IRegistratedServicesFilter registratedServicesFilter)
 {
     InstanceProvider          = instanceProvider;
     InstanceChecker           = instanceChecker;
     RegistratedServicesFilter = registratedServicesFilter;
 }
Example #5
0
 public RoundRobinLoadBalancer(IServiceInstanceProvider serviceInstanceProvider, IDistributedCache distributedCache = null, ILogger logger = null)
 {
     ServiceInstanceProvider = serviceInstanceProvider ?? throw new ArgumentNullException(nameof(serviceInstanceProvider));
     _distributedCache       = distributedCache;
     _logger = logger;
     _logger?.LogDebug("Distributed cache was provided to load balancer: {DistributedCacheIsNull}", _distributedCache == null);
 }
Example #6
0
        public async Task WhenUserIdentityHasWebConnectionInfo()
        {
            // Arrange
            var webConnectionInfo = new WebConnectionInfo
            {
                ServiceInstanceId = "1",
                PersonInfo        = new PersonInfo(),
                SessionCredential = new SessionCredential(),
                UserAuthToken     = "some"
            };

            // Mock HealthVaultIdentityProvider
            IHealthVaultIdentityProvider healthVaultIdentityProvider = Substitute.For <IHealthVaultIdentityProvider>();

            healthVaultIdentityProvider.TryGetIdentity().Returns(new HealthVaultIdentity
            {
                WebConnectionInfo = webConnectionInfo
            });
            Ioc.Container.Configure(c => c.ExportInstance(healthVaultIdentityProvider).As <IHealthVaultIdentityProvider>());

            // Mock HealthVaultConnection
            WebHealthVaultConfiguration webHealthVaultConfiguration = new WebHealthVaultConfiguration();

            webHealthVaultConfiguration.DefaultHealthVaultUrl      = new Uri("http://www.bing.com");
            webHealthVaultConfiguration.DefaultHealthVaultShellUrl = new Uri("http://www.bing.com");

            IServiceLocator serviceLocator = Substitute.For <IServiceLocator>();

            serviceLocator.GetInstance <WebHealthVaultConfiguration>().Returns(webHealthVaultConfiguration);
            serviceLocator.GetInstance <IHealthWebRequestClient>().Returns(Substitute.For <IHealthWebRequestClient>());
            serviceLocator
            .GetInstance <IHealthServiceResponseParser>()
            .Returns(Substitute.For <IHealthServiceResponseParser>());

            WebHealthVaultConnection webHealthVaultConnection = Substitute.For <WebHealthVaultConnection>(serviceLocator);

            Ioc.Container.Configure(c => c.ExportInstance(webHealthVaultConnection).As <IWebHealthVaultConnection>());

            // Mock ServiceInstanceProvider
            IServiceInstanceProvider serviceInstanceProvider = Substitute.For <IServiceInstanceProvider>();

            serviceInstanceProvider
            .GetHealthServiceInstanceAsync(Arg.Any <string>())
            .Returns(Task.FromResult(new HealthServiceInstance()));
            Ioc.Container.Configure(c => c.ExportInstance(serviceInstanceProvider).As <IServiceInstanceProvider>());

            WebHealthVaultFactory factory = new WebHealthVaultFactory();

            // Act
            IWebHealthVaultConnection resultWebHealthVaultConnection = await factory.CreateWebConnectionInternalAsync();

            // Assert
            Assert.AreEqual(webConnectionInfo.UserAuthToken, resultWebHealthVaultConnection.UserAuthToken);
        }
        internal async Task ResetConnectionLeaseTimeOutAsync(WebConnectionInfo webConnectionInfo)
        {
            IServiceInstanceProvider serviceInstanceProvider = Ioc.Get <IServiceInstanceProvider>();
            HealthServiceInstance    serviceInstance         = await serviceInstanceProvider.GetHealthServiceInstanceAsync(webConnectionInfo.ServiceInstanceId);

            WebHealthVaultConfiguration webHealthVaultConfiguration = Ioc.Get <WebHealthVaultConfiguration>();
            var serviceInstanceHealthServiceUrl = serviceInstance.HealthServiceUrl;

            // Set socket to be refreshed in case the end point has been changed based on the healthvault service instance
            if (!webHealthVaultConfiguration.DefaultHealthVaultUrl.Equals(serviceInstanceHealthServiceUrl))
            {
                SetConnectionLeaseTimeOut(serviceInstanceHealthServiceUrl);
            }
        }
Example #8
0
        // Enables unit test
        internal async Task <IOfflineHealthVaultConnection> CreateOfflineConnectionInternalAsync(
            string offlinePersonId,
            string instanceId = null,
            SessionCredential sessionCredential = null)
        {
            Guid parsedOfflinePersonId;

            if (!Guid.TryParse(offlinePersonId, out parsedOfflinePersonId))
            {
                throw new ArgumentException("Unable to parse offline person id to Guid", nameof(offlinePersonId));
            }

            IServiceLocator serviceLocator = new ServiceLocator();

            HealthServiceInstance serviceInstance = null;

            if (!string.IsNullOrEmpty(instanceId))
            {
                // Get ServiceInstance
                IServiceInstanceProvider serviceInstanceProvider = serviceLocator.GetInstance <IServiceInstanceProvider>();
                serviceInstance = await serviceInstanceProvider.GetHealthServiceInstanceAsync(instanceId);
            }

            IOfflineHealthVaultConnection offlineHealthVaultConnection = Ioc.Container.Locate <IOfflineHealthVaultConnection>(
                extraData: new { serviceLocator = serviceLocator });

            OfflineHealthVaultConnection connection = offlineHealthVaultConnection as OfflineHealthVaultConnection;

            connection.SessionCredential = sessionCredential;
            connection.OfflinePersonId   = parsedOfflinePersonId;

            // By default, service instance is "US", so do not override in case the instance id is not set
            if (serviceInstance != null)
            {
                connection.ServiceInstance = serviceInstance;
            }

            return(offlineHealthVaultConnection);
        }
Example #9
0
        public async Task WhenCreateOfflineConnection_WithOfflinePersonIdAndInstanceId()
        {
            // Arrange
            WebHealthVaultConfiguration configuration = new WebHealthVaultConfiguration
            {
                DefaultHealthVaultShellUrl = new Uri("http://www.bing.com"),
                DefaultHealthVaultUrl      = new Uri("http://www.bing.com")
            };

            IServiceLocator serviceLocator = Substitute.For <IServiceLocator>();

            serviceLocator.GetInstance <WebHealthVaultConfiguration>().Returns(configuration);

            IOfflineHealthVaultConnection mokcedOfflineHealthVaultConnection = Substitute.For <OfflineHealthVaultConnection>(serviceLocator);

            Ioc.Container.Configure(c => c.ExportInstance(mokcedOfflineHealthVaultConnection).As <IOfflineHealthVaultConnection>());

            WebHealthVaultFactory factory = new WebHealthVaultFactory();
            string offlinePersonId        = Guid.NewGuid().ToString();

            IServiceInstanceProvider serviceInstanceProvider = Substitute.For <IServiceInstanceProvider>();

            serviceInstanceProvider.GetHealthServiceInstanceAsync(Arg.Any <string>())
            .Returns(new HealthServiceInstance {
                Name = "Test"
            });
            Ioc.Container.Configure(c => c.ExportInstance(serviceInstanceProvider).As <IServiceInstanceProvider>());

            string instanceId = Guid.NewGuid().ToString();

            // Act
            IOfflineHealthVaultConnection offlineHealthVaultConnection = await factory.CreateOfflineConnectionInternalAsync(
                offlinePersonId : offlinePersonId,
                instanceId : instanceId);

            // Assert
            Assert.AreEqual(offlinePersonId, offlineHealthVaultConnection.OfflinePersonId.ToString());
            Assert.AreEqual("Test", ((OfflineHealthVaultConnection)offlineHealthVaultConnection).ServiceInstance.Name);
        }
Example #10
0
        // Enables unit test
        internal async Task <IWebHealthVaultConnection> CreateWebConnectionInternalAsync()
        {
            IHealthVaultIdentityProvider healthVaultIdentityProvider = Ioc.Container.Locate <IHealthVaultIdentityProvider>();
            HealthVaultIdentity          identity = healthVaultIdentityProvider.TryGetIdentity();

            IServiceLocator serviceLocator = new ServiceLocator();

            if (identity == null)
            {
                IWebHealthVaultConnection anonymousWebConnection = serviceLocator.GetInstance <IWebHealthVaultConnection>();
                return(anonymousWebConnection);
            }

            var webConnectionInfo = identity.WebConnectionInfo;

            if (webConnectionInfo == null)
            {
                throw new NotSupportedException("WebConnectionInfo is expected for authenticated connections");
            }

            // Get ServiceInstance
            IServiceInstanceProvider serviceInstanceProvider = Ioc.Container.Locate <IServiceInstanceProvider>();
            HealthServiceInstance    serviceInstance         = await serviceInstanceProvider.GetHealthServiceInstanceAsync(webConnectionInfo.ServiceInstanceId);

            // Get AuthInformation
            SessionCredential sessionCredentialToken = webConnectionInfo.SessionCredential;
            string            token = webConnectionInfo.UserAuthToken;

            IWebHealthVaultConnection webConnection = Ioc.Container.Locate <IWebHealthVaultConnection>(extraData: new { serviceLocator = serviceLocator });

            WebHealthVaultConnection connection = webConnection as WebHealthVaultConnection;

            connection.UserAuthToken     = token;
            connection.ServiceInstance   = serviceInstance;
            connection.SessionCredential = sessionCredentialToken;

            return(webConnection);
        }