Exemplo n.º 1
0
        /// <summary>
        /// Gets the device unique ID, or uses the fallback if none is available due to application configuration.
        /// </summary>
        /// <returns>
        /// The discovered device identifier.
        /// </returns>
        public virtual string GetDeviceUniqueId()
        {
            if (this.deviceId != null)
            {
                return(this.deviceId);
            }

            object uniqueId;

            if (DeviceExtendedProperties.TryGetValue("DeviceUniqueId", out uniqueId) == true)
            {
                byte[] result = (byte[])uniqueId;
                using (SHA256 hasher = new SHA256Managed())
                {
                    return(this.deviceId = Convert.ToBase64String(hasher.ComputeHash(result)));
                }
            }

            FallbackDeviceContext fallbackContext = this.FallbackContext;

            return(this.deviceId = fallbackContext.DeviceUniqueId);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Reads the serialized context from persistent storage, or will create a new context if none exits.
        /// </summary>
        /// <returns>The fallback context we will be using.</returns>
        private FallbackDeviceContext ReadSerializedContext()
        {
            // if we already have a context, just return that
            if (this.cachedContext != null)
            {
                return(this.cachedContext);
            }

            // if we don't aquire the sync root and check again, in case we were waiting before
            lock (this.syncRoot)
            {
                if (this.cachedContext != null)
                {
                    return(this.cachedContext);
                }

                FallbackDeviceContext temp = ReadSerializedContext(DeviceService.ContextPersistentStorageFileName);
                Thread.MemoryBarrier();
                this.cachedContext = temp;
            }

            return(this.cachedContext);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Reads the serialized context from persistent storage, or will create a new context if none exits.
        /// </summary>
        /// <param name="fileName">The file to read from storage.</param>
        /// <returns>The fallback context we will be using.</returns>
        private static FallbackDeviceContext ReadSerializedContext(string fileName)
        {
            // get a reference to the persitent store
            using (IsolatedStorageFile persistentStore = IsolatedStorageFile.GetUserStoreForApplication())
            {
                // if the file exits, attempt to read/deserialize it. If we fail, we'll just regen it.
                bool regenerateContext = true;
                if (persistentStore.FileExists(fileName) == true)
                {
                    try
                    {
                        using (IsolatedStorageFileStream stream = persistentStore.OpenFile(fileName, FileMode.Open, FileAccess.Read))
                        {
                            XDocument document = XDocument.Load(stream);
                            FallbackDeviceContext temp = new FallbackDeviceContext();
                            if (temp.Deserialize(document.Root) == true)
                            {
                                regenerateContext = false;
                                return temp;
                            }
                        }
                    }
                    catch (IsolatedStorageException)
                    {
                        // TODO: swallow?
                    }
                    catch (FileNotFoundException)
                    {
                        // TODO: swallow?
                    }
                    catch (XmlException)
                    {
                        // TODO: swallow?
                    }
                }

                // if we're here we will need to regen our context
                if (regenerateContext == true)
                {
                    // create the XML document first
                    XDocument document = new XDocument();
                    document.Add(new XElement(XName.Get(typeof(FallbackDeviceContext).Name)));

                    // initialize the new set of settings and serialize to the document root
                    FallbackDeviceContext temp = new FallbackDeviceContext();
                    temp.Initialize();
                    temp.Serialize(document.Root);

                    // write to persistent storage
                    try
                    {
                        using (IsolatedStorageFileStream stream = persistentStore.OpenFile(fileName, FileMode.OpenOrCreate, FileAccess.Write))
                        {
                            stream.SetLength(0);
                            document.Save(stream);
                            stream.Flush(true);
                            return temp;
                        }
                    }
                    catch (IsolatedStorageException)
                    {
                        // TODO: swallow?
                    }
                    catch (FileNotFoundException)
                    {
                        // TODO: swallow?
                    }
                }
            }

            FallbackDeviceContext defaultReturn = new FallbackDeviceContext();
            defaultReturn.Initialize();
            return defaultReturn;
        }
Exemplo n.º 4
0
        /// <summary>
        /// Reads the serialized context from persistent storage, or will create a new context if none exits.
        /// </summary>
        /// <returns>The fallback context we will be using.</returns>
        private FallbackDeviceContext ReadSerializedContext()
        {
            // if we already have a context, just return that
            if (this.cachedContext != null)
            {
                return this.cachedContext;
            }

            // if we don't aquire the sync root and check again, in case we were waiting before
            lock (this.syncRoot)
            {
                if (this.cachedContext != null)
                {
                    return this.cachedContext;
                }

                FallbackDeviceContext temp = ReadSerializedContext(DeviceService.ContextPersistentStorageFileName);
                Thread.MemoryBarrier();
                this.cachedContext = temp;
            }

            return this.cachedContext;
        }
Exemplo n.º 5
0
        /// <summary>
        /// Reads the serialized context from persistent storage, or will create a new context if none exits.
        /// </summary>
        /// <param name="fileName">The file to read from storage.</param>
        /// <returns>The fallback context we will be using.</returns>
        private static FallbackDeviceContext ReadSerializedContext(string fileName)
        {
            // get a reference to the persitent store
            using (IsolatedStorageFile persistentStore = IsolatedStorageFile.GetUserStoreForApplication())
            {
                // if the file exits, attempt to read/deserialize it. If we fail, we'll just regen it.
                bool regenerateContext = true;
                if (persistentStore.FileExists(fileName) == true)
                {
                    try
                    {
                        using (IsolatedStorageFileStream stream = persistentStore.OpenFile(fileName, FileMode.Open, FileAccess.Read))
                        {
                            XDocument             document = XDocument.Load(stream);
                            FallbackDeviceContext temp     = new FallbackDeviceContext();
                            if (temp.Deserialize(document.Root) == true)
                            {
                                regenerateContext = false;
                                return(temp);
                            }
                        }
                    }
                    catch (IsolatedStorageException)
                    {
                        // TODO: swallow?
                    }
                    catch (FileNotFoundException)
                    {
                        // TODO: swallow?
                    }
                    catch (XmlException)
                    {
                        // TODO: swallow?
                    }
                }

                // if we're here we will need to regen our context
                if (regenerateContext == true)
                {
                    // create the XML document first
                    XDocument document = new XDocument();
                    document.Add(new XElement(XName.Get(typeof(FallbackDeviceContext).Name)));

                    // initialize the new set of settings and serialize to the document root
                    FallbackDeviceContext temp = new FallbackDeviceContext();
                    temp.Initialize();
                    temp.Serialize(document.Root);

                    // write to persistent storage
                    try
                    {
                        using (IsolatedStorageFileStream stream = persistentStore.OpenFile(fileName, FileMode.OpenOrCreate, FileAccess.Write))
                        {
                            stream.SetLength(0);
                            document.Save(stream);
                            stream.Flush(true);
                            return(temp);
                        }
                    }
                    catch (IsolatedStorageException)
                    {
                        // TODO: swallow?
                    }
                    catch (FileNotFoundException)
                    {
                        // TODO: swallow?
                    }
                }
            }

            FallbackDeviceContext defaultReturn = new FallbackDeviceContext();

            defaultReturn.Initialize();
            return(defaultReturn);
        }