Example #1
0
        /// <summary>
        /// Get the first RequestRecord in the queue
        /// </summary>
        public static RequestRecord GetRequestRecord(string queueName)
        {
            List <RequestRecord>       requests = new List <RequestRecord>();
            DataContractJsonSerializer dc       = new DataContractJsonSerializer(requests.GetType());

            using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
            {
                lock (fileLocks[queueName])
                {
                    // try block because the using block below will throw if the file doesn't exist
                    try
                    {
                        // if the file opens, read the contents
                        using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(QueueFilename(queueName), FileMode.Open, file))
                        {
                            try
                            {
                                // if the file opens, read the contents
                                requests = dc.ReadObject(stream) as List <RequestRecord>;
                                if (requests.Count > 0)
                                {
                                    RequestRecord record = requests[0];
                                    record.DeserializeBody();
                                    return(record);
                                }
                                else
                                {
                                    return(null);
                                }
                            }
                            catch (Exception ex)
                            {
                                stream.Position = 0;
                                string s = new StreamReader(stream).ReadToEnd();
                                TraceHelper.AddMessage(String.Format("Exception in deserializing RequestRecord: {0}; record: {1}", ex.Message, s));
                                return(null);
                            }
                        }
                    }
                    catch (Exception)
                    {
                        // could not open the isolated storage file
                        return(null);
                    }
                }
            }
        }
Example #2
0
        /// <summary>
        /// Dequeue the first record
        /// </summary>
        public static RequestRecord DequeueRequestRecord(string queueName)
        {
            List <RequestRecord>       requests = new List <RequestRecord>();
            DataContractJsonSerializer dc       = new DataContractJsonSerializer(requests.GetType());

            using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
            {
                lock (fileLocks[queueName])
                {
                    using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(QueueFilename(queueName), FileMode.Open, file))
                    {
                        try
                        {
                            // if the file opens, read the contents
                            requests = dc.ReadObject(stream) as List <RequestRecord>;
                            if (requests.Count > 0)
                            {
                                RequestRecord record = requests[0];
                                requests.Remove(record);  // remove the first entry
                                stream.SetLength(0);
                                stream.Position = 0;
                                dc.WriteObject(stream, requests);

                                record.DeserializeBody();
                                return(record);
                            }
                            else
                            {
                                return(null);
                            }
                        }
                        catch (Exception ex)
                        {
                            stream.Position = 0;
                            string s = new StreamReader(stream).ReadToEnd();
                            TraceHelper.AddMessage(String.Format("Exception in deserializing RequestRecord: {0}; record: {1}", ex.Message, s));
                            return(null);
                        }
                    }
                }
            }
        }