我正在努力实现以下目标:
一个简单的用例是:用于审核文档并批准或拒绝文档的工作流程。将为此创建工作流程,通知个人,并且只要他们想要,他们就可以通过批准或拒绝审核来提供他们的反馈。
我以安德鲁朱的代码为例,http://xhinker.com/post/WF4.aspx。
我遇到的问题是:
WaitForRunnableInstance
无限期等待,因此我从未收到通知我的工作流程已启动并持久保存到数据库; BlockingBookmarks
设置为我的自定义活动的ID,ExecutionStatus
设置为空闲,IsInitialized
设置为1,IsSuspended
设置为0,IsCompleted
设置为0,IsReadyToRun
设置为0。我已经开始讨论微软论坛,可以在http://social.msdn.microsoft.com/Forums/en-US/wfprerelease/thread/6262874d-3493-4be1-bd05-b990307e1875/看到并得到一些反馈,但有些事情仍然不对。
有关于此的任何想法吗?使用自定义活动的长时间运行工作流程的任何有用模式?
谢谢!
答案 0 :(得分:5)
这通常是长时间运行的工作流的最小示例,它等待控制台上的用户输入。 (此代码从未执行过,仅作为示例)
/// Activity that waits on bookmark for
/// someone to send it some text
///
public sealed class ReadLine: NativeActivity<string>
{
[RequiredArgument]
public InArgument<string> BookmarkName { get; set; }
protected override bool CanInduceIdle
{
get
{
return true;
}
}
protected override void Execute(NativeActivityContext context)
{
context.CreateBookmark(
BookmarkName.Get(context),
new BookmarkCallback(OnReadComplete));
}
void OnReadComplete(NativeActivityContext context, Bookmark bookmark, object state)
{
context.SetValue(base.Result, state as string);
}
}
/// Program that uses ReadLine activity's bookmark to persist
/// workflow and waits for user input to resume it
///
public class Program
{
static InstanceStore InstanceStore;
static Activity Activity = GetExampleActivity();
static AutoResetEvent unloadEvent = new AutoResetEvent(false);
static Guid WfId;
static WorkflowApplication WfApp;
const string READ_LINE_BOOKMARK = "ReadLineBookMark";
static void Main()
{
CreateInstanceStore();
CreateWorkflowApp();
// Start workflow application and wait for input
StartAndUnload();
//Get user input and send it to ReadLine bookmark reviving workflow
GetInputAndComplete();
}
static void StartAndUnload()
{
WfApp.Run();
WfId = app.Id;
// !! Workflow will go idle on bookmark, no need to call Unload()
unloadEvent.WaitOne();
}
static void GetInputAndComplete()
{
var input = Console.ReadLine();
// We've the text input, let's resume this thing
WfApp.Load(WfId);
WfApp.ResumeBookmark(READ_LINE_BOOKMARK, input);
unloadEvent.WaitOne();
}
static void CreateInstanceStore()
{
InstanceStore = new SqlWorkflowInstanceStore("connection string");
var handle = InstanceStore.CreateInstanceHandle();
var view = InstanceStore.Execute(
handle,
new CreateWorkflowOwnerCommand(),
TimeSpan.FromSeconds(5));
handle.Free();
InstanceStore.DefaultInstanceOwner = view.InstanceOwner;
}
static void CreateWorkflowApp()
{
WfApp = new WorkflowApplication(Activity)
{
InstanceStore = InstanceStore,
};
WfApp.PersistableIdle = (e) => { return PersistableIdleAction.Unload; }
WfApp.Unloaded = (e) =>
{
Console.WriteLine("WF App Unloaded\n");
unloadEvent.Set();
};
WfApp.Completed = (e) =>
{
Console.WriteLine("\nWF App Ended: {0}.", e.CompletionState);
};
}
static Activity GetExampleActivity()
{
var response = new Variable<string>();
return return new Sequence()
{
Variables = { response },
Activities =
{
new WriteLine()
{
Text = new InArgument<string>("Type some word:")
},
new ReadLine()
{
BookmarkName = READ_LINE_BOOKMARK,
Result = new OutArgument<string>(response)
},
new WriteLine()
{
Text = new InArgument<string>((context) => "You've typed: " + response.Get(context))
}
}
};
}
话虽如此,请考虑使用IIS和AppFabric,您不会后悔。 AppFabric,点击了6次,负责在WF中实现两个必须痛苦的事情:持久性和监控。如果选择此路径,您将永远不需要编写代码。
将您的工作流部署为WCF应用程序,您只需将其称为任何其他WCF合同。你有 OperationContracts 这些是接收活动(如果花费太长时间等待和持久)和相应的发送活动(那些将值返回给客户端的活动)。你甚至有他们之间的相关概念。 AppFabric负责恢复工作流程,只需将之前初始化的关联句柄传递给它。
AppFabric为您提供配置UI,用于配置持久性存储,监控和其他选项,例如空闲和/或持续之前的时间。
您可以可视化Active / Idle / Suspended工作流程,监控数据等等。