Привет всем!
Помогите решить следующую проблему:
Создаю Custom Persistence Service для моего флоуа по примеру из WFSample msdn.
Внутри флоу есть параметры Dictionary<> params. При сохранении флоу необходимо сохранять и параметры (аналогично восстановление флоу). Для простоты — сохранение в файл.
Проблема с загрузкой параметров. в дебаг режиме можно отследить, что сервис сохраняет параметры в файл, и восстанавливает их при восстановлении флоу. Но вот когда к уже восстановленному флоу обращаешься для получения параметров — их нет, т.е. params = 0!!!
В чем может быть проблема, что я делаю не так?! Плиз ХЕЛП! 2 ночи не сплю ;(
Код сервиса частично привожу ниже.
public class myPersistenceService : WorkflowPersistenceService
{
protected bool unloadOnIdle;
protected TimerList timerList;
protected string dir = "c:\\persist\\";
public myPersistenceService()
{
unloadOnIdle = true;
timerList = new TimerList();
timerList.EventHandler += new EventHandler<TimerEventsArg>(timerList_EventHandler);
}
public myPersistenceService(bool _f)
{
unloadOnIdle = _f;
timerList = new TimerList();
timerList.EventHandler += new EventHandler<TimerEventsArg>(timerList_EventHandler);
}
public void Runtime_WorkflowCompleted(object sender, WorkflowCompletedEventArgs e)
{
timerList.RemoveKey(e.WorkflowInstance.InstanceId);
}
protected override void Start()
{
timerList.Load(this.dir + "timers.bin");
base.Start();
}
protected override void Stop()
{
timerList.enable = false;
timerList.Save(this.dir + "timers.bin");
base.Stop();
}
void timerList_EventHandler(object sender, TimerEventsArg e)
{
if (this.Runtime.IsStarted == false)
this.Runtime.StartRuntime();
WorkflowInstance instance = this.Runtime.GetWorkflow((Guid)e.Instance);
if (instance != null)
instance.Load();
}
protected override System.Workflow.ComponentModel.Activity LoadCompletedContextActivity(Guid scopeId, System.Workflow.ComponentModel.Activity outerActivity)
{
throw new Exception("The method or operation is not implemented.");
}
protected override System.Workflow.ComponentModel.Activity LoadWorkflowInstanceState(Guid instanceId)
{
star.ManageProcess activity;
using (FileStream fs = new FileStream(string.Format(dir + "{0}.sav1", instanceId),
FileMode.Open,
FileAccess.Read))
{
byte[] byts = new byte[fs.Length];
fs.Read(byts, 0, byts.Length);
activity = (star.ManageProcess)System.Workflow.Runtime.Hosting.WorkflowPersistenceService.RestoreFromDefaultSerializedForm(byts, null);
fs.Close();
}
activity.Parametrs = (Dictionary<string, object>)LoadParametrs(instanceId);
return activity;
}
protected override void SaveCompletedContextActivity(System.Workflow.ComponentModel.Activity activity)
{
throw new Exception("The method or operation is not implemented.");
}
protected override void SaveWorkflowInstanceState(System.Workflow.ComponentModel.Activity rootActivity, bool unlock)
{
Guid instance = (Guid)rootActivity.GetValue(Activity.ActivityContextGuidProperty);
using (FileStream fs = new FileStream(string.Format(this.dir + "{0}.sav1", instance.ToString()),
FileMode.OpenOrCreate,
FileAccess.Write))
{
byte[] byts = WorkflowPersistenceService.GetDefaultSerializedForm(rootActivity);
fs.Write(byts, 0, byts.Length);
fs.Close();
}
///save params
SaveParametrs(rootActivity);
// See when the next timer (Delay activity) for this workflow will expire
TimerEventSubscriptionCollection timers = (TimerEventSubscriptionCollection)rootActivity.GetValue(TimerEventSubscriptionCollection.TimerCollectionProperty);
TimerEventSubscription subscription = timers.Peek();
if (subscription != null)
{
timerList.AddWorkflow(subscription.WorkflowInstanceId, subscription.ExpiresAt);
}else
timerList.AddWorkflow(instance);
}
private void ReloadWorkflow(object id)
{
// Reload the workflow so that it will continue processing
this.Runtime.GetWorkflow((Guid)id).Load();
}
protected override bool UnloadOnIdle(System.Workflow.ComponentModel.Activity activity)
{
return (unloadOnIdle);
}
protected override void UnlockWorkflowInstanceState(System.Workflow.ComponentModel.Activity rootActivity)
{
throw new Exception("The method or operation is not implemented.");
}
public Dictionary<Guid, DateTime> GetAllWorkflows()
{
return timerList.GetAllWorkflows();
}
#region work with params
public void SaveParametrs(Activity rootActivity)
{
Guid uid = (Guid)rootActivity.GetValue(Activity.ActivityContextGuidProperty);
using (FileStream fs = new FileStream(string.Format(this.dir + "param_{0}.sav", uid.ToString()),
FileMode.OpenOrCreate,
FileAccess.Write))
{
BinaryFormatter formatter = new BinaryFormatter();
try
{
formatter.Serialize(fs, ((ManageProcess)rootActivity).Parametrs);
}
catch (SerializationException err)
{
throw new SerializationException(err.Message);
}
}
}
public object LoadParametrs(Guid instance)
{
try
{
using (FileStream fs = new FileStream(string.Format(dir + "param_{0}.sav", instance.ToString()),
FileMode.Open,
FileAccess.Read))
{
BinaryFormatter formatter = new BinaryFormatter();
try
{
Dictionary<string, object> parametrs = (Dictionary<string, object>)formatter.Deserialize(fs);
return parametrs;
}
catch (SerializationException err)
{
throw new SerializationException(err.Message);
}
}
}
catch (Exception err)
{
throw new Exception(err.Message);
}
}
#endregion
}
После загрузки workflow — проверяю параметры в них а там их нет, хотя
отладка показала что они загрузились туда!
System.Collections.ObjectModel.ReadOnlyCollection<WorkflowInstance> ws = runtimeWF.GetLoadedWorkflows();
foreach (WorkflowInstance inst in ws)
{
star.ManageProcess mmm = (star.ManageProcess)runtimeWF.GetWorkflow(inst.InstanceId).GetWorkflowDefinition();
Dictionary<string, object> param = mmm.Parametrs;
}
Очень нужна помощь!
данное сообщение получено с www.gotdotnet.ru
ссылка на оригинальное сообщение