zoukankan      html  css  js  c++  java
  • WF4.0 Beta1 自定义持久化PersistenceProvider

     

    WF4.0 Beta1 自定义持久化PersistenceProvider

    文件持久化类

    public class myFilePersistenceProvider : PersistenceProvider

    {

    string fileName;

     

    public myFilePersistenceProvider(string fileName, Guid id)

    : base(id)

    {

    this.fileName = fileName;

    }

     

    public override void Delete(object instance, TimeSpan timeout)

    {

    File.Delete(fileName);

    }

     

    public override IAsyncResult BeginDelete(object instance, TimeSpan timeout, AsyncCallback callback, object state)

    {

    Delete(instance, timeout);

     

    return myFilePersistenceCompletedAsyncResult.Complete(callback, state);

    }

     

    public override void EndDelete(IAsyncResult result)

    {

    }

     

    public override object Create(object instance, TimeSpan timeout)

    {

    using (FileStream stream = new FileStream(fileName, FileMode.Create))

    {

    //System.Runtime.Serialization

    NetDataContractSerializer serializer = new NetDataContractSerializer();

    serializer.WriteObject(stream, instance);

    }

     

    return null;

    }

     

    public override IAsyncResult BeginCreate(object instance, TimeSpan timeout, AsyncCallback callback, object state)

    {

    Create(instance, timeout);

    return myFilePersistenceCompletedAsyncResult.Complete(callback, state);

    }

     

    public override object EndCreate(IAsyncResult result)

    {

    return myFilePersistenceCompletedAsyncResult.End(result);

    }

     

    public override object Load(TimeSpan timeout)

    {

    return Load(timeout, true);

    }

     

    public override object Load(TimeSpan timeout, bool lockInstance)

    {

    object result = null;

    using (FileStream stream = new FileStream(fileName, FileMode.Open))

    {

    NetDataContractSerializer serializer = new NetDataContractSerializer();

    result = serializer.ReadObject(stream);

    }

     

    return result;

    }

     

    public override IAsyncResult BeginLoad(TimeSpan timeout, AsyncCallback callback, object state)

    {

    return BeginLoad(timeout, true, callback, state);

    }

     

    public override IAsyncResult BeginLoad(TimeSpan timeout, bool lockInstance, AsyncCallback callback, object state)

    {

    object instance = Load(timeout, lockInstance);

     

    return myFilePersistenceCompletedAsyncResult.Complete(callback, state, instance);

    }

     

    public override object EndLoad(IAsyncResult result)

    {

    return myFilePersistenceCompletedAsyncResult.End(result);

    }

     

    public override object Update(object instance, TimeSpan timeout)

    {

    using (FileStream stream = new FileStream(fileName, FileMode.Create))

    {

    NetDataContractSerializer serializer = new NetDataContractSerializer();

    serializer.WriteObject(stream, instance);

    }

     

    return null;

    }

     

    public override IAsyncResult BeginUpdate(object instance, TimeSpan timeout, AsyncCallback callback, object state)

    {

    Create(instance, timeout);

    return myFilePersistenceCompletedAsyncResult.Complete(callback, state);

    }

     

    public override object EndUpdate(IAsyncResult result)

    {

    return myFilePersistenceCompletedAsyncResult.End(result);

    }

     

    protected override TimeSpan DefaultCloseTimeout

    {

    get { return TimeSpan.FromSeconds(30); }

    }

     

    protected override TimeSpan DefaultOpenTimeout

    {

    get { return TimeSpan.FromSeconds(30); }

    }

     

    protected override void OnAbort()

    {

    }

     

    protected override IAsyncResult OnBeginClose(TimeSpan timeout, AsyncCallback callback, object state)

    {

    return myFilePersistenceCompletedAsyncResult.Complete(callback, state);

    }

     

    protected override IAsyncResult OnBeginOpen(TimeSpan timeout, AsyncCallback callback, object state)

    {

    return myFilePersistenceCompletedAsyncResult.Complete(callback, state);

    }

     

    protected override void OnClose(TimeSpan timeout)

    {

    }

     

    protected override void OnEndClose(IAsyncResult result)

    {

    }

     

    protected override void OnEndOpen(IAsyncResult result)

    {

    }

     

    protected override void OnOpen(TimeSpan timeout)

    {

    }

    }

    public class myFilePersistenceCompletedAsyncResult : IAsyncResult

    {

    object state;

    object result;

     

    public myFilePersistenceCompletedAsyncResult(object state)

    {

    this.state = state;

    }

     

    public object AsyncState

    {

    get

    {

    return this.state;

    }

    }

     

    public System.Threading.WaitHandle AsyncWaitHandle

    {

    get

    {

    return new System.Threading.ManualResetEvent(true);

    }

    }

     

    public bool CompletedSynchronously

    {

    get

    {

    return true;

    }

    }

     

    public bool IsCompleted

    {

    get

    {

    return true;

    }

    }

     

    public static IAsyncResult Complete(AsyncCallback callback, object state)

    {

    return Complete(callback, state, null);

    }

     

    public static IAsyncResult Complete(AsyncCallback callback, object state, object returnValue)

    {

    myFilePersistenceCompletedAsyncResult result = new myFilePersistenceCompletedAsyncResult(state);

    result.result = returnValue;

     

    if (callback != null)

    {

    callback(result);

    }

     

    return result;

    }

     

    public static object End(IAsyncResult result)

    {

    myFilePersistenceCompletedAsyncResult thisPtr = (myFilePersistenceCompletedAsyncResult)result;

     

    return thisPtr.result;

    }

    }

     

    功能类

    public class wfTools

    {

    public static PersistenceProvider createPersistenceProvider(Guid id)

    {

    return new myFilePersistenceProvider(id.ToString() + ".txt", id);

    }

     

    public static WorkflowInstance createWorkflow(WorkflowElement workflow, Guid id)

    {

     

    WorkflowInstance instance = new WorkflowInstance(workflow, id);

     

    bindingInstanceEvent(instance);

     

    PersistenceProvider pp = createPersistenceProvider(id);

    instance.Extensions.Add(pp);

     

    return instance;

    }

     

     

    public static WorkflowInstance loadWorkflow(WorkflowElement workflow, Guid id)

    {

    PersistenceProvider pp = createPersistenceProvider(id);

    WorkflowInstance instance = WorkflowInstance.Load(workflow, pp);

    bindingInstanceEvent(instance);

    return instance;

    }

     

     

    static void bindingInstanceEvent(WorkflowInstance instance)

    {

     

    instance.OnIdle = idel;

    instance.OnUnloaded = unloaded;

    instance.OnUnhandledException = unhandledException;

    instance.OnCompleted = completed;

    }

     

     

     

    static void unloaded()

    {

    System.Console.WriteLine("unloaded");

    }

     

    static IdleAction idel()

    {

    System.Console.WriteLine("idel");

    return IdleAction.Unload;

    }

     

    static void completed(WorkflowCompletedEventArgs e)

    {

    System.Console.WriteLine("completed");

    }

     

    static void aborted(WorkflowAbortedEventArgs e)

    {

    System.Console.WriteLine("aborted", e.Reason.Message);

    }

     

    static UnhandledExceptionAction unhandledException(WorkflowUnhandledExceptionEventArgs e)

    {

    System.Console.WriteLine("unhandledException{0}", e.UnhandledException.Message);

     

    return UnhandledExceptionAction.Abort;

    }

     

    }

     

    例子流程

    等待触发

    public class myBookmark : NativeActivity

    {

    public string bookmarkName { get; set; }

    protected override void Execute(ActivityExecutionContext context)

    {

    context.CreateNamedBookmark(bookmarkName);

    }

    }

    流程

     

     

    使用

    第一组代码

    Guid id = new Guid("DB35221A-C901-46F7-8075-E65A8D97FE13");

    wfTools.openPersistence();

     

    WorkflowInstance myInstance = wfTools.createWorkflow(new Sequence1(), id);

    myInstance.Run();

     

    System.Console.Read();

     

    产生的文件:

    第二组代码

    Guid id = new Guid("DB35221A-C901-46F7-8075-E65A8D97FE13");

    wfTools.openPersistence();

     

    WorkflowInstance myInstance = wfTools.loadWorkflow(new Sequence1(), id);

    myInstance.ResumeBookmark("wxd", null);

     

    System.Console.Read();

     

     

    文件已自动删除

     

     

  • 相关阅读:
    从请假日期列表中取得请假起止日期
    存储过程编写经验和优化措施
    安装IE8不能调试VS2003的解决办法
    javascript驗證若干DropDownList是否有選择
    欢迎光临C/S框架网 www.csframework.com
    C#开发框架钢铁贸易进销存系统演示视频
    基于.Net C/S结构系统开发框架V2.2正式发布!
    C#.NET Winform+WebService开发框架完整版本
    C#.Net C/S快速开发框架V2.2版本介绍
    专注C# .Net C/S结构系统开发框架,C/S框架网
  • 原文地址:https://www.cnblogs.com/foundation/p/1532426.html
Copyright © 2011-2022 走看看