how to version minder for web application data
- by dankyy1
hi all;I'm devoloping a web application which renders data from DB and also updates datas with editor UI Pages.So i want to implement a versioning mechanism for render pages got data over db again if only data on db updated by editor pages..
I decided to use Session objects for the version information that client had taken latestly.And the Application object that the latest DB version of objects ,i used the data objects guid as key for each data item
client version holder class like below
ItemRunnigVersionInformation class holds currentitem guid and last loadtime from DB
public class ClientVersionManager
{
public static List<ItemRunnigVersionInformation> SessionItemRunnigVersionInformation
{
get
{
if (HttpContext.Current.Session["SessionItemRunnigVersionInformation"] == null)
HttpContext.Current.Session["SessionItemRunnigVersionInformation"] = new List<ItemRunnigVersionInformation>();
return (List<ItemRunnigVersionInformation>)HttpContext.Current.Session["SessionItemRunnigVersionInformation"];
}
set
{
HttpContext.Current.Session["SessionItemRunnigVersionInformation"] = value;
}
}
/// <summary>
/// this will be updated when editor pages
/// </summary>
/// <param name="itemRunnigVersionInformation"></param>
public static void UpdateItemRunnigSessionVersion(string itemGuid)
{
ItemRunnigVersionInformation itemRunnigVersionAtAppDomain = PlayListVersionManager.GetItemRunnigVersionInformationByID(itemGuid);
ItemRunnigVersionInformation itemRunnigVersionInformationAtSession = SessionItemRunnigVersionInformation.FirstOrDefault(t => t.ItemGuid == itemGuid);
if ((itemRunnigVersionInformationAtSession == null) && (itemRunnigVersionAtAppDomain != null))
{
ExtensionMethodsForClientVersionManager.ExtensionMethodsForClientVersionManager.Add(SessionItemRunnigVersionInformation, itemRunnigVersionAtAppDomain);
}
else if (itemRunnigVersionAtAppDomain != null)
{
ExtensionMethodsForClientVersionManager.ExtensionMethodsForClientVersionManager.Remove(SessionItemRunnigVersionInformation, itemRunnigVersionInformationAtSession);
ExtensionMethodsForClientVersionManager.ExtensionMethodsForClientVersionManager.Add(SessionItemRunnigVersionInformation, itemRunnigVersionAtAppDomain);
}
}
/// <summary>
/// by given parameters check versions over PlayListVersionManager versions and
/// adds versions to clientversion manager if any item version on
/// playlist not found it will also added to PlaylistManager list
/// </summary>
/// <param name="playList"></param>
/// <param name="userGuid"></param>
/// <param name="ownerGuid"></param>
public static void UpdateCurrentSessionVersion(PlayList playList, string userGuid, string ownerGuid)
{
ItemRunnigVersionInformation tmpItemRunnigVersionInformation;
List<ItemRunnigVersionInformation> currentItemRunnigVersionInformationList = new List<ItemRunnigVersionInformation>();
if (!string.IsNullOrEmpty(userGuid))
{
tmpItemRunnigVersionInformation = PlayListVersionManager.GetItemRunnigVersionInformationByID(userGuid);
if (tmpItemRunnigVersionInformation == null)
{
tmpItemRunnigVersionInformation = new ItemRunnigVersionInformation(userGuid, DateTime.Now.ToUniversalTime());
PlayListVersionManager.UpdateItemRunnigAppDomainVersion(tmpItemRunnigVersionInformation);
}
ExtensionMethodsForClientVersionManager.ExtensionMethodsForClientVersionManager.Add(currentItemRunnigVersionInformationList, tmpItemRunnigVersionInformation);
}
if (!string.IsNullOrEmpty(ownerGuid))
{
tmpItemRunnigVersionInformation = PlayListVersionManager.GetItemRunnigVersionInformationByID(ownerGuid);
if (tmpItemRunnigVersionInformation == null)
{
tmpItemRunnigVersionInformation = new ItemRunnigVersionInformation(ownerGuid, DateTime.Now.ToUniversalTime());
PlayListVersionManager.UpdateItemRunnigAppDomainVersion(tmpItemRunnigVersionInformation);
}
ExtensionMethodsForClientVersionManager.ExtensionMethodsForClientVersionManager.Add(currentItemRunnigVersionInformationList, tmpItemRunnigVersionInformation);
}
if ((playList != null) && (playList.PlayListItemCollection != null))
{
tmpItemRunnigVersionInformation = PlayListVersionManager.GetItemRunnigVersionInformationByID(playList.GUID);
if (tmpItemRunnigVersionInformation == null)
{
tmpItemRunnigVersionInformation = new ItemRunnigVersionInformation(playList.GUID, DateTime.Now.ToUniversalTime());
PlayListVersionManager.UpdateItemRunnigAppDomainVersion(tmpItemRunnigVersionInformation);
}
currentItemRunnigVersionInformationList.Add(tmpItemRunnigVersionInformation);
foreach (PlayListItem playListItem in playList.PlayListItemCollection)
{
tmpItemRunnigVersionInformation = PlayListVersionManager.GetItemRunnigVersionInformationByID(playListItem.GUID);
if (tmpItemRunnigVersionInformation == null)
{
tmpItemRunnigVersionInformation = new ItemRunnigVersionInformation(playListItem.GUID, DateTime.Now.ToUniversalTime());
PlayListVersionManager.UpdateItemRunnigAppDomainVersion(tmpItemRunnigVersionInformation);
}
currentItemRunnigVersionInformationList.Add(tmpItemRunnigVersionInformation);
foreach (SoftKey softKey in playListItem.PlayListSoftKeys)
{
tmpItemRunnigVersionInformation = PlayListVersionManager.GetItemRunnigVersionInformationByID(softKey.GUID);
if (tmpItemRunnigVersionInformation == null)
{
tmpItemRunnigVersionInformation = new ItemRunnigVersionInformation(softKey.GUID, DateTime.Now.ToUniversalTime());
PlayListVersionManager.UpdateItemRunnigAppDomainVersion(tmpItemRunnigVersionInformation);
}
ExtensionMethodsForClientVersionManager.ExtensionMethodsForClientVersionManager.Add(currentItemRunnigVersionInformationList, tmpItemRunnigVersionInformation);
}
foreach (MenuItem menuItem in playListItem.MenuItems)
{
tmpItemRunnigVersionInformation = PlayListVersionManager.GetItemRunnigVersionInformationByID(menuItem.Guid);
if (tmpItemRunnigVersionInformation == null)
{
tmpItemRunnigVersionInformation = new ItemRunnigVersionInformation(menuItem.Guid, DateTime.Now.ToUniversalTime());
PlayListVersionManager.UpdateItemRunnigAppDomainVersion(tmpItemRunnigVersionInformation);
}
ExtensionMethodsForClientVersionManager.ExtensionMethodsForClientVersionManager.Add(currentItemRunnigVersionInformationList, tmpItemRunnigVersionInformation);
}
}
}
SessionItemRunnigVersionInformation = currentItemRunnigVersionInformationList;
}
public static ItemRunnigVersionInformation GetItemRunnigVersionInformationById(string itemGuid)
{
return SessionItemRunnigVersionInformation.FirstOrDefault(t => t.ItemGuid == itemGuid);
}
public static void DeleteItemRunnigAppDomain(string itemGuid)
{
ExtensionMethodsForClientVersionManager.ExtensionMethodsForClientVersionManager.Remove(SessionItemRunnigVersionInformation, NG.IPTOffice.Paloma.Helper.ExtensionMethodsFoPlayListVersionManager.ExtensionMethodsFoPlayListVersionManager.GetMatchingItemRunnigVersionInformation(SessionItemRunnigVersionInformation, itemGuid));
}
}
and that was for server one
public class PlayListVersionManager
{
public static List<ItemRunnigVersionInformation> AppDomainItemRunnigVersionInformation
{
get
{
if (HttpContext.Current.Application["AppDomainItemRunnigVersionInformation"] == null)
HttpContext.Current.Application["AppDomainItemRunnigVersionInformation"] = new List<ItemRunnigVersionInformation>();
return (List<ItemRunnigVersionInformation>)HttpContext.Current.Application["AppDomainItemRunnigVersionInformation"];
}
set
{
HttpContext.Current.Application["AppDomainItemRunnigVersionInformation"] = value;
}
}
public static ItemRunnigVersionInformation GetItemRunnigVersionInformationByID(string itemGuid)
{
return ExtensionMethodsFoPlayListVersionManager.ExtensionMethodsFoPlayListVersionManager.GetMatchingItemRunnigVersionInformation(AppDomainItemRunnigVersionInformation, itemGuid);
}
/// <summary>
/// this will be updated when editor pages
/// if any record at playlistversion is found it will be addedd
/// </summary>
/// <param name="itemRunnigVersionInformation"></param>
public static void UpdateItemRunnigAppDomainVersion(ItemRunnigVersionInformation itemRunnigVersionInformation)
{
ItemRunnigVersionInformation itemRunnigVersionInformationAtAppDomain = NG.IPTOffice.Paloma.Helper.ExtensionMethodsFoPlayListVersionManager.ExtensionMethodsFoPlayListVersionManager.GetMatchingItemRunnigVersionInformation(AppDomainItemRunnigVersionInformation, itemRunnigVersionInformation.ItemGuid);
if (itemRunnigVersionInformationAtAppDomain == null)
{
ExtensionMethodsFoPlayListVersionManager.ExtensionMethodsFoPlayListVersionManager.Add(AppDomainItemRunnigVersionInformation, itemRunnigVersionInformation);
}
else
{
ExtensionMethodsFoPlayListVersionManager.ExtensionMethodsFoPlayListVersionManager.Remove(AppDomainItemRunnigVersionInformation, itemRunnigVersionInformationAtAppDomain);
ExtensionMethodsFoPlayListVersionManager.ExtensionMethodsFoPlayListVersionManager.Add(AppDomainItemRunnigVersionInformation, itemRunnigVersionInformation);
}
}
//this will be checked each time if needed to update item over DB
public static bool IsRunnigItemLastVersion(ItemRunnigVersionInformation itemRunnigVersionInformation, bool ignoreNullEntry, out bool itemNotExistsAtAppDomain)
{
itemNotExistsAtAppDomain = false;
if (itemRunnigVersionInformation != null)
{
ItemRunnigVersionInformation itemRunnigVersionInformationAtAppDomain = AppDomainItemRunnigVersionInformation.FirstOrDefault(t => t.ItemGuid == itemRunnigVersionInformation.ItemGuid);
itemNotExistsAtAppDomain = (itemRunnigVersionInformationAtAppDomain == null);
if (itemNotExistsAtAppDomain && (ignoreNullEntry))
{
ExtensionMethodsFoPlayListVersionManager.ExtensionMethodsFoPlayListVersionManager.Add(AppDomainItemRunnigVersionInformation, itemRunnigVersionInformation);
return true;
}
else if (!itemNotExistsAtAppDomain && (itemRunnigVersionInformationAtAppDomain.LastLoadTime <= itemRunnigVersionInformation.LastLoadTime))
return true;
else return false;
}
else
return ignoreNullEntry;
}
public static void DeleteItemRunnigAppDomain(string itemGuid)
{
ExtensionMethodsFoPlayListVersionManager.ExtensionMethodsFoPlayListVersionManager.Remove(AppDomainItemRunnigVersionInformation, NG.IPTOffice.Paloma.Helper.ExtensionMethodsFoPlayListVersionManager.ExtensionMethodsFoPlayListVersionManager.GetMatchingItemRunnigVersionInformation(AppDomainItemRunnigVersionInformation, itemGuid));
}
}
when more than one client requests the page i got "Collection was modified; enumeration operation may not execute." like below..
xception: System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
at System.Collections.Generic.List1.Enumerator.MoveNextRare()
at System.Collections.Generic.List1.Enumerator.MoveNext()
at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable1 source, Func2 predicate)
at NG.IPTOffice.Paloma.Helper.PlayListVersionManager.UpdateItemRunnigAppDomainVersion(ItemRunnigVersionInformation itemRunnigVersionInformation) in
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
--- End of inner exception stack trace ---
at System.Web.UI.Page.HandleError(Exception e)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest()
at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
at System.Web.UI.Page.ProcessRequest(HttpContext context)
at ASP.playlistwebform_aspx.ProcessRequest(HttpContext context) in c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\ipservicestest\8921e5c8\5d09c94d\App_Web_n4qdnfcq.2.cs:line 0
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)-----------
how to implement version management like this scnerio?
how can i to avoid this exception?
thnx