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; }
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; }