コード例 #1
0
        public static async Task <CapabilityDiscoveryResult> GetDiscoveryCapabilityResultAsync(string capability)
        {
            var cacheResult = await DiscoveryServiceCache.LoadAsync();

            CapabilityDiscoveryResult discoveryCapabilityResult = null;

            if (cacheResult != null && cacheResult.DiscoveryInfoForServices.ContainsKey(capability))
            {
                discoveryCapabilityResult = cacheResult.DiscoveryInfoForServices[capability];

                var initialAuthResult = await GetAccessTokenAsync(discoveryCapabilityResult.ServiceResourceId);

                if (initialAuthResult.UserInfo.UniqueId != cacheResult.UserId)
                {
                    // cache is for another user
                    cacheResult = null;
                }
            }

            if (cacheResult == null)
            {
                cacheResult = await CreateAndSaveDiscoveryServiceCacheAsync();

                discoveryCapabilityResult = cacheResult.DiscoveryInfoForServices[capability];
            }

            return(discoveryCapabilityResult);
        }
        public static async Task <DiscoveryServiceCache> CreateAndSaveAsync(string userId, IDictionary <string, CapabilityDiscoveryResult> discoveryInfoForServices)
        {
            var cache = new DiscoveryServiceCache
            {
                UserId = userId,
                DiscoveryInfoForServices = discoveryInfoForServices
            };

            StorageFolder localFolder = ApplicationData.Current.LocalFolder;

            StorageFile textFile = await localFolder.CreateFileAsync(FileName, CreationCollisionOption.ReplaceExisting);

            try
            {
                _lock.EnterWriteLock();
                using (IRandomAccessStream textStream = await textFile.OpenAsync(FileAccessMode.ReadWrite))
                {
                    using (DataWriter textWriter = new DataWriter(textStream))
                    {
                        cache.Save(textWriter);
                        await textWriter.StoreAsync();
                    }
                }
            }
            finally
            {
                _lock.ExitWriteLock();
            }

            return(cache);
        }
コード例 #3
0
        public static async Task <IDictionary <string, CapabilityDiscoveryResult> > GetAllCapabilityDiscoveryResultAsync()
        {
            DiscoveryServiceCache discoveryCache = null;

            var cacheResult = await DiscoveryServiceCache.LoadAsync();

            if (cacheResult == null)
            {
                discoveryCache = await CreateAndSaveDiscoveryServiceCacheAsync();

                return(discoveryCache.DiscoveryInfoForServices);
            }

            return(cacheResult.DiscoveryInfoForServices);
        }
        public static async Task <ServiceCapabilityCache> LoadAsync(ServiceCapabilities capability)
        {
            CapabilityDiscoveryResult capabilityDiscoveryResult = null;

            DiscoveryServiceCache cache = await LoadAsync();

            cache.DiscoveryInfoForServices.TryGetValue(capability.ToString(), out capabilityDiscoveryResult);

            if (cache == null || capabilityDiscoveryResult == null)
            {
                return(null);
            }

            return(new ServiceCapabilityCache
            {
                UserId = cache.UserId,
                CapabilityResult = capabilityDiscoveryResult
            });
        }
コード例 #5
0
        public static async Task <DiscoveryServiceCache> CreateAndSaveDiscoveryServiceCacheAsync()
        {
            DiscoveryServiceCache discoveryCache = null;

            string userId = null;

            var discoveryClient = new DiscoveryClient(DiscoveryServiceEndpointUri,
                                                      async() =>
            {
                var authResult = await GetAccessTokenAsync(DiscoveryServiceResourceId);

                userId = authResult.UserInfo.UniqueId;

                return(authResult.AccessToken);
            });

            var discoveryCapabilityResult = await discoveryClient.DiscoverCapabilitiesAsync();

            discoveryCache = await DiscoveryServiceCache.CreateAndSaveAsync(userId, discoveryCapabilityResult);

            return(discoveryCache);
        }
        private static DiscoveryServiceCache Load(DataReader textReader)
        {
            var cache = new DiscoveryServiceCache();

            cache.UserId = textReader.ReadString();
            var entryCount = textReader.ReadInt32();

            cache.DiscoveryInfoForServices = new Dictionary <string, CapabilityDiscoveryResult>(entryCount);

            for (var i = 0; i < entryCount; i++)
            {
                var key = textReader.ReadString();

                var serviceResourceId  = textReader.ReadString();
                var serviceEndpointUri = new Uri(textReader.ReadString());
                var serviceApiVersion  = textReader.ReadString();

                cache.DiscoveryInfoForServices.Add(key, new CapabilityDiscoveryResult(serviceEndpointUri, serviceResourceId, serviceApiVersion));
            }

            return(cache);
        }
コード例 #7
0
        public static async Task <CapabilityDiscoveryResult> GetCapabilityDiscoveryResultAsync(ServiceCapabilities serviceCapability)
        {
            var discoveryServiceInfo = await GetAllCapabilityDiscoveryResultAsync();

            DiscoveryServiceCache discoveryCache = null;

            var cacheResult = await DiscoveryServiceCache.LoadAsync(serviceCapability);

            if (cacheResult == null)
            {
                discoveryCache = await CreateAndSaveDiscoveryServiceCacheAsync();

                CapabilityDiscoveryResult capabilityDiscoveryResult = null;

                discoveryCache.DiscoveryInfoForServices.TryGetValue(serviceCapability.ToString(), out capabilityDiscoveryResult);

                return(capabilityDiscoveryResult);
            }
            else
            {
                return(cacheResult.CapabilityResult);
            }
        }
        private static DiscoveryServiceCache Load(DataReader textReader)
        {
            var cache = new DiscoveryServiceCache();

            cache.UserId = textReader.ReadString();
            var entryCount = textReader.ReadInt32();

            cache.DiscoveryInfoForServices = new Dictionary<string, CapabilityDiscoveryResult>(entryCount);

            for (var i = 0; i < entryCount; i++)
            {
                var key = textReader.ReadString();

                var serviceResourceId = textReader.ReadString();
                var serviceEndpointUri = new Uri(textReader.ReadString());
                var serviceApiVersion = textReader.ReadString();

                cache.DiscoveryInfoForServices.Add(key, new CapabilityDiscoveryResult(serviceEndpointUri, serviceResourceId, serviceApiVersion));
            }

            return cache;
        }
        public static async Task<DiscoveryServiceCache> CreateAndSaveAsync(string userId, IDictionary<string, CapabilityDiscoveryResult> discoveryInfoForServices)
        {
            var cache = new DiscoveryServiceCache
            {
                UserId = userId,
                DiscoveryInfoForServices = discoveryInfoForServices
            };

            StorageFolder localFolder = ApplicationData.Current.LocalFolder;

            StorageFile textFile = await localFolder.CreateFileAsync(FileName, CreationCollisionOption.ReplaceExisting);
            try
            {
                _lock.EnterWriteLock();
                using (IRandomAccessStream textStream = await textFile.OpenAsync(FileAccessMode.ReadWrite))
                {
                    using (DataWriter textWriter = new DataWriter(textStream))
                    {
                        cache.Save(textWriter);
                        await textWriter.StoreAsync();
                    }
                }
            }
            finally
            {
                _lock.ExitWriteLock();
            }

            return cache;
        }