Esempio n. 1
0
        /// <summary>
        /// Removes an object from the backing store.
        /// </summary>
        /// <param name="id">The StateServer identifier of the object to delete from the backing store.</param>
        public void Erase(CachedObjectId id)
        {
            // Write-behind will call this Erase implementation if an object is removed from the named cache.
            // If you want the object removed from the DB in that situation, this is the place to do it:

            try
            {
                if (Configuration.USE_DATABASE)
                {
                    using (SqlConnection conn = new SqlConnection(Configuration.CONNECTION_STRING))
                    {
                        conn.Open();

                        SqlCommand cmd = new SqlCommand("Delete", conn);
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.Add(new SqlParameter("@ID", Convert.ToInt32(id.GetStringKey())));

                        // Execute the command
                        cmd.ExecuteNonQuery();
                    }
                }

                Logger.WriteMessage(TraceEventType.Information, 12, $"The Erase method is called for object {id.GetStringKey()}");
            }
            catch (Exception ex)
            {
                Logger.WriteMessage(TraceEventType.Error, 13, $"Exception occurred in the Erase method: {ex.ToString()}");
            }
        }
Esempio n. 2
0
        // Sample uses one table and two stored procedures defined in the WriteBehindEventHandler_DatabaseObjects.sql file.
        // The USE_DATABASE and CONNECTION_STRING properties are defined in the Configuration class, that is part of
        // the Common project and can be overwritten by corresponding settings in the app.config file of the WriteBehindEventHandler service.
        #region IBackingStore Members

        /// <summary>
        /// Persists an object to the backing store.
        /// </summary>
        /// <param name="id">The StateServer identifier of the object to persist.</param>
        /// <param name="value">The cached object to be written to the backing store.</param>
        public void Store(CachedObjectId id, object value)
        {
            // The value parameter is the object that's been stored in the cache and needs to be written to the DB.
            // In this example, the incoming value will be of the SampleObject type.

            try
            {
                if (Configuration.USE_DATABASE)
                {
                    SampleObject saObj = value as SampleObject;
                    if (saObj != null)
                    {
                        using (SqlConnection conn = new SqlConnection(Configuration.CONNECTION_STRING))
                        {
                            conn.Open();

                            SqlCommand cmd = new SqlCommand("AddOrUpdate", conn);
                            cmd.CommandType = CommandType.StoredProcedure;
                            cmd.Parameters.Add(new SqlParameter("@ID", Convert.ToInt32(id.GetStringKey())));
                            cmd.Parameters.Add(new SqlParameter("@IntVal", saObj.IntVal));
                            cmd.Parameters.Add(new SqlParameter("@DoubleVal", saObj.DoubleVal));
                            cmd.Parameters.Add(new SqlParameter("@StringVal", saObj.StringVal));

                            // Execute the command
                            cmd.ExecuteNonQuery();
                        }
                    }
                }

                Logger.WriteMessage(TraceEventType.Information, 10, $"The Store method is called for object {id.ToString()} with value {value.ToString()}");
            }
            catch (Exception ex)
            {
                Logger.WriteMessage(TraceEventType.Error, 11, $"Exception occurred in the Store method: {ex.ToString()}");
            }
        }